Excel VBA 退出 Word 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27899896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:48:06  来源:igfitidea点击:

Excel VBA quit Word document

excelvbaexcel-vbams-word

提问by Tim Wilkinson

I have a macro that inserts select cells of an Excel document into a Word template, copies the entire Word document then closes the document without saving, to preserve certain keywords.

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

However when it closes the Word document it leaves a blank Word window open, with no active document, and each time the macro runs it leaves a new blank window.

但是,当它关闭 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

This is the window I am left with, and a new one is created each time the macro is run.

这是我留下的窗口,每次运行宏时都会创建一个新窗口。

Blank Word window left after macro

宏后留下空白 Word 窗口

I have tried using

我试过使用

appWd.Application.Quit SaveChanges:=wdDoNotSaveChanges

But this will force any other open documents to quit as well. Does anyone know how I can close the document without leaving this blank application window?

但这也将迫使任何其他打开的文档也退出。有谁知道如何在不离开这个空白应用程序窗口的情况下关闭文档?

回答by Our Man in Bananas

Please add the below to your code:

请将以下内容添加到您的代码中:

appWd.Quit

This would be between

这将介于

appWd.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

and

Set appWd = Nothing

That calls the WinWord Quitmethod, which should solve your problem.

这会调用 WinWord Quit方法,该方法应该可以解决您的问题。

回答by Sharunas Bielskis

I had the same problem. And only code below closed this Word window:

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

Dim x As Variant

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

I found this code in one of the answers Closing word application from excel vba

我在excel vba 的 Closing word application的答案之一中找到了此代码