VB to MS Word Problem

VB to MS Word Problem

Post by Gran » Sat, 11 Apr 1998 04:00:00

My program needs to send some information to an existing word document.

I can get it to work, but encounter a problem which I can't seem to fix.

I am using VB5 and MS Word97

I use the following code to open the word document and send the

information across to a table in the word document.

(I have the Word 8.0 Object Library selected in the Project References.)

Dim wrdDoc as Word.Document

Dim MyTable

Set wrdDoc = GetObject(sMyWordDocument)

wrdDoc.Parent.Visible = True

Documents.Open filename:=(sMyWordDocument)

Set MyTable = ActiveDocument.Tables(1)

        MyTable.Cell(1,1).Range.InsertAfter sSomeString

        'And so on until all the info is in

Set wrdDoc = Nothing

Set MyTable = Nothing

End Sub

This all works fine.  The VB app opens word, and the correct document is

displayed, and all the information is inserted.  If, however, I close

MS-Word (just closing the document, and leaving Word open has no

effect), then run the above procedure again from the same instance of

the VB app, Word opens again, but not the document, and I get the

following error:

Run-Time error '-2147023174 (800706ba)':

Automation error

and the code halts at the following line of code:

Documents.Open filename:=(sMyWordDocument)

I suspect that the way I am opening Word is the problem, but so far it's

the only way I can get Word to open, and the for the document to be

displayed.  Also the user needs to be able to edit the word document, so

I can't close it from the VB app with the Application-Quit method.

What am I doing wrong, and how can I fix it?

A Second question - does anybody know a good (preferably on-line)

resource for integrating VB with Office 97.  I need to do it quite

often, and so far all the VB documentation I have on the subject is VERY


Thank you in advance.



VB to MS Word Problem

Post by Damit Senanayak » Sat, 11 Apr 1998 04:00:00

Quote:>What am I doing wrong, and how can I fix it?

Well, obviously, closing Word in the middle of your app isn't going to give
good results...

You should use:

Dim objWord as Object
Set objWord=CreateObject("Word.Document")
objWord.Documents.Open filename:= sMyWordDocument
Set MyTable = ActiveDocument.Tables(1)

instead of

Quote:>Set wrdDoc = GetObject(sMyWordDocument)
>wrdDoc.Parent.Visible = True
>Documents.Open filename:=(sMyWordDocument)
>Set MyTable = ActiveDocument.Tables(1)

This is because GetObject only returns a reference to an object once
whereas CreateObject can create an object (and more than once).

Quote:>A Second question - does anybody know a good (preferably on-line)
>resource for integrating VB with Office 97.  I need to do it quite
>often, and so far all the VB documentation I have on the subject is VERY

There's a VBA reference on the Office 97 CD in the Valupack\Help  [I think]
folder. It covers using VBA5 from Office 97, but you could use the same code
in VB5.
My e-mail address may have 2 bugs in it; deBUG to reply.

Damit Senanayake

SBN Members' Free Home Pages: http://freesbn.dyn.ml.org/
Damit's Home; http://web.singnet.com.sg/~apesena/
ICQ UIN: 6930718
SiteBuilder Network Level 2 Member
MSDN Online Member

"It's one small step for man, one giant leap for mankind." -- Neil Armstrong

begin 666 Damit Senanayake.vcf



/-%H-"D5.1#I60T%21 T*


VB to MS Word Problem

Post by Gran » Sun, 12 Apr 1998 04:00:00

Thanks for your help I really appreciate it!

I got an error message with the code.  It said that the "filename" property
was not supported.

I played around with it for a while, and kinda merged your suggestion with
eve's (who also replied to this message).  This is what I got, and it also

Private Sub Command1_Click()
Dim wa As Word.Application
Dim objWord As Object
Dim MyTable As Table

Dim sMyWordDocument As String

sMyWordDocument = App.Path & "\word.doc"

Set objWord = CreateObject("Word.Document")
Set objWord = objWord.Application.Documents.Open(sMyWordDocument)
Set wa = objWord.Application

wa.Visible = True

Set MyTable = objWord.Application.ActiveDocument.Tables(1)

MyTable.Cell(1, 1).Range.InsertAfter "MMMMMMMMMM"

End Sub


1. Replacing A Word With Picture In MS-Word Using VB !

Dear Friends
Hope you all doing great.

I have this MS-Word file where I have to insert lots of images at many
points again and again.
So I was thinking that is there any way that I can insert these
pictures at required points using VB.
I mean that first while creating the MS-Word file I write down the
name of the pic and afterwords using a VB application I can replace
that "word" i.e. name of the pic with the actual image.

I hope you got what I want.

So if you have any idea how I can do this please let me know.



2. oe6, multiple users

3. Word 8 and OLE link problems of Word 6 / Word 7 docs

4. New Pricing For Aspose.Chart

5. Using MS-Word thesaurus from VB

6. QD3D Viewer and PowerPlant

7. VB Controlling/Disabling MS Word Menus

8. DOS Game too fast!! how to slow down?

9. Freetext serch from VB in MS Word Documents/PDF Documents?

10. How to show a print preview like MS Word in VB.

11. Delete and Cut fails when programming MS Word if Word not visible

12. Is necessary to have Word installed to use VB Word object?

13. How to add Icon to MS Excel and MS Word Toolbar through an app?