Error trying to save Word as pdf from Excel

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,340
Office Version
  1. 2016
Platform
  1. Windows
I'm having trouble trying to save a Word document as a pdf file from Excel.

The Word file is embedded into the workbook and the document is extracted and populated, and then need to save it as a pdf file but within a folder selected by the user.

The part that is causing me the issue is the saving as pdf, everything else works fine - this is what I have;

Code:
'Set doc name
dt = Format(TextBox3, "dd_mmmm_yyyy")
strFile = "1-2-1 Meeting Record - " & ComboBox1.Value & " - " & dt

'Save 121
myFile = Application.GetSaveAsFilename(InitialFileName:=strFile, FileFilter:="PDF Files (*.pdf), *.pdf", Title:="Select Folder to save")
If myFile <> "False" Then
Application.ScreenUpdating = False

'write the PDF file
 myDoc.ExportAsFixedFormat OutputFileName:=myFile, _
 ExportFormat:=wdExportFormatPDF, _
 OpenAfterExport:=False, _
 OptimizeFor:=wdExportOptimizeForPrint, _
 Range:=wdExportAllDocument, _
 Item:=wdExportDocumentContent, _
 IncludeDocProps:=True, KeepIRM:=True, _
 CreateBookmarks:=wdExportCreateNoBookmarks, _
 DocStructureTags:=True, _
 BitmapMissingFonts:=True, _
 UseISO19005_1:=False

It fails at the export pdf part and gives me 'Invalid procedure call or argument error 5'

Does anyone know where I am going wrong please?
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
4,094
- The example below worked for me. What Office version are you using?
- Are the text box and combo box inside a user form or on a worksheet?
- What kind of module holds your code?
- The message box will display the path to be used, inspect it for trouble.
- Try writing a simple Word macro to test the method there, no Excel involved.

Code:
'Excel module
Sub Sharky()
Dim dt$, strfile$, myfile$, mydoc As Document, ow
dt = Format(Me.TextBox3, "dd_mmmm_yyyy")
strfile = "1-2-1 Meeting Record - " & Me.ComboBox1.Value & " - " & dt
Set ow = GetObject(, "Word.Application")
ow.Visible = True
ow.Documents.Open "C:\pub\first.docm"
Set mydoc = ow.ActiveDocument
myfile = Application.GetSaveAsFilename(InitialFileName:=strfile, FileFilter:= _
"PDF Files (*.pdf), *.pdf", Title:="Select Folder to save")
If myfile <> "False" Then
    MsgBox myfile, 64, "This path will be used"
    mydoc.Windows(1).Activate
    mydoc.ExportAsFixedFormat OutputFileName:=myfile, _
    ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False
End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,122,511
Messages
5,596,581
Members
414,079
Latest member
Frills

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
Top