saveas pdf from excel vba

pamtupac

New Member
Joined
May 30, 2017
Messages
13
hi all,

i'm trying to write a macro that will save my already opened word doc as a PDF and closes the doc from my excel when i click a button. here is my code:

Private Sub CommandButton4_Click()
'name from cell in sheet
cName = Range("a1").Value

Dim appWD As Word.Application
Set appWD = GetObject(, "Word.Application")
appWD.Visible = True

Set wdDoc = appWD.Documents.Open("C:\Users\sesa460426\Documents\Quotes\Tool\QuickQuoteDoc.docx")
With wdDoc
.SaveAs ("C:\Users\sesa460426\Documents\Quotes") & cName & (".pdf")
.Close
End With
wdApp.Quit
End Sub

i keep getting run time error 91: object variable or with block variable not set.

it is saving it as a pdf but i can not open this pdf so i'm wondering if i have to use a different method to save it as pdf or is just cuz the runtime error in my code.

Thank you in advance
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You need ExportAsFixedFormat rather than SaveAs:

Code:
Private Sub CommandButton4_Click()

    Dim wordApp As Word.Application
    Dim cName As String
    
    cName = ActiveSheet.Range("A1").Value

    On Error Resume Next
    Set wordApp = GetObject(, "Word.Application")
    On Error GoTo 0
    
    If Not wordApp Is Nothing Then
        wordApp.ActiveDocument.ExportAsFixedFormat OutputFileName:="C:\Users\sesa460426\Documents\Quotes" & cName & ".pdf", _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False
        wordApp.Quit False
    Else
        MsgBox "A Word document isn't open"
    End If

End Sub
 
Upvote 0
thank you so much! i'm very new to vba and while i understand the logic it's the syntax that i usually have issues with. Thanks again :)
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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