Append a Table inside word document using vbscript

J

jam

Hi All,

Iam using the vbscript below. this does add an empty table in a word
document. this erases all the contents in the word document and then
adds the table. I need to append the table(means to add the table at
the end of the file) kindly help me in this.

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Open(strFilePath,1,True)
Set objRange = objDoc.Range()
objRange.Tables.Add objRange, intRowCount, intColumnCount
Set objTable = objRange.Tables(1)
objDoc.SaveAs(strFilePath)
objWord.Quit

Thanks,
Ruby
 
H

Helmut Weber

Hi Ruby,

I don't know about vbscript,
but using ExcelVBA as a substitute,
this one works for me:

Public Sub test()
Dim objWord As Object
Dim objTable As Object
strFilePath = "c:\test\002.doc"
Dim intRowCount As Long
Dim intColumnCount As Long
intRowCount = 5
intColumnCount = 5

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
' set read only parameter to false
' otherwise you get stuck
Set objDoc = objWord.Documents.Open(strFilePath, 1, False)
Set objrange = objDoc.Range()
' collapse the range to it's end
' there are other ways to do that
objrange.Start = objrange.End
objrange.tables.Add objrange, intRowCount, intColumnCount
' In case there are tables already
Set objTable = objDoc.tables(objDoc.tables.Count)
objDoc.SaveAs (strFilePath)
objWord.Quit
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
T

Tony Jollans

The reason you lose everything in the document is that you are telling it
place the table where the rest of the content already is. The first
parameter to the Table Add is where to put it and you have said put it at
objRange, having just set objRange to be the whole document content. When
the table is put there, what was there before gets pushed away. See Helmut's
reply for how to avoid this by collapsing the range.
 

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