Automation error. The object invoked has disconnected

G

GrannyM

I've been doing a lot of research on the above error, but haven't found
anything to help with my problem. I have a macro that worked great between
Excel and Word 97, but since we switched to 2003, I keep getting the above
error. The macro is written in excel, but opens a file in Word, and searches
for specific information to pull into an excel spreadsheet. When the macro
executes the "Find" command, it will give the above error and close out
excel. Here is the section of my code causing the problem. If I rem out the
2 "find" lines, (which of course defeats the purpose of the macro!)
everything else runs without problem.

Public myWord As Word.Application

Set myWord = GetObject(, "Word.application")
myWord.Visible = True

myWord.Documents.Open FileName:=ABFileName$, ConfirmConversions:=False,
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", Format:=wdOpenFormatAuto

myWord.ActiveDocument.SaveAs FileName:="i:\rec.txt", FileFormat:=wdFormatText

myWord.Selection.Find.Execute FindText:=O7C$
myWord.Selection.MoveDown Unit:=wdLine, Count:=3
myWord.Selection.HomeKey Unit:=wdLine
myWord.Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
myWord.Selection.Delete

myWord.Selection.Find.Execute FindText:="PlanRefNu"
myWord.Selection.HomeKey Unit:=wdLine
myWord.Selection.EndKey Unit:=wdStory, Extend:=wdExtend
myWord.Selection.Delete

myWord.Selection.HomeKey Unit:=wdLine
myWord.Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
myWord.Selection.Delete

myWord.ActiveDocument.Save
myWord.ActiveDocument.Close
Set myWord = Nothing

Thanks!
 
M

Mike

try this
Sub mywordtest()


Dim myWord As Word.Application

Set myWord = GetObject(, "Word.application")
myWord.Visible = True

myWord.Documents.Open Filename:=ABFileName$, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto

myWord.ActiveDocument.SaveAs Filename:="i:\rec.txt", FileFormat:=wdFormatText

myWord.Selection.Find.Execute FindText:=O7C$
myWord.Selection.MoveDown Unit:=wdLine, Count:=3
myWord.Selection.HomeKey Unit:=wdLine
myWord.Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
myWord.Selection.Delete

myWord.Selection.Find.Execute FindText:="PlanRefNu"
myWord.Selection.HomeKey Unit:=wdLine
myWord.Selection.EndKey Unit:=wdStory, Extend:=wdExtend
myWord.Selection.Delete

myWord.Selection.HomeKey Unit:=wdLine
myWord.Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
myWord.Selection.Delete

myWord.ActiveDocument.Save
myWord.ActiveDocument.Close
Set myWord = Nothing
End Sub
 
G

GrannyM

Tried your code and now I get a different error. I moved this piece of code
to it's own sub, Dim'd myWord instead of using Public and called the sub from
the rest of the code. Instead of the above error, I now get "Method
'Execute' of object 'Find' failed." So, I did a search for the value of the
variable O7C$ on the Word document to make sure it worked in Word, and it
did. Then I replaced the variable in the code with the value of the
variable, and I still received the failed message above. (At least it's
something different then before!)
 
M

Mike

one other thing that i don't see is the path to the word document like
C:\MyPath
Set myWord = GetObject("C:\MyPath")
Set myWord = GetObject("C:\MyPath, Word.application")
 
G

GrannyM

I'm sorry, I'm confused. I'm setting myWord to Word.Application. Then I'm
opening my document. The path is in the variable ABFileName$. Are you
saying I need to set the path to the document after I open the document? I
tried to insert:
Set myWord = GetObject(ABFileName$, "Word.Application"), but I get "Filename
or Classname not found during Automation Operation. I tried inserting it
both before and after I actually opened the file and get the same error both
places.

I think it's finding the document, because if I rem out the lines with the
Find command, everything else works. I'm wondering if it has something to do
with my references or their order. I have the following references set:

Visual Basic for Applications
Microsoft Excel 11.0 Object Library
OLE Automation
Microsoft Office 11.0 Object Library
Microsoft DAO 2.5 Object Library
Microsoft Forms 2.0 Object Library
Microsoft Visual Basic for Applications Extensibility 5.3
Microsoft Word 11.0 Object Library
Microsoft ActiveX Data Objects 2.8 Library
 
M

Mike

The path needs to be like GetObject("C:\word.doc", "Word.Application"),
not like GetObject(ABFileName$, "Word.Application"),
If you goto properties of the word document it will give you the path
 
G

GrannyM

I used a variable because the document name will depend on the date the user
needs. These are daily reports. But just to try to get this to work, I
changed it to:

Set myWord = GetObject("m:\home\daily\autobal\history\040207.rtf",
"Word.Application")

I copied the path and filename and inserted them into the Word open dialog
box and it opened the file, so I know the path is correct. But I still get
the "Filename or Classname not found during Automation Operation." I'm
putting this immediately after opening the file. I also tried to put it
before opening the file, but that didn't work either.
 
M

Mike

Try this remove the Word.Application
Set myWord = GetObject("m:\home\daily\autobal\history\040207.rtf")
 
G

GrannyM

Let me start out with saying I really appreciate all your time helping me
with this.

When I changed to the below, since myWord is dim'd as Word.Application, I
rec'd a type mismatch error. So, I dim'd myDoc as Word.Document, replaced
"myWord on your set command to myDoc, and that piece worked. However, I'm
back to "Method
'Execute' of object 'Find' failed." on myWord.Selection.Find.Execute
FindText:=O7C$. I tried replacing that line with
myDoc.Application.Selection.Find.Execute FindText:=O7C$ but rec'd the same
error. I tried replacing the O7C$ variable with the variable value. I also
went to the actual document and did a search for the value, and it did find
it in the document. However, my code still does not.
 
M

Mike

I found this code on the web see if you can do something with it
Sub OpenWordDoc()


Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objBookmark As Word.Bookmark


Set objWord = New Word.Application
objWord.Visible = True


Set objDoc = objWord.Documents.Open(Filename:="c:\docname.doc")


If objDoc.Bookmarks.Exists("BookmarkName") Then
Set objBookmark = objDoc.Bookmarks("BookmarkName")
End If


Set objBookmark = Nothing


objDoc.Close SaveChanges:=wdDoNotSaveChanges
Set objDoc = Nothing


objWord.Quit
Set objWord = Nothing


End Sub
 
G

GrannyM

That didn't seem to do anything different then what we had worked with
before. However, I did some more searching on the "Method 'Execute' of
object 'Find' Failed and I found the answer. I would have not been able to
solve this problem without your help of getting past the original error, so
again, thank you so much for sticking in there with me.

It's listed under Bug:Automation client recieves error or crashes calling
Word's Find object on support.microsoft.com. The resolution said to resolve
the problem, either use late binding, or reregister the Word type library on
the system. I basically went back to my original code and dim'd myWord as
object instead of as Word.Application, then set myword =
getObject(,Word.Application).

Thanks again
 

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