Macro to save external word/ppt documents as pdf

vicoyh

New Member
Joined
Jul 17, 2014
Messages
9
Hi,

I am trying to make the following steps work but Step 5 (macro) simply does not work.
Step 1 I have a worksheet where a user is able to enter data in one tab,
Step 2 which is stored in another tab by way of macros,
Step 3 which populates cells in another tab,
Step 4 the cells will then populate the footers of several word/ppt documents acting as a unique watermark in a seperate hidden folder.
Step 5 When the user hits save in excel, save the word/ppt documents as PDFs in a folder which has the file name similar to the entered data
I am clueless how to create a macro to save ppt documents as PDFs and also a macro to generate a folder with file name similar to the entered data

My macro code is as such (For the macro text in red, I get the error Run-time error '5' Invalid procedure call or argument):

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim oWord As Object
Dim oDoc As Object
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open("T:\Watermarked document\1.doc")
oDoc.ExportAsFixedFormat OutputFileName:="T:\1. doc", _
ExportFormat:=wdExportFormatPDF

oWord.Quit
Set oWord = Nothing


Thank you very much for helping!

Best Regards,
Victor
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to MrExcel.

Firstly, the output file name needs a pdf extension (not doc). Secondly, you can't use Word constants when late binding, so try changing wdExportFormatPDF to 17 (the value of that constant).
 
Upvote 0
Thank you very much, Andrew.

I tried and it generates the pdf for me. However I get a run-time error 13 for this line:
Set oDoc = oWord.Documents.Open("T:\Watermarked document\1.doc")

Any idea how to resolve this?

Thank you very much for helping.
 
Upvote 0
Hi Andrew,

Thanks for helping. My code is as such:

' Step 1
Dim objWord
Set objWord = CreateObject("Word.Application")
' Hidden window!
objWord.Visible = False
' Step 2
Dim objDoc
Set objDoc = objWord.Documents.Open("T:\Business Management\PSG Tracking\Z Test Folder\NCE\Watermarked document\1.doc")
objDoc.ExportAsFixedFormat OutputFileName:="T:\Business Management\PSG Tracking\Z Test Folder\NCE\Watermarked document\1.pdf", _
ExportFormat:=17

Where have I gone wrong?
 
Upvote 0
On this line: Set objDoc = objWord.Documents.Open("T:\Business Management\PSG Tracking\Z Test Folder\NCE\Watermarked document\1.doc")
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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