MailMerge似乎打开Excel文件两次?

时间:2009-09-17 14:03:55

标签: excel ms-word

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

为什么这段代码打开“excelfile”两次excel,其中一个是readonly。如何从Word中关闭这些Excel文件?

2 个答案:

答案 0 :(得分:0)

来自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

会杀死所有Excel实例。

答案 1 :(得分:0)

可以使用此VBA代码关闭工作簿

Workbooks("BOOK1.XLS").Close

您可以将其添加到最后,这样如果文件已更新但未保存,则不会显示保存提示。

 SaveChanges:=False

如果在Excel中调用Excel,则可以通过VBA关闭Excel。

Application.Quit

如果从Excel外部调用,则需要设置对Excel的引用,然后将其关闭。

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

您需要确保关闭或保存所有工作簿,否则Excel将提示用户保存