I'm writing a macro to print several worksheets in the same workbook to a single PDF file. I get the "automation error the object invoked has disconnected from its client" error message when I run the code below. The codes works until the Activesheet line then errors out (the message boxes are just for validation and will be deleted).
I originally had the OpenAfterPublish parameter set to "true" but changing it to "false" had no impact--still get the error.
Is there a way around this error or another way to create the pdf? I'm using Excel 2016 on Windows 10; to manually print to a PDF I use Microsoft Print to PDF.
Thanks in advance for the help!
Sub PrintToPDF()
Path = CreateObject("WScript.Shell").specialfolders("MyDocuments")
MsgBox Path
Filename = Path & "\Fuel_Report_" & Format(Date, "yyyymmdd") & ".pdf"
MsgBox Filename
Worksheets(Array("FormA_CA", "FormB_CA", "FormC_CA", "FormA_RE", "FormB_RE", "FormC_RE")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Filename, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
I originally had the OpenAfterPublish parameter set to "true" but changing it to "false" had no impact--still get the error.
Is there a way around this error or another way to create the pdf? I'm using Excel 2016 on Windows 10; to manually print to a PDF I use Microsoft Print to PDF.
Thanks in advance for the help!
Sub PrintToPDF()
Path = CreateObject("WScript.Shell").specialfolders("MyDocuments")
MsgBox Path
Filename = Path & "\Fuel_Report_" & Format(Date, "yyyymmdd") & ".pdf"
MsgBox Filename
Worksheets(Array("FormA_CA", "FormB_CA", "FormC_CA", "FormA_RE", "FormB_RE", "FormC_RE")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Filename, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub