我有一个宏,可以将Excel文档的选定单元格插入到Word模板中,复制整个Word文档,然后关闭文档而不保存,以保留某些关键字。
然而,当它关闭Word文档时,它会打开一个空白的Word窗口,没有活动文档,每次运行宏时都会留下一个新的空白窗口。
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
这是我剩下的窗口,每次运行宏时都会创建一个新窗口。
我尝试过使用
appWd.Application.Quit SaveChanges:=wdDoNotSaveChanges
但这会强制其他任何打开的文件也退出。有没有人知道如何在不离开这个空白的应用程序窗口的情况下关闭文档?
答案 0 :(得分:8)
请将以下内容添加到您的代码中:
appWd.Quit
这将介于
之间appWd.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
和
Set appWd = Nothing
调用WinWord 退出方法,这可以解决您的问题。
答案 1 :(得分:1)
我遇到了同样的问题。只有下面的代码关闭了这个Word窗口:
Dim x As Variant
X = Shell("powershell.exe kill -processname winword", 1)
我在其中一个答案中找到了这段代码 Closing word application from excel vba