General discussion

Locked

Use a macro in MS project to create a custom PDF file name automatically

By scotfett ·
I searched everywhere for someone who could tell me how to use MS Project 2003 macros and CutePDF - to generate a custom file name automatically. I didn't find it anywhere so I gave up and figured it out. For anyone who wants it - I've copied an example of the code below.

'
' ************************************************************************************************
' THIS SUBROUTINE ALLOWS YOU TO USE A PDF WRITER SUCH AS CUTEPDF TO PRODUCE CUSTOM-NAMED PDF FILES
' FROM A MICROSOFT PROJECT 2003 PROJECT PLAN. THIS LIKELY WORKS IN 2007, 2010 VERSIONS AS WELL.
' ************************************************************************************************
' The ability to create custom PDF names is missing from most of the free PDF file writers.
' The trick below is to fool MS project into naming the file for you before you write
'
' 1) Build an MS Project report (view(reports) - you'll need a table to base it on.)
' 2) Gather the data you need to build the appropriate PDF file name
' 3) Assemble the pdf file name in a string field
' 4) Save the original MS project report name so you can restore it when finished
' 5) Use the OrganizerRenameItem command as shown below to replace the MS proj report name with your PDF name
' 6) Use MS project ReportPrint command with the PDF writer as the default printer
' *** important - to set the default writer for MS Project 2003 reports, run the report manually one
' time, clicking the print button and checking that the PDF writer is the default (at the top)
' Once you've got this set, you can cancel out of this screen and use your macro
' - whatever printer you selected will be the default until you change it or close the proj plan.
' Simply setting your windows default to the PDF printer won't do it.
' 7)Use the OrganizerRenameItem command to restore the original MS proj report name into your plan
'
' NOTES:
' The example below is trimmed down to focus on the concepts. Normally you may have other checks and
' balances on user input and opportunities for them to cancel the run etc.
'
' The PDF writer still pops up the "save as" dialog box but with the custom name filled in.
' Now you only need hit enter to create the file.
' *** caution - let the PDF writer box finish its job before you attempt another write command.
' If you have multiple "save as" boxes open, all of them could be overwritten with the latest
' data even though they each have their own unique name. I usually insert a msgbox to
' stop further execution in the macro until the user signals that PDF save is finished.
' This may not always be the case but I managed to do it to myself a few times in Proj 2003.
'
' The PDF file name created has an unavoidable but perfectly predictable prefix that can be removed later
' "Microsoft Office Project - " Below are the three PDF files saved as result of the sample code below
'
' Microsoft Office Project - The Big Boss-06-19 V1.pdf
' Microsoft Office Project - The Guy Who Does The Work-06-19 V1.pdf
' Microsoft Office Project - Jim Joyride-06-19 V1.pdf
'
'
Sub Build_Custom_PDF_File_name()

Dim projname As String 'full path project name is needed for the OrganizerRenameItem command
Dim orig_rpt_name As String 'a temporary place to hold the original MS project report name
Dim pdf_name As String 'temporary place to "build" the pdf name you want
Dim superintendant(3) As String 'dummy data - a string of employee names as an example
Dim PDF_suffix As String 'dummy data - a custom suffix to be pasted after the employee name

' Force in some test data just for giggles (and to test of course)
superintendant(0) = "The Big Boss"
superintendant(1) = "The Guy Who Does The Work"
superintendant(2) = "Jim Joyride"

'
Accepted_Input = False
Do While (Accepted_Input = False)
PDF_suffix = InputBox("Enter PDF suffix for this run:") 'Request user input for the suffix
' and confirm that they're okay with what they keyed
Accepted_Input = Message("You entered PDF suffix: " & PDF_suffix, pjYesNo, "Suffix is OK", "Re-enter Suffix")
Loop


projname = ActiveProject.FullName 'Acquire the project name
orig_rpt_name = "Employee by Date" 'This example uses a pre-defined report layout called Employee By Date"

Index = 0
Do While (Index < 3)
pdf_name = "" & superintendant(Index) & "-" & PDF_suffix & "" 'Build a PDF name with employee and suffix
OrganizerRenameItem Type:=pjReports, filename:=projname, _
Name:=orig_rpt_name, newname:=pdf_name 'rename the report to your PDF name
ReportPrint Name:=pdf_name
OrganizerRenameItem Type:=pjReports, filename:=projname, _
Name:=pdf_name, newname:=orig_rpt_name 'restore the orig rpt name
Index = Index + 1
Loop

MsgBox ("Run Ended")
End Sub

This conversation is currently closed to new comments.

0 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Back to Software Forum
0 total posts (Page 1 of 1)  

Related Discussions

Related Forums