CreateObject("Word.Application") fails though Word session starts

T

tyla

We have an Excel application which fails for some users, sometimes,
with a 1004 at the line:
Set gobjWordApp = CreateObject("Word.Application")
where gobjWordApp has been defined as an Object.
What's interesting is that the Word session has been started, as per
Task Manager. It isn't visible to the user at that point (as expected)
and the gobjWordApp object is undefined.. So while Word is there, it
looks like the reference/pointer back to VBA simply doesn't occur.
Additional hints: Changing the line to a simple
"CreateObject("Word.Application") works fine -- although it's pretty
useless for the application since there's no addressable object for
VBA to interact with.
Re-installing Office doesn't change anything.
The typical environment is Windows XP/SP2 and Office 2003/SP2 or
SP3.
There doesn't seem to be a particular pattern in terms of who this
works for and who it fails for, although we're getting suspicious of
the new Vista-compatible versions of HP printer drivers.
Has anyone had experience with this kind of problem or, even
better, know of a fix?

/ Tyla /
 
D

Dave Peterson

I don't know. But maybe a few more questions/answers will help someone else
come up with an idea.

How did you declare gobjWordApp?

Did you declare it as an object (using late binding) or did you add a reference
to that workbook's project--and maybe the reference is missing???

What is the error description? 1004 isn't really too helpful.
 
M

Marchand

Dave,



I don't know. But maybe a few more questions/answers will help someone else
come up with an idea.

How did you declare gobjWordApp?

Did you declare it as an object (using late binding) or did you add a reference
to that workbook's project--and maybe the reference is missing???

What is the error description? 1004 isn't really too helpful.
 
M

Marchand

Thanks for your respones, and your questions.
- I absolutely agree that 1004 isn't a useful error code -- but it's
the only one Excel's providing.
- The 'objWordApp' object is declared with a "Public Const gobjWordApp
as Object" in a separate modeul in the application. Thus it does use
late binding.
- The idea of a missing reference is interesting. The "Tools /
References" within VBE look normal. And I can easily create the Word
session, just not (always) get it assigned to an Excel variable. Is
there something I'm missing here, that I'm not checking but should?

/ T /
 
D

Dave Peterson

I still don't have a guess.

But when you see that 1004 error, what's the error description that you see?
 
P

papou

Hi
Why do you declare your Word Object as a constant?
I do not see any reason so far with the info you provided.
In addition there is obviously a mistake in this line:
Public Const gobjWordApp as Object

Can you just try and change to:
Public objWordApp As Object

And see if it works?

HTH
Cordially
Pascal

Marchand said:
Thanks for your respones, and your questions.
- I absolutely agree that 1004 isn't a useful error code -- but it's
the only one Excel's providing.
- The 'objWordApp' object is declared with a "Public Const gobjWordApp
as Object" in a separate modeul in the application. Thus it does use
late binding.
- The idea of a missing reference is interesting. The "Tools /
References" within VBE look normal. And I can easily create the Word
session, just not (always) get it assigned to an Excel variable. Is
there something I'm missing here, that I'm not checking but should?

/ T /
 
T

tyla

My apoligies for some of the misingofmation in this thread from a well-
meaning colleague.
The relevant code surrounding the problem is
Dim gobjWordAPp as Object
'...
set gobjWordAPp = CreateObject("Word.Application")

THe 1004 error message is the very generic
"Run time error 1004" with no further details

JP, thanks for the link. I've never used the second parameter of
CreateObject() but will try it and see, and get back to this thread
with the results.

/ Tyla /
 
D

Dave Peterson

But you're positive that MSWord is started???

I'd use:

on error resume next
set gobjWordAPp = CreateObject("Word.Application")
if err.number <> 0 then
msgbox "something bad happened"
err.clear
exit sub '????
end if
on error goto 0

But that just masks the error--it doesn't explain why MSWord is really started.
I still don't have a guess for that.
 
J

JP

Not sure if this applies to your application, but if the code still
won't work, you can try early binding. You would just add code like
this to the ThisWorkbook module to check if there is a reference to
the Word object library when the workbook opens. If not, the code
creates one.

(this is air code from memory, please test first)

Dim bWasFound As Boolean
Private Sub Workbook_Open()
For i = 1 To ThisWorkbook.VBProject.References.Count
If ThisWorkbook.VBProject.References.Item(i).Name = "Word"
Then
' Not sure if the name of the library is "Word"
bWasFound = True
Exit Sub
End If
Next i

If bWasFound = False Then
ThisWorkbook.VBProject.References.AddFromFile _
"C:\Program Files\Microsoft Office\Office10\MSWORD.OLB"
End If

End Sub

Then you could use this code (adapted from http://www.dicks-clicks.com/excel/olBinding.htm)
to set a reference to the Word Application object.

Dim wdApp As Word.Application

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")

If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.application")
End If

On Error GoTo 0

If wdApp Is Nothing then
msgbox "Cannot start Word", vbCritical
Exit Sub
End if


In the Workbook_BeforeClose you can remove the reference if you wish.


HTH,
JP
 
T

tyla

Yup -- Word definitely starts. There's WINWORD.exe process showing as
running in Task Manager after CreateObject() executes.
I'll play with the 'on error' code see if I can tease out any more
details.
 
J

Jim Cone

For what it's worth...
Each user must have a copy of Word installed.
Also, as far as I can see, you haven't specified the office versions involved.
If it is both 2007 and prior versions, then you might ask in a Word newsgroup,
if Word 2007 is compatible with prior versions when using automation?
 
T

tyla

JP,

I've been reluctant to go for early binding, since it makes
assumptions about the users machines I can't always depend on (this is
a commercial app). The ability to programmatcially reference the
VBEProject is virtually nil due to security settins. Users can also
have various version of Word, making a hard-wired "AddFromFile" pretty
dicey. Is there a way to assert a reference baed on GUID rather than
file name?

/ T /
 
T

tyla

Jim, Good points, all of them. We support Word 2000 and above and
basically don't care beyond that. I've been reluctant to cross-post
this issue, but that's definitely a possibility.

/Tyla /
 
J

JP

Did you test out the code in that link I sent you? Apparently, some AV
programs have script stoppers that might affect the code.

I guess at this point all I can think of is that there is something
about your code environment causing this issue, but I can't identify
it because I'm not there. You might inadvertently be leaving out some
lines of code, environmental variables, or other parameters that seem
inocuous but would help identify the cause.

Sorry I couldn't be more help here.

--JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top