Search code examples
.netexcelvb.netexport-to-excelquickbooks

How do I export a Quickbooks report to Excel?


I began a Windows Forms project to produce a cash flow report from Quickbooks reports. I would like to use the Quickbooks SDK to log into Quickbooks and export a report to Excel. I have successfully done the login but am stuck with doing the report export to Excel. Is using SendKeys a possibility?

Here is my code:

Imports QBFC13Lib

Public Class Form1

    Private Sub GetReportToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GetReportToolStripMenuItem.Click
        Dim sessionBegun As Boolean
        sessionBegun = False
        Dim connectionOpen As Boolean
        connectionOpen = False
        Dim sessionManager As QBSessionManager
        sessionManager = Nothing

        Try

            'Create the session Manager object
            sessionManager = New QBSessionManager


            'Connect to QuickBooks and begin a session
            sessionManager.OpenConnection("", "Your application")
            connectionOpen = True
            sessionManager.BeginSession("", ENOpenMode.omDontCare)
            sessionBegun = True

            MsgBox("started session!")

            'End the session and close the connection to QuickBooks
            sessionManager.EndSession()
            sessionBegun = False
            sessionManager.CloseConnection()
            connectionOpen = False

            MsgBox("Connection Closed")

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error")
            If (sessionBegun) Then
                sessionManager.EndSession()
            End If
            If (connectionOpen) Then
                sessionManager.CloseConnection()
            End If
        End Try


        sessionManager = Nothing
        sessionBegun = Nothing
        connectionOpen = Nothing

    End Sub

Solution

  • The QuickBooks SDK itself does not natively support exporting to Excel, so you will have to write some code to do this.

    Once you've done the login, you can export reports along these lines:

    var query = msgSetRq.AppendGeneralSummaryReportQueryRq();
    query.GeneralSummaryReportType.SetValue(ENGeneralSummaryReportType.gsrtLotNumberInStockBySite);
    
    msgSetRq.Attributes.OnError = ENRqOnError.roeContinue;
    
    var msgSetRs = sessManager.DoRequests(msgSetRq);
    

    I would highly recommend you review Intuit's documentation on reporting with QBFC:

    1. https://developer-static.intuit.com/qbSDK-current/doc/PDF/QBSDK_ProGuide.pdf
    2. https://developer.intuit.com/app/developer/qbdesktop/docs/develop/exploring-the-quickbooks-desktop-sdk/preparing-report-requests

    You'll get back an object with all of the report data. You can then loop through the report and build your Excel sheet.

    Creating Excel sheets looks something like this:

    Dim appXL As Excel.Application
          Dim wbXl As Excel.Workbook
          Dim shXL As Excel.Worksheet
          Dim raXL As Excel.Range
    
          ' Start Excel and get Application object.
          appXL = CreateObject("Excel.Application")
          appXL.Visible = True
    
          ' Add a new workbook.
          wbXl = appXL.Workbooks.Add
          shXL = wbXl.ActiveSheet
    
    
          ' Create an array to set multiple values at once.
          Dim qbdata(5, 2) As String
          qbdata(0, 0) = "data from QB here"
          qbdata(0, 1) = "and here"
    
          ' Fill A2:B6 with an array of data
          shXL.Range("A2", "B6").Value = qbdata
    
    
          ' Make sure Excel is visible and give the user control
          ' of Excel's lifetime.
          appXL.Visible = True
          appXL.UserControl = True
    
          ' Release object references.
          raXL = Nothing
          shXL = Nothing
          wbXl = Nothing
          appXL.Quit()
          appXL = Nothing