Error when closing Word application after copy to clipboard
I'm running the following VBA macro in Excel.
The macro creates a Word application which opens an existing document, copies its contents to the clipboard. It then opens a new word application and pastes the clipboard contents into a new document.
So far so good. But when I quit the first application I get a Program Error which says "Winword.exe has generated errors and will be closed, etc..."
What am I doing wrong?
Sub myFunc()
currDir = Worksheets.Application.ThisWorkbook.Path
Set appWD = CreateObject("Word.Application.9")
appWD.Visible = True
Set tempDoc = appWD.Documents.Open(currDir & "\" & "V.DOC")
appWD.ActiveDocument.Content.Copy
Set appWD_NewDoc = CreateObject("Word.Application.9")
appWD_NewDoc.Visible = True
appWD_NewDoc.Documents.Add
appWD_NewDoc.ActiveDocument.Content.Paste
tempDoc.Close (wdDoNotSaveChanges)
Set tempDoc = Nothing
appWD.Quit 'THIS IS WHERE THE ERROR OCCURS!!
Set appWD = Nothing
End Sub
Re: Error when closing Word application after copy to clipboard
HI,
ive used your code and it worked fine. Win NT, Excel 2002, word 2002
i did have to change it slightly:
VB Code:
CreateObject("Word.Application.9")
changed to:
VB Code:
CreateObject("Word.Application")
Re: Error when closing Word application after copy to clipboard
Thanks for your answer !
I forgot to mention that I am using Excel 2000 and that my application was run on Win2000 Professional.
It seems to work fine on XP though.
Still a mystery why the error occurs in 2K, I've tried it on 2 different machines.
Re: Error when closing Word application after copy to clipboard
Quote:
Originally Posted by perronnberg
I'm running the following VBA macro in Excel.
Code:
Sub myFunc()
currDir = Worksheets.Application.ThisWorkbook.Path
Set appWD = CreateObject("Word.Application.9")
appWD.Visible = True
Set tempDoc = appWD.Documents.Open(currDir & "\" & "V.DOC")
appWD.ActiveDocument.Content.Copy
Set appWD_NewDoc = CreateObject("Word.Application")
appWD_NewDoc.Visible = True
appWD_NewDoc.Documents.Add
'---- pastes v.doc content into the new doc
appWD_NewDoc.ActiveDocument.Content.Paste
'---- clears up the temp doc (v.doc)
tempDoc.Close (wdDoNotSaveChanges)
Set tempDoc = Nothing
'---- You haven't saved or closed the copied doc?
appWD.Quit 'THIS IS WHERE THE ERROR OCCURS!!
Set appWD = Nothing
End Sub
Perhaps the error is that you are closing the app but not having saved/closed the new doc?
Re: Error when closing Word application after copy to clipboard
Thanx for the suggestion, but...
I tried saving and closing the new doc without getting rid of the error.
Code:
appWD_NewDoc.ActiveDocument.Save
appWD_NewDoc.ActiveDocument.Close
appWD_NewDoc.Quit
Set appWD_NewDoc = Nothing
appWD_Template.Quit
Set appWD_Template = Nothing
Re: Error when closing Word application after copy to clipboard
Just ran that code.. stepped through - no problem ... from excel opening word and a doc. Perhaps it is the content of the v.doc?
Try with only a line or two of test data?
Re: Error when closing Word application after copy to clipboard
Thanx again !
You seemed to have hit the spot, There's something in my document that causes the error!!
I'll just have to figure out what part. Besides plain text the doc has tables and bookmarks.