Excel VBA退出Word文档

我有一个macros将一个Excel文档的选定单元格插入Word模板,复制整个Word文档,然后closures文档而不保存,以保留某些关键字。

但是,当它closuresWord文档时,会留下一个空白的Word窗口,并且没有活动的文档,并且每次macros运行时都会留下新的空白窗口。

Dim appWd As Word.Application Dim wdFind As Object Dim ClipEmpty As New MSForms.DataObject Dim ClipT As String Sub CopyDatatoWord() Dim docWD As Word.Document Dim sheet1 As Object Dim sheet2 As Object Dim saveCell1 As String Dim saveCell2 As String Dim saveCell3 As String Dim dir1 As String Dim dir2 As String date_example = Cells(Application.ActiveCell.Row, 3) Set appWd = CreateObject("Word.Application") appWd.Visible = True Set docWD = appWd.Documents.Open(ThisWorkbook.Path & "\template.docx") 'Select Sheet where copying from in excel Set sheet1 = Sheets("Scheduling tracker") Set wdFind = appWd.Selection.Find Cells(Application.ActiveCell.Row, 15).Select Selection.Copy wdFind.Text = "QREQQ" Call NoFormatPaste Cells(Application.ActiveCell.Row, 14).Select Selection.Copy wdFind.Text = "QREQNOQ" Call NoFormatPaste Cells(Application.ActiveCell.Row, 6).Select Selection.Copy wdFind.Text = "QNAMEQ" Call NoFormatPaste Cells(Application.ActiveCell.Row, 15).Select Selection.Copy wdFind.Text = "QREQQ" Call NoFormatPaste Cells(Application.ActiveCell.Row, 14).Select Selection.Copy wdFind.Text = "QREQNOQ" Call NoFormatPaste Dim dateValue As String dateValue = Cells(Application.ActiveCell.Row, 3).Value wdFind.Text = "QDATEQ" Call NoFormatDatePaste(dateValue) Dim timeValue As String timeValue = Cells(Application.ActiveCell.Row, 4).Value wdFind.Text = "QTIMEQ" Call NoFormatTimePaste(timeValue) appWd.Selection.WholeStory appWd.Selection.Copy appWd.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges Set appWd = Nothing Set docWD = Nothing Set appXL = Nothing Set wbXL = Nothing End Sub 

这是我剩下的窗口,每次运行macros时都会创build一个新窗口。

宏观后留下空白的Word窗口

我曾尝试使用

 appWd.Application.Quit SaveChanges:=wdDoNotSaveChanges 

但这会迫使任何其他公开的文件退出。 有谁知道我怎样才能closures文件,而不离开这个空白的应用程序窗口?

请将下面的代码添加到您的代码中:

 appWd.Quit 

这将是之间

 appWd.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 

 Set appWd = Nothing 

这就叫做WinWord Quit方法,它可以解决你的问题。

我有同样的问题。 只有下面的代码closures了这个Word窗口:

 Dim x As Variant X = Shell("powershell.exe kill -processname winword", 1) 

我在其中一个答案中find了这个代码从excel vba中closuresword应用程序

这是VBA中的代码问题:

 x = Shell("powershell.exe kill -processname winword", 1) 

因为之前完成代码VBA运行下一个代码和这个问题。