VB.Net error when trying to keep Excel file open in loop if the same file is used in the next iteration

0

I have a program in VB.net that generates PDF files from items in a datagridview. It loops through the datagridview, determines what type of file it is, opens the file, saves a PDF, and closes the file. Often, these lists use several worksheets from the same Excel file, each with it's own entry in the datagridview. To prevent the sub from constantly opening and closing the same Excel file 20 times, I am trying to keep the file open if the next item in the list is the same file.

Here is the problem. If the list has a different Excel file than the first one, I get this error:

System.Runtime.InteropServices.COMException: 'Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))'

on this line:

xl.Workbooks(WorkbookName).Sheets(CurrentSheet).ExportAsFixedFormat(0, FileName:=ReportPath & "\" & PDFName)

Here is the full code (irrelevant lines have been eliminated for efficiency):

Sub PrepareForPDF()
    Dim xl As Object
    Dim DocPath As String
    Dim ReportPath As String
    Dim CurrentFile As String
    Dim CurrentBook As Excel.Workbook
    Dim CurrentSheet As String
    Dim PDFName As String
    Dim WorkbookName As String
    Dim WbOpen As Boolean = False

    DocPath = Form1.txtSourcePath.Text.ToString
    ReportPath = Form1.txtSavePath.Text.ToString

'Loop through DGV, generate a PDF of each file
    For i As Integer = 0 To Form1.DataGridView1.Rows.Count - 1

        CurrentFile = DocPath & "\" & Form1.DataGridView1(1, i).Value.ToString

        'Build the PDF file name
        PDFName = Form1.DataGridView1(0, i).Value.ToString & " - " & Form1.DataGridView1(1, i).Value.ToString
        If Form1.DataGridView1(2, i).Value.ToString = "Excel" Then
            PDFName += " - " & Form1.DataGridView1(3, i).Value.ToString
        End If
        PDFName += ".pdf"

        'Generate PDF based on file type - eliminated other file types for efficiency
        If Form1.DataGridView1(2, i).Value.ToString = "Excel" Then

            WorkbookName = Form1.DataGridView1(1, i).Value.ToString
            CurrentSheet = Form1.DataGridView1(3, i).Value.ToString

            If WbOpen = False Then
                xl = New Excel.Application
                xl.Workbooks.Open(FileName:=CurrentFile, UpdateLinks:=False)
                CurrentBook = xl.ActiveWorkbook
            Else
                CurrentBook = xl.ActiveWorkbook
            End If

            CurrentBook.Sheets(CurrentSheet).ExportAsFixedFormat(0, FileName:=ReportPath & "\" & PDFName)

            If i < Form1.DataGridView1.Rows.Count - 1 Then
                If Form1.DataGridView1(1, i + 1).Value.ToString = WorkbookName Then
                    WbOpen = True
                End If
            Else
                WbOpen = False
                CurrentBook = Nothing
                xl.Workbooks(WorkbookName).Close(False)
                xl.Quit()
                ReleaseObject(xl)
            End If
        End If
    Next
    xl = Nothing
End Sub

I did some debugging, and it appears that it is not properly reassigning the CurrentBook variable to a new Excel file but rather keeping the value of an old one.

c#
excel
vb.net
asked on Stack Overflow Feb 4, 2019 by user11014333 • edited Feb 7, 2019 by user11014333

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0