colts4u
New Member
- Joined
- Jan 18, 2022
- Messages
- 11
- Office Version
- 365
- Platform
- Windows
I've been hammering my head trying to figure out why I keep getting an Error 1004 when I run this sub to save chart as a PDF. The error says "Document nor saved. The document may be open, or an error may have been encountered when saving." I've stepped through it several times and have made some changes, but nothing seems to work. The folder exists, so I don't think it has to do with it not being found. Here's the code...
VBA Code:
Sub SaveChartAsPDF()
On Error GoTo err_handler
'Create and assign variables
Dim saveLocation As String
Dim strTestFolder As String
Dim strTestName As String
Dim strEvent As String
Dim strCycleCount As String
Dim cht As Chart
Charts(1).Activate
strTestFolder = "C:\Users\username\OneDrive - Carrier Corporation\Current Inspections\"
'strTestFolder = "C:\Users\username\Documents\PDF Favorites\" 'Tried saving local
strTestName = Worksheets("DataHistory").Range("A1")
strEvent = Worksheets("DataHistory").Range("C1")
strCycleCount = Worksheets("DataHistory").Range("B1")
saveLocation = strTestFolder & strTestName & "\" & strTestName & "_" & strCycleCount & "_" & strEvent & ".pdf"
'Charts(1).Activate
Set cht = Charts(1)
'Save a chart as PDF
cht.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
exit_handler:
Exit Sub
err_handler:
MsgBox "Error " & Err.Number & Chr(13) & Chr(10) & Err.Description, vbExclamation, "Whoops, it didn't save as PDF! :("
Resume exit_handler
End Sub