Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA quit Word document

Tags:

ms-word

excel

vba

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.

Blank Word window left after macro

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?

like image 694
Tim Wilkinson Avatar asked Jan 12 '15 10:01

Tim Wilkinson


1 Answers

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.

like image 151
Our Man in Bananas Avatar answered Oct 06 '22 20:10

Our Man in Bananas