Results 1 to 5 of 5

Thread: Using Excel to close the correct word document

  1. #1
    Board Regular
    Join Date
    Mar 2006
    Posts
    456
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Using Excel to close the correct word document

    I have been using Excel to create a report, that is then pasted into another application. Because of the restrictions of the other application, I need to copy the information from Excel, Paste it into Word, and then copy from Word to paste into the application. (it has to do with merged cells in Excel being unmerged when pasted directly into our application).

    So no problem... the Excel code opens word in the back ground, does the pasting, then selects everything in word, copies it, and closes word.

    I discovered I'm leaving zombie processes of word hanging around because I only use

    Set wdapp = New Word.Application (to Start word)

    Set wdapp = Nothing (to close word)

    I've done some searches and discovered I need to put

    wdapp.Quit SaveChanges:=wdDoNotSaveChanges

    before I use Set wdapp = Nothing line to stop word.

    During my research I came across something that says I need to close the document object before I close the application object.

    if I put ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

    it closes down the wrong document. (we run with 2-3 word document open most of the time.)

    if I truly need to close the word document before the application, how can I select which word document? I can get the name of the newly created word document in a variable and use that to close just that word document?

    Mark

  2. #2
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    75,184
    Post Thanks / Like
    Mentioned
    62 Post(s)
    Tagged
    6 Thread(s)

    Default Re: Using Excel to close the correct word document

    Mark

    Are you using code to open the Word document that you want to work with and then subsequently close?
    If posting code please use code tags.

  3. #3
    Board Regular
    Join Date
    Mar 2006
    Posts
    456
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Using Excel to close the correct word document

    Yes, then same process opens word, and then a new document At the end of the same sub process, it closes it all (well that instance of word). I thought about just closing the document that had "document" as the name, but they have other temporary documents open they are building throughout the visit to document, that they won't be saving.

  4. #4
    Board Regular Norie's Avatar
    Join Date
    Apr 2004
    Location
    Stirling, Scotland
    Posts
    75,184
    Post Thanks / Like
    Mentioned
    62 Post(s)
    Tagged
    6 Thread(s)

    Default Re: Using Excel to close the correct word document

    Mark

    Create a reference to the document you are creating/opening and use that reference to close the document once you are finished with it, something like this.
    Code:
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document]
    
        Set wdApp = New Word.Application (to Start word)
    
        Set wdDoc = wdApp.Documents.Add()
    
        ' do stuff with document
    
        ' close document
    
        wdDoc.Close
    
        Set wdDoc = Nothing
    
        wdApp.Quit
    
        Set wdApp = Nothing
    By the way, setting wdApp to Nothing does not close Word.
    If posting code please use code tags.

  5. #5
    Board Regular
    Join Date
    Mar 2006
    Posts
    456
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Using Excel to close the correct word document

    Thanks Norei!!! This does the trick... I thought setting it nothing, would close it. I appreciate the help!

    Mark

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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