Error HRESULT E_FAIL has been returned from a call to a COM component.

Hi,

I need some help figuring this one out.

I am trying to automatically export I/C Internal Usage reports from Sage 300 using a C# console app. I have recorded a macro in Sage 300 and have used the macro to successfully export the report to PDF. From the Console App however, I get the error in the subject as soon the GetPrintSetup method is called from the API. Below is my code and attached is a screen grab of the error in VS2022. The highlighted line is where the error appears.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AccpacCOMAPI;

namespace ConsoleApp1
{
internal class Program
{
private static AccpacSession session;
private static AccpacReport rpt;
private static AccpacPrintSetup rptPrintSetup;
static void Main(string[] args)
{
session = new AccpacSession();
session.Init("", "XY", "XY1000", "69A");

if (!session.IsOpened)
{
session.Open("ADMIN", "ADMIN1", "SAMINC", DateTime.Now, 0, "");
}

rpt = session.ReportSelect("ICTRLSTI", " ", " ");
rptPrintSetup = session.GetPrintSetup(" ", " ");
rptPrintSetup.DeviceName = "Microsoft Print to PDF";
rptPrintSetup.OutputName = "USB001:";
rptPrintSetup.PaperSize = 9;
rptPrintSetup.UseA4 = true;
rptPrintSetup.UseReportOrientation = true;

rpt.PrinterSetup(rptPrintSetup);

rpt.SetParam("REPRINT", "1");
rpt.SetParam("COMMENTS", "1");
rpt.SetParam("HOMECUR", "USD");
rpt.SetParam("MULTICUR", "0");
rpt.SetParam("FROMNUM", "INT0000000000000000002");
rpt.SetParam("TONUM", "INT0000000000000000002");
rpt.SetParam("FROMDATE", "19000101");
rpt.SetParam("TODATE", "99991231");
rpt.SetParam("ENTERED", "1");
rpt.SetParam("POSTED", "1");
rpt.SetParam("HOMEDECIMAL", "2");
rpt.SetParam("FRACTIONAL", "0");
rpt.SetParam("SWOPTLIC", "0");
rpt.SetParam("OPTFLDS", "0");
rpt.SetParam("SWAPACTIVE", "1");
rpt.SetParam("SWGLACTIVE", "1");
rpt.SetParam("SWSNLTLIC", "0");
rpt.SetParam("SERIALLOT", "0");
rpt.NumOfCopies = 1;

rpt.Destination = tagPrintDestinationEnum.PD_FILE;
rpt.Format = tagPrintFormatEnum.PF_PDF;
rpt.PrintDir = "C:\\Sage\\ExportedReports\\InternalUsage.pdf";
rpt.PrintReport();
Console.ReadLine();
}
}
}