Gjeep
Gjeep

Reputation: 27

Save a word file from excel as pdf through vba

So I got the project working with opening the .dotx and inserting the values from the excel file and saving as .docx. But I can't for the life of me get it to save as .pdf instead. Well I can, but the pdf can't be opened.

Sub Cost_Statement()

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim i As Integer
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = False
Application.ScreenUpdating = True

TemplateLocation = "C:\Custom documents\Cost_statement.dotx"

Set wrdDoc = wrdApp.Documents.Add(TemplateLocation, False, , False)

On Error GoTo NextNumber

cellno = "Main!C19"
FindText2 = "<<EXCELCOST>>"
ReplacementText2 = Range(cellno).Value
wrdDoc.Content.Find.Execute FindText2, ReplaceWith:=ReplacementText2, Replace:=wdReplaceAll

cellno = "Main!C20"
FindText3 = "<<EXCELDEST>>"
ReplacementText3 = Range(cellno).Value
wrdDoc.Content.Find.Execute FindText3, ReplaceWith:=ReplacementText3, Replace:=wdReplaceAll

NextNumber:

FileAddress = Range("Main!C21").Text
FileAddress = "C:\Cost Statement pdfs\" & FileAddress & ".docx"

With wrdDoc

.SaveAs (FileAddress)
.Close

End With

wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing

End Sub

Any help would be appreciated.

Upvotes: 0

Views: 88

Answers (1)

TourEiffel
TourEiffel

Reputation: 4414

Did you try this way?

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:="C:\PathPDF.pdf"

also note that

FileAddress = Range("Main!C21").Text
FileAddress = "C:\Cost Statement pdfs\" & FileAddress & ".docx"

is the same as

FileAddress = "C:\Cost Statement pdfs\" & FileAddress & ".docx"

Upvotes: 1

Related Questions