Macro: Designate File name & Print to PDF

mws4ua

New Member
Joined
Sep 23, 2008
Messages
28
I did search the forum a bit first, but couldn't find an answer...

I want to create a macro that will select text in a certian cell, then use that text as part of a name when it publishes the sheet to PDF. I used the Macro recorder to create the code below.

Each month I will update the contents of cell B6 (201102 = February 2011, 201103 = March 2011, etc.)

I want to save the analysis on Sheet "18CFA" each month as a PDF, and I want the file name to begin with the 6-digit month identifier ("201103 - Line 18 Analysis.pdf" in this example).

In the current macro, I go to the number in B6, copy it, then go to the sheet I want to save and click the "Save As PDF or XPS" button. I then Paste what was copied from B6 (in this case, "201103", then type the rest of the name and click Publish.

The macro currently does two things wrong. First, when it's finished, the contents of cell B6 are always "201103," and second, obviously, it's not using what I type into B6 as the file name.

Anyway... here's the code:

Sub PrintPDF()
'
' PrintPDF Macro
'
'
Range("B6").Select

ActiveCell.FormulaR1C1 = "201103"

Range("A1").Select

Sheets("18FCA").Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Documents and Settings\mstuenke\Desktop\201103 - Line 18 Analysis.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try

Code:
Sub PrintPDF()
'
' PrintPDF Macro
'
'
Dim d As String
d = Format(Date, "yyyymm")
Sheets("18FCA").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Documents and Settings\mstuenke\Desktop\" & d & " - Line 18 Analysis.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

End Sub
 
Upvote 0
Cell B6 no longer changes back to 201103, but the file name is 201103 no matter what I enter into B6...
 
Upvote 0
Try entering a date (not text) in B6 and try

Code:
Sub PrintPDF()
'
' PrintPDF Macro
'
'
Dim d As String
d = Format(Range("B6").Value, "yyyymm")
Sheets("18FCA").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Documents and Settings\mstuenke\Desktop\" & d & " - Line 18 Analysis.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

End Sub
 
Upvote 0
One more quick question...

I actually want to print two sheets into a single PDF document. The sheets are "18CFA" and "18CFA2". What would the "Sheets(..." portion of the code look like if I wanted to print both?

Thanks again, btw!
 
Upvote 0
I know I'm blowing up my own thread... But I think I figured it out:

Code:
Sub PrintPDF()
'
' PrintPDF Macro
'
'
Dim d As String
d = Format(Range("B6").Value, "yyyymm")
Sheets(Array("18FCA", "18FCA2")).Select
Sheets("18FCA").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Documents and Settings\mstuenke\Desktop\" & d & " - Line 18 Analysis.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub
 
Upvote 0
Maybe

Code:
Sub PrintPDF()
'
' PrintPDF Macro
'
'
Dim d As String, ws As Worksheet
d = Format(Range("B6").Value, "yyyymm")
For Each ws In Sheets(Array("18FCA", "18FCA2"))
    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Documents and Settings\mstuenke\Desktop\" & d & " - Line 18 Analysis.pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
Next ws
End Sub
 
Upvote 0
Thanks for responding, but I opened a blank workbook and played w/ the recorder to create the code in post #7. It works pretty well.

Thanks again for your help. I'm a code noob. I need to learn more about the Dim function.
 
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top