hmltnangel
Active Member
- Joined
- Aug 25, 2010
- Messages
- 290
- Office Version
- 365
- Platform
- Windows
Hi folks,
I have bee puzzling this one for a little bit now and can make excel do a few things. Unfortunately cant get it quite right.
I want it to simply save my excel worksheet as a pdf to my desktop with cell E4 on the active sheet as the filename.
The code below saves to desktop without cell e4 as the filename.
Any suggestions
I have bee puzzling this one for a little bit now and can make excel do a few things. Unfortunately cant get it quite right.
I want it to simply save my excel worksheet as a pdf to my desktop with cell E4 on the active sheet as the filename.
The code below saves to desktop without cell e4 as the filename.
Any suggestions
Code:
Sub PDFActiveSheet()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
'replace spaces and periods in sheet name
strName = Range("E4")
'create default name for savng file
strFile = Environ("USERPROFILE") & "\Desktop" & "\strName" & ".pdf"
'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