none
macro to convert documents into pdf/a RRS feed

  • Question

  • Hello,

    Is their any macro to convert documents(word,excel) into pdf/a file format?

    Thanks in advance

    Tuesday, October 30, 2012 7:46 AM

Answers

  • Hi Mikepras,

    I've figured out the below code for you.

    Sub SaveAsPdf()
        If Application.Name = "Microsoft Word" Then
            Dim doc As Word.Document
            Set doc = Word.Application.ActiveDocument
            
            Dim docName As String
            docName = doc.Path + "\" + doc.Name + ".pdf"
            
            doc.SaveAs2 docName, wdFormatPDF
        End If
        If Application.Name = "Microsoft Excel" Then
            Dim wbk As Excel.Workbook
            Set wbk = Excel.Application.ActiveWorkbook
            
            Dim fName As String
            fName = wbk.Path + "\" + wbk.Name + ".pdf"
            
            wbk.ExportAsFixedFormat xlTypePDF, fName
        End If
        
    End Sub

    In order to get this code compiled, in word you'll need to add reference to Excel Object Library while in Excel you'll need to add reference to Word Object Library.

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Mikepras Wednesday, October 31, 2012 1:34 PM
    Wednesday, October 31, 2012 10:28 AM
    Moderator

All replies

  • Check out this link, see if it helps.

    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f8989c05-d04a-4b4a-be0f-fc0055691de7/


    If you want something you've never had, you need to do something you've never done.
    Everyone(not just the thread starter) should take the time to mark helpful posts, propose answers, and mark answers to questions.

    Tuesday, October 30, 2012 8:31 AM
  • Hi Mikepras,

    Thank you for posting in the MSDN Forum.

    For word documents, you can have the below code

    Sub SaveAsPdf()
        
        Dim doc As Word.Document
        Set doc = Application.ActiveDocument
        
        Dim docName As String
        docName = doc.Path + "\" + doc.Name + ".pdf"
        
        doc.SaveAs2 docName, wdFormatPDF
        
    End Sub

    For Excel Workbook

    Sub SaveAsPdf()
        
        Dim wbk As Excel.Workbook
        Set wbk = Application.ActiveWorkbook
        
        Dim fName As String
        fName = wbk.Path + "\" + wbk.Name + ".pdf"
        
        wbk.ExportAsFixedFormat xlTypePDF, fName
        
    End Sub

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, October 31, 2012 3:07 AM
    Moderator
  • Thanks a lot Quist,

    I tried to mix up both the macros into one to save any document (excel or word) into pdf, but not succeed. Request you to provide your guidance on the same.

    Thanks in advance!

    Wednesday, October 31, 2012 9:06 AM
  • Hi Mikepras,

    I've figured out the below code for you.

    Sub SaveAsPdf()
        If Application.Name = "Microsoft Word" Then
            Dim doc As Word.Document
            Set doc = Word.Application.ActiveDocument
            
            Dim docName As String
            docName = doc.Path + "\" + doc.Name + ".pdf"
            
            doc.SaveAs2 docName, wdFormatPDF
        End If
        If Application.Name = "Microsoft Excel" Then
            Dim wbk As Excel.Workbook
            Set wbk = Excel.Application.ActiveWorkbook
            
            Dim fName As String
            fName = wbk.Path + "\" + wbk.Name + ".pdf"
            
            wbk.ExportAsFixedFormat xlTypePDF, fName
        End If
        
    End Sub

    In order to get this code compiled, in word you'll need to add reference to Excel Object Library while in Excel you'll need to add reference to Word Object Library.

    Hope it helps.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Mikepras Wednesday, October 31, 2012 1:34 PM
    Wednesday, October 31, 2012 10:28 AM
    Moderator
  • Hello Quist,

    Thanks a lot! This will definitely work for me.

    Thanks again.

    Wednesday, October 31, 2012 12:01 PM
  • Hello Quist,

    I have made a macro which connect with the AS400 database and fetch data in excel or xml file format.

    But, I have few issues in that, if source file have multilingual data, thenin excel instead of correct data it shows small boxes.

    Please suggest something.

    Thanks!

    Wednesday, October 31, 2012 1:36 PM
  • Hi Mikepras,

    I've replied you in the other thread of you.

    Have a nice day.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, November 1, 2012 1:54 AM
    Moderator
  • Hi Quist Zhang,

    I tried with this Macro for conversion, My word file was converted into PDF But the problem is, The PDF file did not open after the conversion. Is there any code to open the file automatically after the conversion?

    Thanks in Advance.

    Monday, January 23, 2017 11:40 AM