VBA CreatePDF name from worksheet reference

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
657
Hi All,

I'm working with a sub that creates a pdf and it works great.

Here it is:

VBA Code:
Sub CreatePDF()
    
    
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


Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", " ")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = strName & "_" & strTime & ".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
 
End Sub


My question is, how do I title the newly formed pdf from a reference cell from the worksheet?

For example, how do I tell the sub to make the new name of the pdf, from the contents from Worksheets(1).Range("B9")?

If you can help me out on this, I'd really appreciate it!


Thank you,
pinaceous
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,796
Office Version
  1. 2010
Platform
  1. Windows
myfile = Worksheets(1).Range("B9")
 

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
657
myfile = Worksheets(1).Range("B9")
HI yky,

Thank you for responding to my post. Could you please help me put your post into the code? I have no idea where it would go. Appreciate it.
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,796
Office Version
  1. 2010
Platform
  1. Windows
Try this:

VBA Code:
Sub CreatePDF()
   
   
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


Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

'replace spaces and periods in sheet name
'strName = Replace(wsA.Name, " ", " ")
'strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = Worksheets(1).Range("B9") & "_" & strTime & ".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

End Sub
 

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
657
Hey yky!

That did it!

Thank you for helping me piece that one together.
pinaceous
 

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,796
Office Version
  1. 2010
Platform
  1. Windows
You're welcome.
 

Watch MrExcel Video

Forum statistics

Threads
1,119,224
Messages
5,576,830
Members
412,749
Latest member
BlakeVanderMeer
Top