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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Microsoft Word VBA ActiveDocument error 1

Status
Not open for further replies.

kar202

Programmer
Jun 28, 2002
16
US
Basically, I have a program that is inserting images saved to the hard drive into Word, but first the program must open Word and change the margins. Here is the code relevent to my problems:

-------------------------------------------
Dim WordApp As Word.Application
Dim WordDoc As Word.document

Set WordApp = CreateObject(Class:="Word.Application") 'open microsoft word
With WordApp
.WindowState = wdWindowStateMinimize
.Visible = True

Set WordDoc = .Documents.Add 'create new word doc

'change page margin to accomodate 9 cards per page
With ActiveDocument.PageSetup
.topMargin = InchesToPoints(0.25)
.bottomMargin = InchesToPoints(0.5)
.leftMargin = InchesToPoints(0.25)
.rightMargin = InchesToPoints(0.25)
End With
-----------------------------------------------

The two problems I am experiencing:
1. The first time I run the program, everything works fine. Now, if I do not close down the current Word document (Document1) and run it again, the margins in the second document (Document2) never change. I did some tests and my program still considers Document1 the "ActiveDocument." I do not know how to correct this.

2. The second problem occurs when I close down Microsoft Word after running the program the first time. I run the program again, and at "ActiveDocument.PageSetup" I get Run-time error '462': The remote server machine does not exist or is unavailable. Once again, it's related to the ActiveDocument member, but a different problem entirely.

To sum up, I have to close down Word and restart the program before I can get it to work again. Any help is very much appreciated. Thanks.
 
Hi!

You're using unqualified references to Word objects and methods (ActiveDocument, InchesToPoints). When automating, this has a tendency of creating extra instances of the automated application in the memory (though I've seen it most with Excel), and the error you mention.

I think perhaps try either
[tt]With WordDoc.PageSetup ' or
With WordApp.ActiveDocument.PageSetup
' or just add the "dot" to your existing reference[/tt]

[tt].topMargin = WordApp.InchesToPoints(0.25)[/tt]

This link relates to Excel, but concerns the same issue, Excel automation fails second time code runs

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top