+ Reply to Thread
Results 1 to 8 of 8

How to save Word documents as PDFs using Excel macro?

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2017
    Location
    Texas
    MS-Off Ver
    14.7.1
    Posts
    4

    How to save Word documents as PDFs using Excel macro?

    Hi all,

    I am having trouble saving Word documents that I am opening and manipulating through an Excel macro as a .pdf. I've tried several ways and it is not working.
    Right now it kind of works, but it asks me to save the changes for each file. I am looping this macro through several files so this is not convenient for me. Thanks for your help!

    Dim wdApp As Object
    Dim wdDoc As Object
    Dim fileloc As String
        
    fileloc = "Macintosh HD:Users:X:Y:Z.docx" 'Replaced folder names with X Y Z for simplicity
        
    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open _
          (Filename:=fileloc)
    
    ***      
    *several lines of code where I manipulate the word doc*
    ***
    
    wdDoc.SaveAs Filename:="Macintosh HD:Users:X:Y:ZZ.docx" 
    
    'Saves files as pdf but asks to accept for each file    
    With Dialogs(wdDialogFileSaveAs)
         .Format = wdFormatPDF
         .Show
    End With
    
    wdDoc.Close savechanges:=False
    Set wdDoc = Nothing
    wdApp.Quit
    Set wdApp = Nothing
    I have tried using wdDoc.ExportAsFixedFormat a few different ways and it hasn't worked.

        wdDoc.ExportAsFixedFormat OutputFileName:= _
            Replace(wdDoc.FullName, ".docx", ".pdf"), _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
            wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
      wdDoc.SaveAs2 Filename:="Text.pdf", _
            FileFormat:=wdFormatPDF
     
    wdDoc.ExportAsFixedFormat OutputFileName:="Macintosh HD:Users:X:Y:ZZ.pdf", _
          ExportFormat:=wdExportFormatPDF
    EDIT: I do have the Microsoft Word Object Library enabled in references on excel
    Last edited by utexas24; 01-22-2017 at 06:40 PM.

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: How to save Word documents as PDFs using Excel macro?

    My guess is do not even neet the Object Library, furthermore, are working on a MAC? Then mention this!

    Have you tried recording a macro to save one file?
    You can then edit the macro to your neeeds
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: How to save Word documents as PDFs using Excel macro?

    I wonder if you even Googled for an answer:
    http://stackoverflow.com/questions/1...at-alternative
    http://ideophone.org/one-click-save-...007-2010-2013/
    Try one of the above links

  4. #4
    Registered User
    Join Date
    01-22-2017
    Location
    Texas
    MS-Off Ver
    14.7.1
    Posts
    4

    Re: How to save Word documents as PDFs using Excel macro?

    Yes I did google. The second link is where I found the code I currently have (with the Save as window). From this same link I tried "No questions asked" version (the code is written up there in my original question) but I can't get it to work. I have tried copying the exact code and also tried it by replacing "ActiveDocument" with my object (wdDoc) but I get the same error "Object doesn't support this property or method"

  5. #5
    Registered User
    Join Date
    01-22-2017
    Location
    Texas
    MS-Off Ver
    14.7.1
    Posts
    4

    Re: How to save Word documents as PDFs using Excel macro?

    It is in my question title but I did not write it in the description...the macro is written in Excel, it is not a macro in Word.

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: How to save Word documents as PDFs using Excel macro?

    Like I said, have you tried recording a macro?
    Well I did with Word (Windows) and this is the result

    Sub Save2PDF()
    '
    ' Save2PDF Macro
    '
    '
    Dim myFolder    As String
    Dim myFile      As String
    
    '*  if you wqnt to use the current document's path
    '*  myFolder = ActiveDocument.Path
    
    myFolder = "G:\Temp"
    
    myFile = ActiveDocument.Name
        
        ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            myFolder & Application.PathSeparator & Replace(myFile, ".docx", ".pdf"), ExportFormat:=wdExportFormatPDF, _
            OpenAfterExport:=False, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
            wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
            IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
            wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
            True, UseISO19005_1:=False
        ChangeFileOpenDirectory myFolder
    End Sub
    It works here, no prompts nothing

  7. #7
    Registered User
    Join Date
    01-22-2017
    Location
    Texas
    MS-Off Ver
    14.7.1
    Posts
    4

    Re: How to save Word documents as PDFs using Excel macro?

    Ok the problem is definitely that I am on a Mac...because there is no export .PDF button. The only option are "Print as PDF" which when I record gives me this:

        Application.PrintOut fileName:="", Range:=wdPrintAllDocument, Item:= _
            wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
            Collate:=True, Background:=False
    But when I run it as a macro it tries to actually print the document to a printer, not as .PDF.

    The other option is to do File...Save as... .PDF but when I record this I do not get anything besides this:

        ChangeFileOpenDirectory _
            "Macintosh HD:Users:X:Y:Z:"
    Which I think is just changing where the file is saved, not the actual option of a .PDF.

    The only way I have gotten so far is using:
    With Dialogs(wdDialogFileSaveAs)
         .Format = wdFormatPDF
         .Show
    End With
    However I need to select "Save" each time. And when I try to loop it through several files, it is able to do the first one but has an error on the next one.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: How to save Word documents as PDFs using Excel macro?

    It's not a button it's VBA code
    My guess if that it sholud be there too, but .... a MAC is like Chineese for me

+ 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. Excel VBA to open/save MS Word documents
    By Summer7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2017, 10:59 AM
  2. Macro to save documents generated with word/excel automatically
    By kees89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2014, 02:17 PM
  3. [SOLVED] Macro for mail merge and save as word documents
    By aganesan99 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2013, 09:16 AM
  4. Using Excel Macro to Print Word Documents
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-26-2013, 01:15 AM
  5. Macro to save excel worksheets as pdfs and change hyperlinks to work in pdf format
    By crnadeau4 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2013, 12:18 PM
  6. Macro Needed to Loop, Save, and Name Excel Files as PDFs
    By ahkarchem in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-11-2012, 10:46 AM
  7. Open and Save Word Documents via Excel
    By abousetta in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-22-2010, 02:54 PM

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