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

Problem with CreateObject to Launch Word 1

Status
Not open for further replies.

GeekGirlau

Programmer
Jul 29, 2001
216
AU
AAAARRRRRGGGHHHHH!!!!!!!!!

I'm having constant fun and games using CreateObject and GetObject. In the following sample, if Word is not running I get error 429, but Word is not being launched.

As soon as I reach the ".Documents.Add" I get error 91 - object not set. I can use Shell to launch Word, however GetObject still won't capture the running instance of Word!!

Any ideas for a bullet-proof activation method before I go insane?? X-)

Dim objWord as Word.Application
dim x

On Error Resume Next
Set objWord = CreateObject("Word.Application")

If Err.Number = 429 Then
Set objWord = New Word.Application
End If

On Error GoTo AutoError

With objWord
.Documents.Open FileName:=strExportFile

<more code here>
.Quit
End With

AutoExit:
On Error Resume Next
Set objWord = Nothing
Exit Sub


AutoError:
Select Case Err.Number
Case 91
Err.Clear
x = Shell(&quot;winword.exe&quot;, vbNormalFocus)
AppActivate x
Set objWord = GetObject(, &quot;Word.Application&quot;)
Resume

Case Else
MsgBox Err.Number & &quot;: &quot; & Err.Description
resume AutoExit
End Select
 
This is what I'm using at the moment:

Dim WordObj As Word.Application

Set WordObj = GetObject(, &quot;word.application&quot;)
If Err.Number <> 0 Then
Set WordObj = CreateObject(&quot;word.application&quot;)
End If

WordObj.Visible = False

WordObj.Documents.Add .......

This works for me

Do you have the correct variables defined?

Perhaps this may have helped.. Lloyd Gozzett
Process Developer
 
How strange...

I have just encountered the same 429 message when using my Db. Nothing has changed since it was last working???

I logged in and out and its now fine.

Is this co-incidence? Lloyd Gozzett
Process Developer
 
This seems to be an intermittant error - I have other databases where the code works without any difficulty, and have also used the same method in VB6, but occasionally the problem strikes again.

So far I've been warning uses that if they receive a particular error message, open Word manually and run the process again, but I'd like to have a more foolproof method if possible!
 
The actual code has a flag that is set to indicate whether Word had to be launched - if Word was not already running the code quits Word, otherwise it leaves it running and just sets the object to nothing.

The problems I am having are as follows:

- CreateObject(&quot;Word.Application&quot;) does not always start Word.
- New Word.Application does not always start Word.
- GetObject(,&quot;Word.Application&quot;) does not always pick up a running instance of Word.
- Even if I use Shell to start Word, followed by AppActivate, GetObject(,&quot;Word.Application&quot;) does not pick up the running instance of Word.

The variable I'm using is dimensioned as Word.Application - I have seen some reference somewhere to setting it as a generic Object instead - does anyone have any positive experiences with this?
 
Here's a couple of ideas.

1:
2: You may want ot read this. Once you get the instance of word to start correctl, you'll mosy likely get more errors. this has to do with fully qualified references.



SYMPTOMS
While running code that uses Automation to control Microsoft Word 97, Word 2000, or Word 2002, you may receive one of the following error messages:

Run-time error '-2147023174' (800706ba)
Automation error
-or-
Run-time error '462': The remote server machine does not exist or is unavailable



CAUSE
Visual Basic has established a reference to Word due to a line of code that calls a Word object, method, or property without qualifying it with a Word object variable. Visual Basic does not release this reference until you end the program. This errant reference interferes with automation code when the code is run more than once.



RESOLUTION
Modify the code so that each call to a Word object, method, or property is qualified with the appropriate object variable.



STATUS
This behavior is by design.



