Word Automation VBA, Common Errors
In the article below I’ve explained how you can automate a word document from another application using VBA:
Though the automation process itself is quite straight forward, there are many things you will need to keep in mind. Consider the code below:
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
This code was generated by macro recorder in word. It creates a table with 8 rows and 4 columns. Now consider the code below where we are trying to automate this process from another office application:
Sub main()
Dim objWord As Object
Dim objDoc As Object
'automation
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
''''
'Genrated by the macro recorder
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End Sub
This code will cause several errors.
Contents
Compile Error, Variable Not Defined:
The first error you will encounter is the “Compile Error, Variable Not Defined” error:
The reason for this error are the variable:
- wdWord9TableBehavior
- wdAutoFitFixed
These variables are defined in the Word Object Library Namespace. Inside the word vba editor, we are inside the Word Object Library Namespace, therefore these variables are recognized there. When we try to run this code from another application (for example Excel) these variables are not recognized and therefore will cause an error. There are 2 methods for overcoming this error:
Method 1: If we are using early binding we could refer to them through the Word Object Library:
- WORD.wdWord9TableBehavior
- WORD.wdAutoFitFixed
Result:
Sub main2()
Dim objWord As Word.Application
Dim objDoc As Word.Document
'automation
Set objWord = New Application
objWord.Visible = True
Set objDoc = objWord.Documents.Add
''''
'Genrated by the macro recorder
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=Word.wdWord9TableBehavior, _
AutoFitBehavior:=Word.wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End Sub
Method 2: If we are using late binding the best method would be to replace those variables with their values. This could be achieved by running a similar code inside the word VBA editor and inserting a breakpoint at that line. By hovering the mouse over those variables you will be able to get their values:
By doing this you will see the value for the variables are:
- wdWord9TableBehavior =1
- WORD.wdAutoFitFixed = 0
Error 2, ActiveDocument not Recognized:
After fixing the previous error, the next error is also a “Compile Error, Variable Not Defined” error. This time the reason is because of the use of the ActiveDocument variable.
Sub main3()
Dim objWord As Object
Dim objDoc As Object
'automation
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
''''
'Genrated by the macro recorder
ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=1, _
AutoFitBehavior:=0
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End Sub
The ActiveDocument object is a part of the Word.Application namespace. When working inside the word VBA editor we are inside the Word.Application namespace therefore we can use the variable ActiveDocument directly. When working in the VBA editor of some other application we can’t do this. The solution would be to change ActiveDocument to objWord.ActiveDocument:
Sub main4()
Dim objWord As Object
Dim objDoc As Object
'automation
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
''''
'Genrated by the macro recorder
objWord.ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=1, _
AutoFitBehavior:=0
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End Sub
Run-time error ‘450’. Wrong Number of Arguments or Invalid Property Assignment:
After fixing the previous errors, the next error the program will encounter is the “Run-time error ‘450’. Wrong Number of Arguments or Invalid Property Assignment”. The reason for this error are the Selection variables used:
Sub main5()
Dim objWord As Object
Dim objDoc As Object
'automation
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
''''
'Genrated by the macro recorder
objWord.ActiveDocument.Tables.Add Range:=Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=1, _
AutoFitBehavior:=0
With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End Sub
Similar to the ActiveDocuments variable, the Selection variable is a member of the Word.Application object. If we want to use this variable outside of the Word VBA editor, we have to refer to it using Word.Application object:
Sub main6()
Dim objWord As Object
Dim objDoc As Object
'automation
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add
''''
'Genrated by the macro recorder
objWord.ActiveDocument.Tables.Add _
Range:=objWord.Selection.Range, _
NumRows:=8, NumColumns:=4, _
DefaultTableBehavior:=1, _
AutoFitBehavior:=0
With objWord.Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.ApplyStyleRowBands = True
.ApplyStyleColumnBands = False
End With
End Sub
See also:
- Word VBA, Open Document
- Word Automation VBA, Common Errors
- Word VBA, Apply Macro to Multiple Files
- Word VBA, Modify Header For Multiple Files
- VBA, Write Excel Values to Word Document
- VBA, Automating Word From Excel
- Automation Errors in VBA
If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com
One thought on “Word Automation VBA, Common Errors”