tags:

views:

204

answers:

2
With ActiveDocument.MailMerge
.MainDocumentType = wdCatalog
.OpenDataSource Name:=excelfile, _
Connection:="Entire spreadsheet", SubType:=8, ReadOnly:=True
''# Range = Selection.Range
.Destination = wdSendToNewDocument
.Execute
End With ''# Activedocument
DDETerminateAll

Why does this code open "excelfile" twice to excel, one of these is readonly. How can I close these Excel files from Word?

A: 

From VBA:

Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim errCode As Integer

Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace

Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='EXCEL.EXE'") 'Find the process to terminate

For Each objProcess In objList

    errCode = objProcess.Terminate 'Terminates a process and all of its threads.

Next

Would kill all instances of Excel.

Mark
I want to kill a chosen one, just DDE opened one, that has a specific xls file open, how to do that?
Tom
A: 

Workbooks can be closed with this VBA code

Workbooks("BOOK1.XLS").Close

You can add this to the end, so that no prompts to save are displayed if the file has been updated and not saved.

 SaveChanges:=False

Excel can be closed via VBA with if called from within Excel.

Application.Quit

If called from outside Excel, you will need to set a reference to Excel and then close it.

Set appExcel = GetObject(, "Excel.Application")
appExcel.Quit

You need to ensure that all Workbooks are closed or saved, otherwise Excel will prompt the user to save

Robert Mearns
thanks, but how to run workbooks.close or excel quit from Word macro?? Word opens the excel via DDE and leaves it open
Tom
With DDE, once the merge document is closed, Excel will automatically be closed as well. The output of the merge does not need to be closed for this to occur.
Robert Mearns