none
Error 91 - Object variable or With block variable not set RRS feed

  • Question

  • When I run this piece of code in debug, it works just fine.  Yet, when I do not use debug, I keep getting an error 91.  The line is in bold that I get the error on.  The main issue is trying to save the workbook when I am done writing to it.

    Set XL = New Excel.Application
    XL.Visible = True
    Set wb = XL.Workbooks.Add
    wb.Activate

    Set WKS = wb.ActiveSheet ' Default: The first sheet in the newly created book

     With rst1
            .MoveFirst
            ' The table headers
            i = 1
            j = 1
            For Each f In .Fields
                WKS.Cells(i, j).Value = f.Name
                j = j + 1
            Next f
            ' The table data
            Do
                i = i + 1
                j = 1
                For Each f In .Fields
                    WKS.Cells(i, j).Value = f.Value
                    j = j + 1
                Next f
                .MoveNext
            Loop Until rst1.EOF
        ActiveWorkbook.SaveAs FileName:=filelocation1, FileFormat:=-4143

        End With

    Thursday, March 1, 2018 9:33 PM

Answers

  • How about?

    wb.ActiveWorkbook.SaveAs FileName:=filelocation1, FileFormat:=-4143

    Just a guess...

    • Marked as answer by ballj_351 Friday, March 2, 2018 12:13 AM
    Thursday, March 1, 2018 10:46 PM

All replies