Excel VBA quit Word document

北慕城南 提交于 2020-01-11 09:22:06

问题


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.

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.

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.

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?


回答1:


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 Quit method, which should solve your problem.




回答2:


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

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



来源:https://stackoverflow.com/questions/27899896/excel-vba-quit-word-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!