VBA: Within Excel how do I save a word document as a PDF?

user4227507

I have some code that copies and pastes data from an Excel file into a word document, how can I get that word document to save as a PDF?

My code is as follows:

Sub RetailerGraphs()

Dim Location As String
Dim Detail As String

Worksheets("-Summary").Activate
Range("AE9").Select
While ActiveCell.Value <> ""
    ActiveCell.Offset(1, 0).Select
    Location = ActiveCell.Value
    Worksheets("Detail Summary").Activate
    Range("B7").Value = Location

    Dim objWord, objDoc As Object
    ActiveWindow.View = xlNormalView
    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Add

    Range("AH40").Select
    While ActiveCell <> ""
        ActiveCell.Offset(1, 0).Select
        Detail = ActiveCell.Value
        Range("B11").Value = Detail 
        Application.Wait (Now + TimeValue("0:00:05"))
        Range("A1:Z111").CopyPicture Appearance:=xlScreen, Format:=xlPicture
        objWord.Visible = True
        objWord.Selection.Paste
        objWord.Selection.TypeParagraph
        Set objSelection = objWord.Selection
        objSelection.InsertBreak (wdPageBreak)
        If ActiveCell.Value = "END" Then
             objWord.SaveAs2 "C:\Docs\MyDoc.pdf"
        End If
    Wend
    Worksheets("-Summary").Activate
Wend

End Sub

Within the lines:

If ActiveCell.Value = "END" Then

End If

I have tried the following code to try to get it to save as a PDF:

 objWord.ExportAsFixedFormat OutputFileName:="C:\wordtest.pdf", _
  ExportFormat:=wdExportFormatPDF

 objWord.ExportAsFixedFormat OutputFileName:="C:\Documents and Settings\All Users\Desktop\YourFile.pdf", ExportFormat:=wdExportFormatPDF

 objDoc.ExportAsFixedFormat OutputFileName:="C:\Documents and Settings\All Users\Desktop\YourFile.pdf", ExportFormat:=wdExportFormatPDF

But I get the error on the line where I try to export it as a PDF, e.g. objWord.SaveAs2 "C:\Docs\MyDoc.pdf" :

Run-time error '438':
Object doesn't support this property or method

Can someone please help?

Note: I have ensured that "Microsoft Word 14.0 Object Library" is ticked in the references, and I change the location to the necessary directory rather than using the above.

Andre

ExportAsFixedFormat is a method of Word.Document (objDoc), not Word.Application (objWord). So

objDoc.ExportAsFixedFormat OutputFileName:="C:\Docs\File.pdf", ExportFormat:=wdExportFormatPDF

should work.

Does this give an error too? Which?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I search for a word in Excel Using VBA and then Delete the entire row?

From Dev

How do I change text in a Word Doc from Excel VBA when Word Doc is located on a server?

From Dev

Save embedded word document from Excel VBA

From Dev

Save embedded word document from Excel (2007) VBA

From Dev

How do I store a variable permanently in a Word 2010 document using VBA?

From Dev

Excel VBA quit Word document

From Dev

How do I search for a word within repository?

From Dev

Powershell - How can I save a single page from a word document?

From Dev

Save Word document as PDF, how to prevent creation of hyperlinks in PDF?

From Dev

How do I search for a word in Excel Using VBA and then Delete the entire row?

From Dev

How do I make a hyperlink within a Word document to another place in the same document?

From Dev

How do I access a new document in vba?

From Dev

how do i save a excel spreadsheet after editing it in access 2007 in VBA

From Dev

How to save html document as pdf?

From Dev

How do I save ut8 encoding with excel vba macro

From Dev

How do I create a macro in excel that will save the current document to a specific location

From Dev

How can I add obfuscating stripes to a PDF or Word document?

From Dev

How can I save a Word document as a PDF *without* any embedded fonts?

From Dev

How do you loop columns within a range in Excel VBA

From Dev

How do I insert a table into a cell using word vba or a table within a table?

From Dev

How do I increment a variable that has a word within the variable name?

From Dev

How do I search for a word within repository?

From Dev

How can I convert images to pdf and Word document in uwp?

From Dev

How do I make sure that word converts the right version of my document to PDF?

From Dev

How do I save the results from an input line to a document in HTML?

From Dev

How do I path to a Python install using an Environment Variable from within an Excel / VBA macro?

From Dev

VBA print/export/save all Excel workbook tabs into one PDF document

From Dev

Open and Close Word Document from VBA Excel

From Dev

Using Excel VBA, how to search for a specific word and insert comments against that word in a Word document?

Related Related

  1. 1

    How do I search for a word in Excel Using VBA and then Delete the entire row?

  2. 2

    How do I change text in a Word Doc from Excel VBA when Word Doc is located on a server?

  3. 3

    Save embedded word document from Excel VBA

  4. 4

    Save embedded word document from Excel (2007) VBA

  5. 5

    How do I store a variable permanently in a Word 2010 document using VBA?

  6. 6

    Excel VBA quit Word document

  7. 7

    How do I search for a word within repository?

  8. 8

    Powershell - How can I save a single page from a word document?

  9. 9

    Save Word document as PDF, how to prevent creation of hyperlinks in PDF?

  10. 10

    How do I search for a word in Excel Using VBA and then Delete the entire row?

  11. 11

    How do I make a hyperlink within a Word document to another place in the same document?

  12. 12

    How do I access a new document in vba?

  13. 13

    how do i save a excel spreadsheet after editing it in access 2007 in VBA

  14. 14

    How to save html document as pdf?

  15. 15

    How do I save ut8 encoding with excel vba macro

  16. 16

    How do I create a macro in excel that will save the current document to a specific location

  17. 17

    How can I add obfuscating stripes to a PDF or Word document?

  18. 18

    How can I save a Word document as a PDF *without* any embedded fonts?

  19. 19

    How do you loop columns within a range in Excel VBA

  20. 20

    How do I insert a table into a cell using word vba or a table within a table?

  21. 21

    How do I increment a variable that has a word within the variable name?

  22. 22

    How do I search for a word within repository?

  23. 23

    How can I convert images to pdf and Word document in uwp?

  24. 24

    How do I make sure that word converts the right version of my document to PDF?

  25. 25

    How do I save the results from an input line to a document in HTML?

  26. 26

    How do I path to a Python install using an Environment Variable from within an Excel / VBA macro?

  27. 27

    VBA print/export/save all Excel workbook tabs into one PDF document

  28. 28

    Open and Close Word Document from VBA Excel

  29. 29

    Using Excel VBA, how to search for a specific word and insert comments against that word in a Word document?

HotTag

Archive