Why does the following not work?

Public Sub PrintErrorReportToWord()
Dim wordApp As Object
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True

With wordApp
.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow

With Selection.Tables(1)
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
End With

End Sub

Thanks

EM

RE: Insert Table into Word Doc by JeanGuyMarcil

JeanGuyMarcil
Fri Jun 13 11:55:01 PDT 2008

"ExcelMonkey" wrote:

> Why does the following not work?
>
> Public Sub PrintErrorReportToWord()
> Dim wordApp As Object
> Set wordApp = CreateObject("Word.Application")
> wordApp.Visible = True
>
> With wordApp
> .Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
> 3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
> wdAutoFitWindow
>
> With Selection.Tables(1)
> If .Style <> "Table Grid" Then
> .Style = "Table Grid"
> End If
> .ApplyStyleHeadingRows = True
> .ApplyStyleLastRow = True
> .ApplyStyleFirstColumn = True
> .ApplyStyleLastColumn = True
> End With
> End With
>
> End Sub

Have you even tried debugging your code?

If you had, you would have immediately noticed that after the line
wordApp.Visible = True
has executed, Word does appear on the screen, but there are no document...
where can the table be created it there are no documents?

In case you do not know how to debug, see the Debug menu in VBA. Use F8 to
execute the code one line at the time. If a line is expected to produce
Result A, but produces Result B instead, you can stop the execution, fix the
code and try again, etc...

Now, as a general practice, you might find it usful to write the code in
Word first, this way, you have Intellisense helping you with properties and
methods available to each object.
For instance, .Tables is a property of the .Document object, not of the
Application one (wordApp in your code).

Even when using late binding code, the property has to belong the the object
otherwise the compiler will not understand what to do with the code...

Also, when automating Word, it is not a good idea to work with the Selection
object. The Selection object is usually linked to an active window... Try
using the range object instead.

Once you have written the code in Word and tested it, you can bring it into
Excel (or any other VBA compatible qpplication) and convert it to late
binding using Objects instead of named objects like (Document or Table...)

Try this code:

Dim wordApp As Object
Dim doc As Object
Dim tbl As Object

Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True


With wordApp
Set doc = .Documents.Add
Set tbl = doc.Tables.Add(Range:=doc.Range.Paragraphs(1).Range,
NumRows:=1, _
NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitWindow)
With tbl
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
End With


Lastly, I am not sure what you want to do with the four ".Apply..."
statements. The Table Grid style is neutral so that those four statements
will not change the look of the table. If, on the other hand, you are using a
style that does have an impact on the last, first rows, etc, like "Table
Classic 4," then those four attributes are "True" by default. So you do not
need to specify that. You would need that if, for example, you were applying
a table style that bolded the last row, but you did not want that last row
bold. Then you would use somehting like:

.ApplyStyleLastRow = False

Good Luck
from
WordMonkey!

RE: Insert Table into Word Doc by ExcelMonkey

ExcelMonkey
Fri Jun 13 12:56:01 PDT 2008

Thanks for you help

EM

"Jean-Guy Marcil" wrote:

> "ExcelMonkey" wrote:
>
> > Why does the following not work?
> >
> > Public Sub PrintErrorReportToWord()
> > Dim wordApp As Object
> > Set wordApp = CreateObject("Word.Application")
> > wordApp.Visible = True
> >
> > With wordApp
> > .Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _
> > 3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
> > wdAutoFitWindow
> >
> > With Selection.Tables(1)
> > If .Style <> "Table Grid" Then
> > .Style = "Table Grid"
> > End If
> > .ApplyStyleHeadingRows = True
> > .ApplyStyleLastRow = True
> > .ApplyStyleFirstColumn = True
> > .ApplyStyleLastColumn = True
> > End With
> > End With
> >
> > End Sub
>
> Have you even tried debugging your code?
>
> If you had, you would have immediately noticed that after the line
> wordApp.Visible = True
> has executed, Word does appear on the screen, but there are no document...
> where can the table be created it there are no documents?
>
> In case you do not know how to debug, see the Debug menu in VBA. Use F8 to
> execute the code one line at the time. If a line is expected to produce
> Result A, but produces Result B instead, you can stop the execution, fix the
> code and try again, etc...
>
> Now, as a general practice, you might find it usful to write the code in
> Word first, this way, you have Intellisense helping you with properties and
> methods available to each object.
> For instance, .Tables is a property of the .Document object, not of the
> Application one (wordApp in your code).
>
> Even when using late binding code, the property has to belong the the object
> otherwise the compiler will not understand what to do with the code...
>
> Also, when automating Word, it is not a good idea to work with the Selection
> object. The Selection object is usually linked to an active window... Try
> using the range object instead.
>
> Once you have written the code in Word and tested it, you can bring it into
> Excel (or any other VBA compatible qpplication) and convert it to late
> binding using Objects instead of named objects like (Document or Table...)
>
> Try this code:
>
> Dim wordApp As Object
> Dim doc As Object
> Dim tbl As Object
>
> Set wordApp = CreateObject("Word.Application")
> wordApp.Visible = True
>
>
> With wordApp
> Set doc = .Documents.Add
> Set tbl = doc.Tables.Add(Range:=doc.Range.Paragraphs(1).Range,
> NumRows:=1, _
> NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, _
> AutoFitBehavior:=wdAutoFitWindow)
> With tbl
> If .Style <> "Table Grid" Then
> .Style = "Table Grid"
> End If
> .ApplyStyleHeadingRows = True
> .ApplyStyleLastRow = True
> .ApplyStyleFirstColumn = True
> .ApplyStyleLastColumn = True
> End With
> End With
>
>
> Lastly, I am not sure what you want to do with the four ".Apply..."
> statements. The Table Grid style is neutral so that those four statements
> will not change the look of the table. If, on the other hand, you are using a
> style that does have an impact on the last, first rows, etc, like "Table
> Classic 4," then those four attributes are "True" by default. So you do not
> need to specify that. You would need that if, for example, you were applying
> a table style that bolded the last row, but you did not want that last row
> bold. Then you would use somehting like:
>
> .ApplyStyleLastRow = False
>
> Good Luck
> from
> WordMonkey!