Using Excel to close the correct word document

mark hansen

Active Member
Joined
Mar 6, 2006
Messages
495
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
 

Some videos you may like

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,559
Office Version
365
Platform
Windows
Mark

Are you using code to open the Word document that you want to work with and then subsequently close?
 

mark hansen

Active Member
Joined
Mar 6, 2006
Messages
495
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,559
Office Version
365
Platform
Windows
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.
 

mark hansen

Active Member
Joined
Mar 6, 2006
Messages
495
Thanks Norei!!! This does the trick... I thought setting it nothing, would close it. I appreciate the help!

Mark
 

Watch MrExcel Video

Forum statistics

Threads
1,099,509
Messages
5,469,038
Members
406,629
Latest member
Alan365

This Week's Hot Topics

Top