2015-01-12 187 views
3

我有一個宏,它將一個Excel文檔的選定單元格插入Word模板,複製整個Word文檔,然後關閉文檔而不保存,以保留某些關鍵字。Excel VBA quit Word文檔

但是,當它關閉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 

這是我留下的窗口,每次運行宏時都會創建一個新窗口。

Blank Word window left after macro

我一直在使用

appWd.Application.Quit SaveChanges:=wdDoNotSaveChanges 

試圖但這會迫使其他任何打開的文檔退出爲好。有沒有人知道如何在不離開這個空白的應用程序窗口的情況下關閉文檔?

回答

4

請在下面添加到您的代碼:

appWd.Quit 

這將是

調用WINWORD之間

appWd.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges 
退出方法,它應該解決你的問題。

0

我有同樣的問題。而下面只代碼關閉該Word窗口:

Dim x As Variant 

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

我發現這個代碼的答案 Closing word application from excel vba

-1

之一這是代碼一個問題VBA

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

因爲之前完成代碼VBA運行下一個代碼和這個問題。