我有一個宏,它將一個Excel文檔的選定單元格插入Word模板,複製整個Word文檔,然後關閉文檔而不保存,以保留某些關鍵字。Excel VBA quit 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
試圖但這會迫使其他任何打開的文檔退出爲好。有沒有人知道如何在不離開這個空白的應用程序窗口的情況下關閉文檔?