Excel不closures

我在word visual basic上运行以下工作macros。 每次我运行它,macros成功生成我想要的报告; 但后来我看着任务pipe理器,我看到一个Excel的实例仍在运行。 我在代码上运行debugging器,debugging器通过最后一行:

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 

我怀疑你的问题与你的不合格的SheetsCells引用有关。

Set oWorksheet = oWorkbook.Worksheets(Sheets("Extensions").Index)应该可以Set oWorksheet = oWorkbook.Worksheets("Extensions") (不需要通过使用名字来获取参考当你可以通过它的名字来索引它时)和Cells(4, i + 6)应该是oWorksheet.Cells(4, i + 6)

我可以复制你的问题之前,我做了这些改变(虽然有时代码会刚刚崩溃),但一旦我解决他们的Excel在End Sub正确closures。 ( oExcel.Quit没有消失,因为oExcelNothing 。)

 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