Using Excel to close the correct word document

mark hansen

Well-known Member
Joined
Mar 6, 2006
Messages
534
Office Version
  1. 2016
Platform
  1. Windows
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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Mark

Are you using code to open the Word document that you want to work with and then subsequently close?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
Thanks Norei!!! This does the trick... I thought setting it nothing, would close it. I appreciate the help!

Mark
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top