I simply cannot get this to work. I am trying to run a word Macro from excel (before importing a table). Here is the code:
Option Explicit
Sub RunWdMacro()
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim wdFileName As Variant
Set WordApp = CreateObject("Word.Application")
wdFileName = Application.GetOpenFilename("Word files ,*.doc;*.docx;*.docm", , _
"Browse for file containing table to be imported")
WordApp.Visible = True
'change TEST to the name of your macro in word
WordApp.Run "Delcolumnbreaks"
WordApp.Quit SaveChanges:=wdDoNotSaveChanges
Set WordApp = Nothing
End Sub
I am prompting for a word file to open, but it errors out at:
WordApp.Run "Delcolumnbreaks"
With error "Run-time error '-2147352573 (800200003) Can't run the specified macro. The macro is in the normal section as module 1
Any thoughts?
Thanks
Related
Here is an abbreviated copy of the code. I put a note in by the line that sometimes will generate an error 424.
Sub Continue()
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim SalesPer As Range
On Error GoTo ErrorTrap2
Set wdApp = CreateObject("Word.Application") 'Create an instance of word
Set wdDoc = wdApp.Documents.Open(DocDirectory & "SpartanQuote.docx", ReadOnly:=True) 'Open word file
Range("DataRange").Select
Selection.Copy
wdApp.Selection.Paste 'Sometimes I'll get error 424 on this line, but not always
'Reset variables
Set wdDoc = Nothing
Set wdApp = Nothing
Application.CutCopyMode = False
Range("ModelSelected").Select
Application.ScreenUpdating = True
Exit Sub
ErrorTrap2:
MsgBox "FATAL ERROR. Sorry. A fatal error occured. Error number: " & Str(Err.Number)
wdApp.Visible = True
Set wdDoc = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = True
End Sub
To access the Selection object you are using the Word Application instance in the code:
wdApp.Selection.Paste 'Sometimes I'll get error 424 on this line, but not always
'Reset variables
Instead, you may try using the document to get the selection object or any range in the document and then try to call the Paste method there. See Working with the Selection Object for possible scenarious.
I'm looking to create a spell check macro for protected word documents. I'm more familiar with Excel VBA and I just created a similar project for protected spreadsheets so I attempted to follow the same logic. So far my code copies misspelled words from the office document, into a new excel workbook and then runs spellcheck, but I am having trouble pasting the new value back into the original word document. I can't have this require "adding a reference library" as this will need to be portable and run without end user intervention.
Here is what I have so far:
Sub SpellCheckDoc()
Dim lockedFields As Long
Dim unlockedFields As New Collection
For Each theFields In ActiveDocument.Fields
If theFields.Locked = True Then
lockedFields = lockedFields + 1
Else
unlockedFields.Add theFields
End If
Next theFields
If lockedFields = ActiveDocument.Fields.Count Then Exit Sub
'Word
Dim objWord As Object 'Word.Application
Set objWord = GetObject(, "Word.Application")
'Excel
Dim objExcel As Object, objWB As Object
Set objExcel = CreateObject("Excel.Application")
Set objWB = objExcel.Workbooks.Add
objExcel.Visible = True
Set wb = objExcel.ActiveWorkbook
Set ws = wb.Worksheets("Sheet1")
For Each theFields In unlockedFields
If CheckSpelling(theFields.Result.Text) = False Then
theFields.Copy ' Select text from Word Doc
'Paste into new workbook and spellcheck
With ws
.Range("A1").Select
.Paste
.Range("A1").CheckSpelling
.Range("A1").Copy
End With
objWord.theFields.Paste ''' This line doesn't work
End If
Next theFields
End Sub
Change theFields.Result.Text. So you can do .CheckSpelling in Excel, then make theFields.Result.Text = .Range("A1").Text
Dim correctSpelling as String
With ws
.Range("A1").Select
.Paste
.Range("A1").CheckSpelling
correctSpelling = .Range("A1").Text
End With
theFields.Result.Text = correctSpelling
End If
Next theFields
End Sub
The following VBA code works perfectly in Windows to copy a table from Excel and paste it into a Word template.
On Mac the pasting step doesn't work and makes Excel and Word crash (no error message, just stop responding and I have to force quit).
I have tried using
.Selection.PasteAndFormat Type:=wdFormatOriginalFormatting
instead of
.Selection.PasteExcelTable False, False, False
but I get the same behavior.
If I stop the macro before the last step, and paste manually into Word, it works. The table is correctly copied into the clipboard and I can paste it.
Sub testPasteMac()
Dim wordApp As Object
Dim FName As String
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
'Copy the table from Excel
ws.Range("A1:E4").Copy
'Open Word
On Error Resume Next
Set wordApp = GetObject(, "word.Application")
If Err = 429 Then
Set wordApp = CreateObject("word.Application")
Err.Clear
End If
'Display Word application
On Error Resume Next
wordApp.Activate
On Error GoTo 0
'Path to Word template file
FName = ThisWorkbook.Path & Application.PathSeparator & "FileName.dotx"
'Open template as new file
wordApp.Documents.Add Template:=FName
'Paste clipboard into Word document
With wordApp
.Selection.Goto what:=-1, Name:="bookmarkName"
.Selection.PasteExcelTable False, False, False
'.Selection.PasteAndFormat Type:=wdFormatOriginalFormatting
End With
End Sub
I use Excel for Mac version 16.18
VisualBasic 7.1
I have a Word document that is received periodically that needs to be combined with other data already in Excel as part of a larger output. I can copy and paste the whole document (WholeStory) from Word into Excel, but I want to make this part of the whole Excel output macro. My current code for this portion is below, and it works fine except that it pastes nothing. There is no data to paste when it gets there, so I guess it is either not picking it up to start with or not carrying it over. Looking for assistance with this. Thanks very much!
Sub ImportSectHWord()
Dim objWord As Object
Dim objDoc As Object
Dim wdFileName
Set objWord = CreateObject("word.Application")
wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")
If wdFileName = False Then Exit Sub
Set objDoc = GetObject(wdFileName)
objWord.Documents.Open (wdFileName)
objWord.Selection.WholeStory
Selection.Copy
Worksheets("H Import").Select
Range("A1").Select
ActiveSheet.Paste
objDoc.Close SaveChanges:=wdDoNotSaveChanges
objWord.Quit
End Sub
The statement
Selection.Copy
is copying whatever is currently selected in Excel.
To copy the Selection object in Word, use
objWord.Selection.Copy
It is always advisable to qualify what objects you are referring to when using methods and properties, even when VBA provides a default object.
Sub ImportSectHWord()
'It is better to always define constants, even though they will default to zero
' which just happens to be the desired value in this case
Const wdDoNotSaveChanges As Long = 0
Dim objWord As Object
Dim objDoc As Object
Dim wdFileName
Set objWord = CreateObject("word.Application")
wdFileName = Application.GetOpenFilename("Word Documents, *.doc*")
If wdFileName = False Then Exit Sub
Set objDoc = GetObject(wdFileName)
objWord.Documents.Open (wdFileName)
objWord.Selection.WholeStory
objWord.Selection.Copy
ActiveWorkbook.Worksheets("H Import").Select
ActiveWorkbook.Worksheets("H Import").Range("A1").Select
ActiveWorkbook.ActiveSheet.Paste
objDoc.Close SaveChanges:=wdDoNotSaveChanges
objWord.Quit
End Sub
I am trying to create an excel macro that opens Word and creates a table. I made a macro in Word to create the table and copied it into the Excel macro. When I run it, I get this error:
run-time error '450'
wrong number of argument or invalid property assignment
at the point which excel is to execute the MS Word macro (which was recorded in Word):
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
EDIT: I have set references for the Word doc
' Declare the variables
Dim WordApp As Object
Dim WordDoc As Object
Dim wksSource As Worksheet
' Assign the active worksheet to an object variable
Set wksSource = ActiveSheet
' Create an instance of the Word application
Set WordApp = CreateObject("Word.Application")
' Make the Word application visible
WordApp.Visible = True
' Open the specified Word document and assign it to an object variable
Set WordDoc = WordApp.Documents.Open("C:\Users\Briet\Documents\PAJ\labels.dotm")
Make sure you've added a reference to the Word Object Lirary in your Excel VBA project.
This worked for me:
Sub Tester()
Dim wd As Word.Application
Set wd = GetObject(, "word.application") 'get reference to open Word document
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range, NumRows:=1, NumColumns:= _
2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
End Sub
Note that because Selection is a native Excel object, you need to qualify that to indicate you're referring to the Selection in Word
wd.ActiveDocument.Tables.Add Range:=wd.Selection.Range