writing to a table in wrod from excel

J

Jason V

I recorded the following macro in word
Sub Macaddtable()
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=6,
NumColumns:= _
4, 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
Selection.TypeText Text:="Qty"
Selection.MoveRight Unit:=wdCharacter, Count:=1

End Sub
I put it in excel and it errors on
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=6, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow
saying not the right numbe of arguments.
I have references to word 11.0 .

Any help please?
 
S

Shasur

Hi Jason

Sine you are trying to add table from Excel without Selection, the commands
selection.range, selection.typetext etc do not work

You need to be specific like ActiveDocument.Paragraphs(1).Range etc

Also if you are adding a new Word Document, try like the following

Dim oWA As Word.Application
Set oWA = New Word.Application
Set oWD = oWA.Documents.Add

oWD.Tables.Add Range:=ActiveDocument.Paragraphs(1).Range, NumRows:=6,
NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitWindow

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top