RobK
RobK

Reputation: 207

VBA - Save Word doc as pdf from Excel - Named argument not found

I'm trying to let an Excel script create a document file and save it as pdf file. Creating the document goes without a problem, but finding the right code to save it as pdf file seems to be a problem. I've searched the internet and there are many manuals and answers to the same question which all come down to the same code:

ActiveDocument.ExportAsFixedFormat OutputFileName:= _
ActiveDocument.Path & "\" & ActiveDocument.Name & ".pdf", ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False

Somehow that doesn't seem to do the trick for me, as it gives me the error "Named argument not found". Here's my script (I've left out the irrelevant lines and I'm sure the path and filename are stored correctly).

'Dim Word formfield values
Dim objWord As Object
Dim strDocName As String, strDocName1 As String, strDocName2 As String
Dim strMyPath As String

'Declare Word variables
Set objWord = CreateObject("word.application")
objWord.Visible = True

With objWord.activedocument
    'fill in a bunch of formfields

    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strMyPath & "\" & strDocName2, _
    OpenAfterExport:=False
    .Close
End With

What am I doing wrong here?

Upvotes: 0

Views: 11096

Answers (2)

RobK
RobK

Reputation: 207

Thanks Blackhawk!

In the meantime I've found another solution. I needed another Object (objWordDoc) variable and I've replaced "With objWord.activedocument" with:

Set objWordDoc = objWord.Documents.Add(strDocToOpen)

With objWordDoc

Nevertheless your suggestions will surely come in handy in the future!

Upvotes: 0

Blackhawk
Blackhawk

Reputation: 6140

The reason it doesn't work is that Excel doesn't know what any of the constant values of Word are, like wdExportAllDocument or wdExportCreateNoBookmarks.

In order to make that code work, you would have to add a reference in your project by clicking "Tools" ---> "References..." and then clicking the checkbox next to "Microsoft Word 14.0 Object Library". You can then use a cleaned up version of the code that you posted at the top of your question. Keep in mind globals like ActiveDocument don't exist in Excel and you will have to use objWord.ActiveDocument instead. Also, for your case I would suggest switching your code to use Early Binding (see here for a description of the difference) so that you will have intellisense. This can prevent troublesome errors caused by misspelling.

Upvotes: 3

Related Questions