MORE INFORMATION
To automate Microsoft Word, you establish an object variable that usually refers to the Word Application or Document object. Other object variables can then be set to refer to a Selection, a Range, or other objects in the Microsoft Word object model. When you write code to use a Word object, method, or property, you should always precede the call with the appropriate object variable. If you do not, Visual Basic uses a hidden global variable reference which it sets to the currently running instance. If Word is shutdown, or if the declared object variable is released, the hidden global variable will now reference an invalid (i.e., destroyed) object. When running the automation code again, calls to this hidden object variable will fail with the aforementioned error.

The following steps illustrate how to reproduce this problem, and how to correct it.

Steps to Reproduce Behavior
Start a new Standard EXE project in Visual Basic. Form1 is created by default.


Click References from the Project menu and check &quot;Microsoft Word 8.0 Object Library&quot; for Word 97. For Word 2000, check &quot;Microsoft Word 9.0 Object Library.&quot; For Word 2002, check &quot;Microsoft Word 10 Object Library.&quot;


Place a CommandButton on Form1.


Copy the following code to the Code Window of Form1:


Option Explicit

Private Sub Command1_Click()
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRange as Word.Range

Set oWord = CreateObject(&quot;Word.Application&quot;)
With oWord
.Visible = True
.Activate
.WindowState = wdWindowStateNormal
End With

Set oDoc = oWord.Documents.Add
MsgBox &quot;Document open&quot;, vbMsgBoxSetForeground
With oDoc
.PageSetup.LeftMargin = InchesToPoints(1.25)
End With

' This example inserts text at the end of section one.
Set oRange = ActiveDocument.Sections(1).Range
With oRange
.MoveEnd Unit:=wdCharacter, Count:= -1
.Collapse Direction:=wdCollapseEnd
.InsertParagraphAfter
.InsertAfter &quot;End of section.&quot;
End With

With oDoc
.Saved = True
End With

Set oRange = Nothing
Set oDoc = Nothing
oWord.Quit
Set oWord = Nothing
End Sub
On the Run menu, click Start or press the F5 key to start the program.


Click the CommandButton. No error occurs. However, a reference to Word 97, 2000, or 2002 has been created and has not been released.


Click the CommandButton again and note that you receive the error previously described.

NOTE: The error occurs because the code refers to the InchesToPoints Method without preceding the call with the oWord object variable.


Stop the project and change the following line:


.PageSetup.LeftMargin = InchesToPoints(1.25)
-to-
.PageSetup.LeftMargin = oWord.InchesToPoints(1.25)
Run the program again. Then, click the CommandButton. No error occurs.


Click the CommandButton again and note that you receive the error.

NOTE: The error occurs because the code refers to the ActiveDocument Section one's Range object without preceding the call with the oWord object variable.


Stop the project and change the following line:


Set oRange = ActiveDocument.Sections(1).Range
-to-
Set oRange = oWord.ActiveDocument.Sections(1).Range
Run the program again. Note that you can run the code multiple times without error.


When building a Visual Basic project automating Word, if your project has a reference to the Microsoft Word 8.0, 9.0, or 10.0 Object Library, sample code for the objects, methods, and properties of the Word Object Model is available from the Word Help file. When the cursor is over a key word in your code, you will see any applicable Help text by pressing the F1 key.

The sample code in the Help topic will be the Microsoft Word Visual Basic for Applications code. It will not show the object references that your Visual Basic code requires. You will need to add the qualifiers as appropriate.



REFERENCES
For additional information, please see the following articles in the Microsoft Knowledge Base:

Q178510 PRB: Excel Automation Method of Object '_Global'Failed
Q167223 Microsoft Office 97 Automation Help File Available
For additional information about the Automation of Office applications, click the article number below to view the article in the Microsoft Knowledge Base:
Q222101 HOWTO: Find and Use Office Object Model Documentation
Or visit the following Microsoft Web site:

Tyrone Lumley
augerinn@gte.net
 
Databaseguy,

You have saved me giong insane to!!! This is by far and away the most well explained and useful tip i have seen to date. I cant give you enough stars!

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top