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