Excel VBA exits Word document
I have a macro that inserts the selection 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 every 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 that I stayed in and a new one is created every time the macro runs.
I have tried using
appWd.Application.Quit SaveChanges:=wdDoNotSaveChanges
But this will cause other open documents to stop working. Does anyone know how I can close the document without leaving this blank application window?
source to share
I had the same problem. And just the 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 Final word-appendix from excel vba
source to share