+ Reply to Thread
Results 1 to 6 of 6

VBA Code to Convert/Print Selected Pages of Word Document to PDF File

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2005
    Location
    Canberra, Australia
    MS-Off Ver
    2007
    Posts
    43

    VBA Code to Convert/Print Selected Pages of Word Document to PDF File

    The code below creates a PDF file from Excel of a Word document. The name of the file is exactly the same except it has a 'pdf' extension rather than 'docx'. The ", 17" at the end of the last line of code gives it the 'pdf' extension.
    The file is 3 pages.
    The main thing I would like to do is create 2 PDF files - one is of just the first page and the other is of pages 2-3.
    I don't know what the code is to specify page ranges.
    The other thing I would like to do is suppress the message that comes up when the second from last line of code is executed - i.e. the one that opens the Word file. It currently says the file is locked for editing and has 3 options for opening the file. I just want to open the file as read-only (which is option 1).
    Also can you tell me how to add this code properly to the thread? At the moment I have just pasted it in from the VBA editor.

    Many thanks
    David

    Sub Create_PDF_File_of_Word_Document()

    Dim wordFile As Object
    Set wordFile = CreateObject("Word.Application")

    Directory_Path = "C:\Data"
    Filename = "Jul-15 Variance Analysis.docx"

    Set wordDoc = wordFile.Documents.Open(Directory_Path & "\" & Filename)

    wordDoc.ExportAsFixedFormat Left(wordDoc.FullName, InStrRev(wordDoc.FullName, ".") - 1), 17

    End Sub

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: VBA Code to Convert/Print Selected Pages of Word Document to PDF File

    You really should look up Word's ExportAsFixedFormat and Open methods for this. You should also declare all variables and, presumably, close the document and quit Word when you're done.

    Try:
    Sub Create_PDF_File_of_Word_Document()
    Dim wdApp As Object, wdDoc As Object, StrPath As String, StrFlNm As String
    StrPath = "C:\Data\"
    StrFlNm = "Jul-15 Variance Analysis.docx"
    Set wdApp = CreateObject("Word.Application")
    With wdApp
      '.Open FileName:=StrPath & StrFlNm , ReadOnly:=True, AddToRecentFiles:=False
      Set wdDoc = .Documents.Open(StrPath & StrFlNm, True, False)
      With wdDoc
        'ExportAsFixedFormat Outputfilename:=.FullName, Exportformat:=wdExportFormatPDF, _
        'OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=1, To:=1
        .ExportAsFixedFormat Split(.FullName, ".doc")(0) & "A.pdf", 17, 0, False, 3, 1, 1
        .ExportAsFixedFormat Split(.FullName, ".doc")(0) & "B.pdf", 17, 0, False, 3, 2, 3
        .Close
      End With
      .Quit
    End With
    Set wdDoc = Nothing: Set wdApp = Nothing
    End Sub
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Registered User
    Join Date
    07-12-2005
    Location
    Canberra, Australia
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Code to Convert/Print Selected Pages of Word Document to PDF File

    Thank you Paul. I will give this a go when I get to work tomorrow and let you know how I get on.
    Cheers
    David

  4. #4
    Registered User
    Join Date
    07-12-2005
    Location
    Canberra, Australia
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Code to Convert/Print Selected Pages of Word Document to PDF File

    Thank you Paul. That worked perfectly.

    If the Word document had more than 3 pages, is it possible when creating the second PDF file to print from page 2 to the end of the document?
    I had a look at Word's ExportAsFixedFormat method but couldn't see that it was possible. I tried a couple of things, but wasn't able to get it to work.

    Also, is it possible to append one pdf file to another with VBA? I would like to do this from Excel.
    I can do it manually, but can't work out what the code is.

    Thanks for your help Paul.
    Regards,
    David

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,835

    Re: VBA Code to Convert/Print Selected Pages of Word Document to PDF File

    Quote Originally Posted by DD1 View Post
    If the Word document had more than 3 pages, is it possible when creating the second PDF file to print from page 2 to the end of the document?
    I had a look at Word's ExportAsFixedFormat method but couldn't see that it was possible. I tried a couple of things, but wasn't able to get it to work.
    That's easy: simply replace the '3' with:
    .ComputeStatistics(2) '2 = wdStatisticPages
    Quote Originally Posted by DD1 View Post
    Also, is it possible to append one pdf file to another with VBA? I would like to do this from Excel.
    I can do it manually, but can't work out what the code is.
    Not from MS Office. You could, however, automate Adobe Acrobat Pro and have it do that. Adobe Acrobat Pro 8 is available as a free download. I don't know the relevant Adobe code to combine files, but people on an Adobe forum should be able to tell you that (or a web search might turn up the code).

  6. #6
    Registered User
    Join Date
    07-12-2005
    Location
    Canberra, Australia
    MS-Off Ver
    2007
    Posts
    43

    Re: VBA Code to Convert/Print Selected Pages of Word Document to PDF File

    Thanks a lot Paul. That works brilliantly.
    I really appreciate your help.
    Regards,
    David

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA Code to Convert/Print Selected Pages of Word Document to PDF File
    By DD1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2016, 01:59 AM
  2. Replies: 0
    Last Post: 05-29-2015, 02:25 AM
  3. With excel to print a selected page from a word document
    By eb4927 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-15-2014, 05:54 PM
  4. VBA code to merge text file in a word document and print all the letters automatically
    By Rajbrinder in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-26-2013, 08:25 AM
  5. Print off the word document using excel vba code
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2013, 09:42 AM
  6. Printing selected pages in a Word document
    By Tristan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2006, 08:45 PM
  7. Replies: 0
    Last Post: 02-28-2005, 06:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1