Reputation: 23
I used a VBA code attached below to save an Excel worksheet as PDF. However when I run the VBA code, it always returns me Run-time error '-2147417848 (800010108)'
Please help me, I am getting desperate. Are there any recommendations to what I am experiencing?
Note: the worksheet I am trying to print only contains the word "test" in cell A1
Sub SimplePrintToPDF()
Dim saveLocation As String
saveLocation = "C:\Users\Santo\Downloads\test123.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation, Quality:=xlQualityStandard, _
IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
I am 100% sure that my VBA code is correct since I have tested this code on another device at it runs perfectly. I have looked through sources and there are recommendations such as turn on windows feature, restart the system and so on but none worked.
Upvotes: 2
Views: 123
Reputation: 54807
Sub SimplePrintToPDF()
' Define constants.
Const dSubfolderName As String = "Downloads"
Const dFileName As String = "Test123.pdf"
On Error GoTo ClearError
' Build the folder path.
Dim pSep As String: pSep = Application.PathSeparator
Dim dFolderPath As String
dFolderPath = Environ("USERPROFILE") & pSep & dSubfolderName & pSep
If Len(Dir(dFolderPath, vbDirectory)) = 0 Then
MsgBox "The path '" & dFolderPath & "' was not found.", vbCritical
Exit Sub
End If
' Build the file path.
Dim dFilePath As String: dFilePath = dFolderPath & dFileName
' Check these results in the Immediate window (Ctrl+G).
Debug.Print "FilePath: " & dFolderPath
Debug.Print "FileName: " & dFileName
Debug.Print "FilePath: " & dFilePath
' Reference the sheet (charts are allowed).
If ActiveSheet Is Nothing Then
MsgBox "No visible workbooks open.", vbCritical
Exit Sub
End If
Dim sh As Object: Set sh = ActiveSheet
' Export to PDF.
sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=dFilePath, _
Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
' Explore subfolder.
'sh.Parent.FollowHyperlink dFolderPath
ProcExit:
On Error Resume Next
' any final code to run after an error
On Error GoTo 0
Exit Sub
ClearError:
MsgBox "Run-time Error '" & Err.Number & "':" & vbLf & Err.Description
Resume ProcExit
End Sub
Upvotes: 1