Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel不是';t关闭_Excel_Vba_Ms Word - Fatal编程技术网

Excel不是';t关闭

Excel不是';t关闭,excel,vba,ms-word,Excel,Vba,Ms Word,我正在word visual basic上运行以下工作宏。每次我运行它时,宏都会成功地生成我想要的报告;但是,当我查看任务管理器时,我看到excel的一个实例仍在运行。我在代码上运行调试器,调试器通过最后一行: oExcel.quit 但它仍然没有终止应用程序 Sub WriteExtension() ' ' WriteExtension Macro ' ' copyFile Dim nWord As New Document word.Ap

我正在word visual basic上运行以下工作宏。每次我运行它时,宏都会成功地生成我想要的报告;但是,当我查看任务管理器时,我看到excel的一个实例仍在运行。我在代码上运行调试器,调试器通过最后一行:

oExcel.quit 
但它仍然没有终止应用程序

Sub WriteExtension()
'
' WriteExtension Macro
'
'
        copyFile

        Dim nWord As New Document
        word.Application.ScreenUpdating = False
        Set nWord = Documents.Open("c:\output\report\here\report", Visible:=False)

        'initialize excel variables
        Dim oExcel As Excel.Application
        Dim oWorkbook As workbook
        Dim oWorksheet As worksheet

        'initialize excel object
        Set oExcel = New Excel.Application
        oExcel.ScreenUpdating = False
        Set oWorkbook = oExcel.Workbooks.Open("c:\spreadsheet\here\spreadsheet.xlsx")
        Set oWorksheet = oWorkbook.Worksheets(Sheets("Extensions").Index)
        'setup loop variables

        Dim tempString As String
        Dim delim As String

        Dim i As Long
        Dim bkMark As Bookmark
        Dim questions(13) As String

        questions(0) = 13
        questions(1) = 15
        questions(2) = 17
        questions(3) = 19
        questions(4) = 29
        questions(5) = 31
        questions(6) = 33
        questions(7) = 36
        questions(8) = 38
        questions(9) = 40
        questions(10) = 42
        questions(11) = 46
        questions(12) = 48

        delim = "#"

        tempString = delim & Join(questions, delim)

        Dim bmrange As Range

        For i = 1 To 78

            If (InStr(1, tempString, delim & i & delim, vbTextCompare)) Then
                Set bmrange = nWord.Bookmarks("BM" & (i)).Range
                If (Cells(4, i + 6) = 1) Then
                    nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = True
                Else
                    nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = False
                End If

            ElseIf (InStr(1, tempString, delim & (i - 1) & delim, vbTextCompare)) Then

                Set bmrange = nWord.Bookmarks("BM" & (i)).Range
                If (Cells(4, i + 6) = 1) Then
                    nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = True
                Else
                    nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = False
                End If
            Else
                nWord.Bookmarks.Item("BM" & i).Range.InsertAfter (Cells(4, i + 6))

            End If

        Next i

        Dim filePath As String
        Dim fileName As String
        Dim newName As String

     '   save the file as a PDF and close the PDF
        filePath = "c:\output\report\here\report"
        fileName = Cells(4, 13) & Cells(4, 12) & Cells(4, 79) & ".pdf"
        newName = filePath & fileName
        nWord.SaveAs2 fileName:=newName, FileFormat:=wdFormatPDF

    '   Close things
        nWord.Close False
        oWorkbook.Close False
        oExcel.Quit

End Sub

我怀疑您的问题与不合格的
工作表
单元格
参考有关

Set-oWorksheet=oWorkbook.worksheet(工作表(“扩展”).Index)
应该是
Set-oWorksheet=oWorkbook.worksheet(“扩展”)
(不需要使用工作表的名称来获取对工作表的引用,只要通过名称对其进行索引即可)和
单元格(4,i+6)
可能应该是
oWorksheet.Cells(4,i+6)

我可以在进行这些更改之前复制您的问题(尽管有时代码会崩溃),但一旦我修复了它们,Excel将在
末尾子部分正确关闭。(在
oExcel.Quit
之后,它没有消失,因为
oExcel
还不是
什么都没有
。)


如果您执行了
应用程序。退出
?或者您是从Word或Excel以外的其他应用程序调用此命令?我是从Word调用此命令。您还尝试了什么?关于这个问题,有几个线程是这样的。或不确定,但您可以说
oExcel.Application.Quit
?您确定看到的Excel实例是由该宏创建的吗?它可能是宏在某个阶段崩溃时留下的。
设置oExcel=Nothing
需要在
oExcel之后执行。退出
,但您已经在接受的答案中得到了这一点。谢谢!这就是答案。我将确保对我使用的变量进行限定。啊哈!我没有赶上不合格的射程。好主意。@BruceWayne-是的,我不知道为什么,但我假设他们创建了对额外自动创建的Excel对象的某种引用,因为他们没有“连接”到
oExcel
Sub WriteExtension()
'
' WriteExtension Macro
'
'
    copyFile

    Dim nWord As New Document
    word.Application.ScreenUpdating = False
    Set nWord = Documents.Open("c:\output\report\here\report", Visible:=False)

    'initialize excel variables
    Dim oExcel As Excel.Application
    Dim oWorkbook As workbook
    Dim oWorksheet As worksheet

    'initialize excel object
    Set oExcel = New Excel.Application
    oExcel.ScreenUpdating = False
    Set oWorkbook = oExcel.Workbooks.Open("c:\spreadsheet\here\spreadsheet.xlsx")
    Set oWorksheet = oWorkbook.Worksheets("Extensions")
    'setup loop variables

    Dim tempString As String
    Dim delim As String

    Dim i As Long
    Dim bkMark As Bookmark
    Dim questions(13) As String

    questions(0) = 13
    questions(1) = 15
    questions(2) = 17
    questions(3) = 19
    questions(4) = 29
    questions(5) = 31
    questions(6) = 33
    questions(7) = 36
    questions(8) = 38
    questions(9) = 40
    questions(10) = 42
    questions(11) = 46
    questions(12) = 48

    delim = "#"

    tempString = delim & Join(questions, delim)

    Dim bmrange As Range

    For i = 1 To 78

        If (InStr(1, tempString, delim & i & delim, vbTextCompare)) Then
            Set bmrange = nWord.Bookmarks("BM" & (i)).Range
            If oWorksheet.Cells(4, i + 6) = 1 Then
                nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = True
            Else
                nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = False
            End If

        ElseIf InStr(1, tempString, delim & (i - 1) & delim, vbTextCompare) Then

            Set bmrange = nWord.Bookmarks("BM" & (i)).Range
            If oWorksheet.Cells(4, i + 6) = 1 Then
                nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = True
            Else
                nWord.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked = False
            End If
        Else
            nWord.Bookmarks.Item("BM" & i).Range.InsertAfter (oWorksheet.Cells(4, i + 6))

        End If

    Next i

    Dim filePath As String
    Dim fileName As String
    Dim newName As String

 '   save the file as a PDF and close the PDF
    filePath = "c:\output\report\here\report"
    fileName = oWorksheet.Cells(4, 13) & oWorksheet.Cells(4, 12) & oWorksheet.Cells(4, 79) & ".pdf"
    newName = filePath & fileName
    nWord.SaveAs2 fileName:=newName, FileFormat:=wdFormatPDF

'   Close things
    nWord.Close False
    oWorkbook.Close False
    oExcel.Quit

    'Optional: Set Excel objects to Nothing so that Excel closes now instead of at End Sub
    Set oWorkbook = Nothing
    Set oExcel = Nothing

End Sub