tlc53
Active Member
- Joined
- Jul 26, 2018
- Messages
- 399
Hi,
I have the following module VBA code which saves a report located on a different sheet (sheet80) to a PDF file.
I would like to have sheet80 hidden as it's all formulated and the report does not need editing. However, when I run the code on a different sheet, it only works correctly if sheet80 is not hidden, otherwise it returns "Could not create PDF file". Not sure why this would matter. Any ideas please?
I have the following module VBA code which saves a report located on a different sheet (sheet80) to a PDF file.
I would like to have sheet80 hidden as it's all formulated and the report does not need editing. However, when I run the code on a different sheet, it only works correctly if sheet80 is not hidden, otherwise it returns "Could not create PDF file". Not sure why this would matter. Any ideas please?
Code:
Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = Sheet80
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & ""
strName = wsA.Range("A1").Value _
& " - " & wsA.Range("A2").Value _
& " " & Format(wsA.Range("A3"), "dd.mm.yy")
'create default name for savng file
strFile = strName & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Last edited by a moderator: