PDA

View Full Version : [SOLVED:] Print to PDF and then rename



JohnnyBravo
12-08-2020, 05:52 AM
I've been going through these steps manually each time but it's getting to be a little bit tedious and I love some sort of a VBA/VB solution if someone can help me.

1) Take the current open document in Word and print to PDF. This requires selecting a different printer from the drop down menu. See screenshot here. (https://ibb.co/qDDfc7Y)

I realize that MS Word has a built-in functionality to convert a document into PDF, but there are times when I print to this virtual PDF printer for other specific reasons. It's a long story and I don't really want to get into the technical reasons of why.

2) The virtual printer (shown in screenshot) converts the document to a PDF file and the output is always going to be here:
E:\Ghost PDF conversions\

3) The file name is always like this: "Microsoft Word - {saved file name}.docx.pdf"

For Example: If I have saved the Word doc as "Letter to Mom", the converted filename will read as ""Microsoft Word - Letter to Mom.docx.pdf"
I put the curly brackets there just to so you guys can see that it varies from file to file (whatever I've saved it as).

I don't like this format, so then I always end up renaming the file manually which gets very boring and tedious. What I would love is a macro that renames it to:
Letter to Mom.pdf (Deleting "Microsoft Word -" from the beginning and deleting ".docx" from the middle)

And then moves the file to:
G:\Correspondence\

Any help would be greatly appreciated!

Running Win7 OS and MS Office 2010 SP2.

macropod
12-08-2020, 01:35 PM
So neither Word's SaveAs method nor its ExportAsFixedFormat method does what you need?

JohnnyBravo
12-08-2020, 03:24 PM
Nevermind folks, I'll take this question elsewhere.

macropod
12-08-2020, 04:32 PM
When you do, kindly provide the cross-post links on both forums, as per VBA Express' policy on Cross-Posting in Rule 3: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

gmayor
12-08-2020, 11:10 PM
There is a limit to what can be done with third party PDF tools. The following macro run with the original document on screen, after the PDF has been created, will rename the PDF and put it in the folder required as per your post.

Sub RenamePDF()
Dim oDoc As Document
Dim sName1 As String, sName2 As String
Const sPath1 As String = "E:\Ghost PDF conversions\"
Const sPath2 As String = "G:\Correspondence\"
Set oDoc = ActiveDocument
oDoc.Save
sName1 = oDoc.Name
sName2 = Replace(oDoc.Name, "docx", "pdf")
sName1 = "Microsoft Word - " & sName1 & ".pdf"
Name sPath1 & sName1 As sPath2 & sName2
lbl_Exit:
Exit Sub
End Sub
Alternatively you could consider https://www.gmayor.com/pdfcreator_addin.htm, which will create the PDF, using PDF Creator (linked from the page) with the option of security measures absent from Microsoft's implementation, with the required name and in the folder of your choice.