Logo 
Search:

MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Cannot use the VBA add table command of MS word in MS Excel VBA

  Asked By: Don    Date: Feb 16    Category: MS Office    Views: 1623
  

I have encountered a problem of trying to create a new document in
MS word and add a table on it:
_____________________________________________________


I typed the following codes in excel VBA editor:


Sub TestWord_MakeTables()

Dim wapp As New Word.Application

With wapp
.NewDocument.Add Filename:="xxx.doc"
End With

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2,
NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:= _
wdAutoFitFixed
'.Selection.Text = "X"
'Tables.Add Range:=Selection.Range, numrows:=10, numcolumns _
:=5, DefaultTableBehavior:=wdWord9TableBehavior,
AutoFitBehavior:= _
wdAutoFitFixed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
End Sub




This is a preliminary program of my excel development program... the
sentence between the two '~~~~~~~~~~~~~~~~~~~ lines is copied from a
Macro I recorded in MS Word macro recorder.

But when i run the above segment in excel, it alerts me with "wrong
no. of parameters or incorrect properties" in that senetence.

I already set the excel vba reference to a MSWord11.0 object
library. Actually the excel & word are from the same MS office
programs.

I would like to ask what's the problem happening in the codes?

Any helps will be highly appreciated.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Eamon Jones     Answered On: Feb 16

I'd suspect that "ActiveDocument" is the Excel workbook rather than the Word
document. I think you'll need to reference Word via "wapp" in the same way
that you do to create  the new document. In the first instance, just put
wapp.ActiveDocument... and see what it makes of it.

 
Answer #2    Answered By: Rachael Ferguson     Answered On: Feb 16

This is a little bit outside my area of expertise but it seems to me that by
using wapp.NewDocument all you have done is to add  a reference in the task
pane (in a non-visible Word App) to a non existent document. There is
probably no active document  and no selection. You are therefore passing
non-existent parameters to the Tables.Add.

I think an approach may be to dim MyDoc as Word.Doc
Set MyDoc = wapp.Documents.add
Then either use MyDoc.activate
Or use MyDoc.Tables.Add rather than ActiveDocument.Tables.Add

I am sure one of the Word VBA Experts will be able to improve on this

 




Tagged: