GetObject Error 13 Type mismatch

S

samaclau

Until recently the following code has worked fine.
Dim objWord As Word.Document

Set objWord = GetObject(conDocDirectory & "Labels.doc", "Word.Document")

objWord.Application.Visible = True

objWord.MailMerge.Execute

Now the GetObject call returns a run time error 13 - Type mismatch.
I am running Office 2003 and XP SP3.
To fix this I have already tried the following:
1. Checked the References - none are missing.
2. Unchecked and re-checked the references - still none are missing
3. Unregistered and re-registered the vbe6.dll - success at each step but
problem still persists.
4. Copied older version of vbe6ext.olb and repeated step 3 - same results.

I hope that this is not a red herring but, about the time that this started,
one of my users installed a trial version of Excel 2007. It has been removed
and I still have the same problem. Could this have messed up the GetObject
definition?
 
S

Stuart McCall

samaclau said:
Until recently the following code has worked fine.
Dim objWord As Word.Document

Set objWord = GetObject(conDocDirectory & "Labels.doc",
"Word.Document")

objWord.Application.Visible = True

objWord.MailMerge.Execute

Now the GetObject call returns a run time error 13 - Type mismatch.
I am running Office 2003 and XP SP3.
To fix this I have already tried the following:
1. Checked the References - none are missing.
2. Unchecked and re-checked the references - still none are missing
3. Unregistered and re-registered the vbe6.dll - success at each step but
problem still persists.
4. Copied older version of vbe6ext.olb and repeated step 3 - same results.

I hope that this is not a red herring but, about the time that this
started,
one of my users installed a trial version of Excel 2007. It has been
removed
and I still have the same problem. Could this have messed up the
GetObject
definition?

Does your GetObject declaration match this one?

Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As
Long, ByVal nCount As Long, lpObject As Any) As Long

(one line of course)
 
S

samaclau

I never defined GetObject. I believe that it is defined in the VBA library as:
Function GetObject([PathName], [Class])
 
S

Stuart McCall

samaclau said:
I never defined GetObject. I believe that it is defined in the VBA library
as:
Function GetObject([PathName], [Class])


Stuart McCall said:
Does your GetObject declaration match this one?

Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject
As
Long, ByVal nCount As Long, lpObject As Any) As Long

(one line of course)

You're right, it is in the VBA library. I forgot. A couple of things to try:

Make sure that the value of conDocDirectory ends with a backslash.

Try removing the "Word.Document" argument. It's optional.
 
S

samaclau

Thank you for your reply. Unfortunately I am not at the computer that is
giving me a problem, so I can't test if removing the "Word.Document" argument
solves the problem. The code has been working fine as is for a year. It
also works fine on my home machine as well. All current Microsoft updates
have been applied to both computers. It seems to me that the GetObject
definition has changed somewhere, and what I am trying to find out is how to
change it back to the way it was. I don't know if that means that I need to
point to a different library, or the registry needs a change, or something
else. I am referencing all the same libraries and in the same order on both
computers, however one works and one doesn't. I really appreciate your help
in this matter.
 
S

Stuart McCall

samaclau said:
Thank you for your reply. Unfortunately I am not at the computer that is
giving me a problem, so I can't test if removing the "Word.Document"
argument
solves the problem. The code has been working fine as is for a year. It
also works fine on my home machine as well. All current Microsoft updates
have been applied to both computers. It seems to me that the GetObject
definition has changed somewhere, and what I am trying to find out is how
to
change it back to the way it was. I don't know if that means that I need
to
point to a different library, or the registry needs a change, or something
else. I am referencing all the same libraries and in the same order on
both
computers, however one works and one doesn't. I really appreciate your
help
in this matter.

Well I'm sort of clutching at straws here, but I think the installation of
the Excel trial is not a red herring. Something to try: on the affected
computer, change "Word.Document" to "Word.Document.12". If that works, then
definitely remove "Word.Document" altogether. What this probably means (if
I'm right) is that the Excel install has changed the class names of all
Office apps. This is a bit naughty, but then we are talking Microsoft...
 
S

samaclau

On my home computer (which still works fine), I changed it to
"Word.Document.12", and the code still executes fine. Therefore if the
install of Excel 2007 changed the class names on the office computer,
shouldn't the change to 12 on my home computer have failed?
 
S

Stuart McCall

samaclau said:
On my home computer (which still works fine), I changed it to
"Word.Document.12", and the code still executes fine. Therefore if the
install of Excel 2007 changed the class names on the office computer,
shouldn't the change to 12 on my home computer have failed?
<snip>

Probably. Uh, I'm afraid I'm out of ideas. I don't have much experience in
this area because I've never included the class name in a GetObject Call
(never needed to).
 
S

Stuart McCall

samaclau said:
Then how do you do a mail/merge into word from access?

Apologies. It's not the class name but the document name that I've always
omitted (I haven't done this stuff in a while). Here's a snippet from a
document manager app. There's some app-specific code and calls into my
library, but if you just look at the Word stuff you should get the gist.

Public Function GenerateDoc() As Boolean
Dim wd As Word.Application
Dim MainDoc As Word.Document
Dim OutDoc As Word.Document
'
On Error GoTo Handler
'
Set wd = AppObject("Word.Application")

With wd
.ScreenUpdating = False
If Len(Dir$(Me![Document Path])) Then
If Not AskOverwrite() Then Exit Function
m_NewId = GetArchiveNumber()
Else
If Not MakePath(NoSlash(PathPart(Me![Document Path]))) Then Exit
Function
m_NewId = AddToArchive()
End If
Me![Document Archive Number] = m_NewId
Me.Dirty = False
'
qn$ = SetTargetQuery()
Set MainDoc = .Documents.Open(CStr(Me.TemplateFile.tag), , , False)
With MainDoc.MailMerge
.SuppressBlankLines = True
.Destination = wdSendToNewDocument
.Execute
End With
MainDoc.Close wdDoNotSaveChanges
Set OutDoc = .ActiveDocument
'
SetDocProperties OutDoc
OutDoc.SaveAs CStr(Me![Document Path])
'
.ScreenUpdating = True
.Visible = True
.Activate
End With
ExitPoint:
Set MainDoc = Nothing
Set OutDoc = Nothing
Set wd = Nothing
GenerateDoc = True
Exit Function
Handler:
ErrBox Err.Description & " - GenerateDoc", APP_TITLE
Resume ExitPoint
End Function

Public Function AppObject(ClassName As String, Optional AppStarted As
Boolean) As Object

Dim obj As Object
'
On Error GoTo AppObject_Err
Set obj = GetObject(, ClassName)
obj.Activate
AppObject_Err:
If Err Then
Err.Clear
Set obj = CreateObject(ClassName)
AppStarted = True
End If
Set AppObject = obj
Set obj = Nothing

End Function
 

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