Click to See Complete Forum and Search --> : More trouble with MS Word Tables


Ghost308
July 11th, 2001, 11:31 AM
Thanks to sotoasty, I have got my code (which i show below) creating the table I need in Word and filling cells with data. Now then, let's say I exit Word but leave my program still running. I hit my report button again to make Word come back up with the table and all, but i get this error instead...

Method "Add" of "Tables" Failed.

I only get this error after the program has launched Word once already, and I ONLY get it if this table is involved. I tried removing the line of code below (which draws the table) and I was able to run Word over and over without this error. Any ideas?


wrdapp.Selection.Tables.Add Range:=Selection.Range, NumRows:=31, NumColumns:=5,DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed

sotoasty
July 11th, 2001, 12:34 PM
ok, This one is a bit more difficult. The problem comes when you initially set up the wrdapp object, it creates a new instance of word. If you close that instance of word, your application has no way of knowing that. Here is one thing I did.


on error resume next
Err = 0
a = WrdApp.Visible
If Err = 462 then
set WrdApp = new Word.Application
End If
Err = 0
WrdApp.Visible = false
If Err <> 0 then
WrdApp.Quit
set WrdApp = new Word.Application
WrdApp.Visible = false
End If




This will check for the current instance of your word application. The other thing is if you have created a document, then closed it from with in word, you might need to open it back up again. The easiest way to take care of that is to save the document when you initially create it in your VB app. Here is my code snippet.



set WrdDoc = WrdApp.Documents.Add
for I = 1 to 5000
DoEvents
next I
WrdDoc.SaveAs App.Path + "\Templates\" + FileNa.Text, , , , , , true





Now that you know the name of the document, you can always open it back up with...


set WrdDoc = WrdApp.Documents.Open(App.Path + "\Templates\" + FileNa.Text, , ReadOnly:=false)





Hope this helps.

Ghost308
July 11th, 2001, 01:09 PM
Okay- that fixed the error I was getting, but now when Word is launched after the first time, it does not create the table at all. All the data I am sending to the table is just typed out as one huge string.

'create instance of word
Dim wrdapp

on error resume next
Err = 0
a = wrdapp.Visible
If Err = 462 then
set wrdapp = new Word.Application
End If
Err = 0
wrdapp.Visible = false
If Err <> 0 then
wrdapp.Quit
set wrdapp = new Word.Application
wrdapp.Visible = false
End If

'create the table
'THIS is THE PART THAT ONLY WORKS on THE FIRST TRY
wrdapp.Selection.Tables.Add Range:=Selection.Range, NumRows:=31, NumColumns _
:=5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
wrdapp.Selection.Tables(1).AutoFormat Format:=wdTableFormatContemporary, _
ApplyBorders:=true, ApplyShading:=true, ApplyFont:=true, ApplyColor:=true _
, ApplyHeadingRows:=true, ApplyLastRow:=false, ApplyFirstColumn:=true, _
ApplyLastColumn:=false, AutoFit:=true

'send some text to the table