PDF Created from VBA Button won't open

QandAdam

New Member
Joined
Jan 12, 2019
Messages
30
I've got this code below which creates a PDF document. The error message appears saying I'm unable to open the document because:

Code:
 it is either not a supported file type or because the file has been damaged

VBA Code:
Code:
Private Sub Command273_Click()

Dim wdApp As Word.Application, wdDoc As Word.Document


FileName = [URN] & " - Agreement X34"
FilePath = "\Backup\Agreements\Leads\" & FileName & ".pdf"


On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0


Set wdDoc = wdApp.Documents.Open("\Backup\Agreements\Leads\Agreement X34.docx")


wdApp.Visible = False


wdDoc.FormFields("ReferenceID").Result = Nz(Me!txtPDFReference, "")
wdDoc.FormFields("txtCompanyName").Result = Nz(Me!BusinessName, "")
wdDoc.FormFields("txtAddressLine1").Result = Nz(Me!AddressLIne1, "")
wdDoc.FormFields("txtAddressLine2").Result = Nz(Me!AddressLine2, "")
wdDoc.FormFields("txtPostCode").Result = Nz(Me!PostCode, "")


wdDoc.SaveAs2 FilePath




End Sub

The code works when I change the FilePath from ".pdf" to ".docx"
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Are you really using Microsoft Access to open a Word Document and save as a PDF?

The issue is with your SaveAs command. If not saving in the same format (Word), you MUST specify the file format (the "pdf" file extension does NOT do this).
You are missing the FileFormat argument for your SaveAs command.
See here: https://docs.microsoft.com/en-us/office/vba/api/word.saveas2
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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