Results 1 to 7 of 7

Thread: Error when closing Word application after copy to clipboard

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    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

  2. #2
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    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:
    1. CreateObject("Word.Application.9")

    changed to:
    VB Code:
    1. CreateObject("Word.Application")
    if you fail to plan, you plan to fail

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    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.
    Last edited by perronnberg; Apr 8th, 2005 at 02:03 AM.

  4. #4
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    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

  6. #6
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    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?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    4

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width