Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble working with word

Status
Not open for further replies.

Rob2k2

Technical User
Oct 14, 2002
36
0
0
GB
I am using the following code to open a word document and insert two pictures:-

Public WordApp As New Word.Application
Public TestDoc As Word.Document
Dim Sel As Word.Selection

Private Sub LPCBtn_Click()

Dim LogoRange As Word.Range

Set TestDoc = WordApp.Documents.Open("C:\Documents and Settings\Robert\Desktop\Print Single Letter\TestDocument.doc")
Set TestDoc = ActiveDocument

With TestDoc
Set LogoRange = ActiveDocument.Bookmarks("TopLogo").Range
Selection.InlineShapes.AddPicture FileName:="C:\Documents and Settings\Robert\Desktop\Print Single Letter\LPCLogo.tiff", LinkToFile:=False, SaveWithDocument:=True, Range:=LogoRange
Set LogoRange = ActiveDocument.Bookmarks("BottomLogo").Range
Selection.InlineShapes.AddPicture FileName:="C:\Documents and Settings\Robert\Desktop\Print Single Letter\LPCFooter.tiff", LinkToFile:=False, SaveWithDocument:=True, Range:=LogoRange
End With
WordApp.Visible = True
Set TestDoc = Nothing
Set WordApp = Nothing
End Sub

The problem is when i first click the button, everything works fine. I then close word and try and click the button again and I get an error on this line:-

Set TestDoc = ActiveDocument

"The remote server machine does not exist"

Any ideas?
 
I suspect it has something to do with the module-level declaration of the app variable:
Code:
Public WordApp As New Word.Application

When you set it to nothing, how do you reinstantiate it?
Code:
Set WordApp = Nothing

Also, when you open the document with:
Code:
Set TestDoc = WordApp.Documents.Open(...)

It's already assigned to the active document so:
Code:
Set TestDoc = WordApp.ActiveDocument
is redundant. VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top