Export as PDF to Desktop

hmltnangel

Board Regular
Joined
Aug 25, 2010
Messages
151
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

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
 

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
620
Instead of:
Code:
strName = Range("E4")
Try:
Code:
strName = Sheets("SHEET NAME HERE").Range("E4").value
 

hmltnangel

Board Regular
Joined
Aug 25, 2010
Messages
151
Thanks, I just managed it on my own as I was coming back to check. :D The below is what I used.

Code:
Sub PDFActiveSheet()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
Path = CreateObject("WScript.Shell").SpecialFolders("Desktop")
'export to PDF if a folder was selected
If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        filename:=Path & "\" & Range("E4") & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "Dist Checklist PDF has been saved to your desktop: " _
      & vbCrLf _
      & myFile
End If
exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler
End Sub
 

Forum statistics

Threads
1,078,348
Messages
5,339,685
Members
399,319
Latest member
JOSEILLO

Some videos you may like

This Week's Hot Topics

Top