none
Programmatically convert Office 2013 Word docx to PDF RRS feed

  • Question

  • Back in Office 2007/2010, I had a piece of C# code that used the Microsoft.Office.Interop.Word assembly reference and I would convert a Word doc into a PDF like so:

    //Creating objects of word and document
    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();

    ..other code, then...

     oWordDoc = oWord.Documents.Open(ref pptPrintFilePath, ref missing,
                        ref readOnly, ref missing, ref missing,
                        ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing,
                        ref isVisible, ref missing, ref missing,
                        ref missing, ref missing);

                    if (oWordDoc != null)
                    {

                        oWordDoc.ExportAsFixedFormat(pdfFilePath,
                            WdExportFormat.wdExportFormatPDF,
                            false,
                            WdExportOptimizeFor.wdExportOptimizeForPrint,
                            WdExportRange.wdExportAllDocument,
                            0,
                            0,
                            WdExportItem.wdExportDocumentContent,
                            true,
                            true,
                            WdExportCreateBookmarks.wdExportCreateNoBookmarks,
                            true,
                            true,
                            false,
                            ref missing);
                    }

    As time marches on, we are in Office 2013 but there is no .Interop .dll.

    How would this be accomplished in Office 2013?

    Thanks!


    STom

    Sunday, September 1, 2013 5:35 PM

Answers

  • Microsoft Office 15.0 Object Library would be one, Microsoft Word 15.0 Object Library would be the other that you'd certainly need. I don't know if you're working with anything else...

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by stombiztalker Tuesday, September 3, 2013 12:39 PM
    Tuesday, September 3, 2013 10:31 AM
    Moderator

All replies

  • Office 2013 has an .Interop DLL, same as previous versions, unless you're looking at the version running on Windows RT or the web app...

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 2, 2013 3:43 PM
    Moderator
  • Hi Cindy,

    Do I need to install an SDK or some part of Visual Studio to see this assembly? I can't seem to find it.

    Thanks!


    STom

    Monday, September 2, 2013 5:08 PM
  • You need to install Office 2013. The PIAs are in the GAC, meaning you need to look for them in the COM (not the .NET) tab.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 3, 2013 6:48 AM
    Moderator
  • Yes, I have Office 2013 installed.

    Within Visual Studio if I go to add a reference, choose the COM tab and type in 'Office' I see:

    Microsoft Office 14.0 Object Library v2.5
    Microsoft Office 15.0 Access Data Engine Object Library v12.0
    Microsoft Office 15.0 Object library v2.7
    Microsoft Office List 14.0 (v1.0, 2.0, 3.0)
    Microsoft Office Project 15. Object Library v4.8
    ..some other Office ones for VS, TFS, Communicator
    TFCOfficeShim 3.0 (and 4.0 Type Library)

    If I search on Interop, I see:
    InteropExtension 1.0 Type Library

    What assemblies would a project like this require? I used to use Microsoft.Office.Core and then the Interop.Word assembly.

    Thanks!


    STom

    Tuesday, September 3, 2013 10:24 AM
  • Microsoft Office 15.0 Object Library would be one, Microsoft Word 15.0 Object Library would be the other that you'd certainly need. I don't know if you're working with anything else...

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by stombiztalker Tuesday, September 3, 2013 12:39 PM
    Tuesday, September 3, 2013 10:31 AM
    Moderator
  • Dear sir

    if your operating system is 64 bit and installed office is 32 bit will not work , you have to install office 64 bit

    ___________________________________________________________________________________
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers
    Tuesday, September 3, 2013 10:36 AM
  • Yes, my OS is 64bit and the version of Office is 32bit.

    If I install Office 64bit, does that mean that whoever runs the application also has to have 64bit installed?

    Thanks!


    STom

    Tuesday, September 3, 2013 12:31 PM
  • Hi Cindy,

    The code seems to be working fine now, even though I'm using 32bit office on a 64bit OS.

    Thanks!


    STom

    Tuesday, September 3, 2013 12:39 PM
  • Dear

    you mean code working now could you provide us with solution 

    Tuesday, September 3, 2013 1:39 PM
  • Here is the C# code:

    using

    System;

    using

    System.Collections.Generic;

    using

    System.Linq;

    using

    System.Text;

    using

    System.IO;

    using

    System.Xml.Xsl;

    using

    System.Diagnostics;

    using

    Microsoft.Office;

    using

    Microsoft.Office.Interop.Word;

     

     

    namespace

    ConvertWordToPDF

    {

    class Program

    {

    static void Main(string[] args)

    {

    #region

    Set Colors

    //set background colors

    Console.Title = "Convert Word to PDF Utility";

    Console.BackgroundColor = ConsoleColor.DarkGreen;

    Console.ForegroundColor = ConsoleColor.Yellow;

    //Console.Clear();

    #endregion

    //get the current root directory. For the WCF workshop, this

    //exe is expected to be in the root directory

    string strCurrDir = System.IO.Directory.GetCurrentDirectory();

    //this is the path for the source Word file

    object pptPrintFilePath = strCurrDir + "\\" + args[0];

    //this is the path where we want to store the .pdf file

    string pdfFilePath = strCurrDir + "\\" + args[1];

    //Create an object for missing values. This will be passed when ever we don’t want to pass value

    Object missing = Type.Missing;

    Console.WriteLine("Exporting {0} to {1}", args[0], args[1]);

    //Creating objects of word and document

    Microsoft.Office.Interop.Word.

    Application oWord =

    new Microsoft.Office.Interop.Word.Application();

     

    Microsoft.Office.Interop.Word.

    Document oWordDoc =

    new Microsoft.Office.Interop.Word.Document();

    try

    {

     

    // You can keep it true if you want to open the file in readonly mode

    object readOnly = false;

    // we can keep it false if you want to open the file but not make it invisible

    object isVisible = true;

    oWordDoc = oWord.Documents.Open(

    ref pptPrintFilePath, ref missing,

    ref readOnly, ref missing, ref missing,

    ref missing, ref missing, ref missing,

    ref missing, ref missing, ref missing,

    ref isVisible, ref missing, ref missing,

    ref missing, ref missing);

    if (oWordDoc != null)

    {

    oWordDoc.ExportAsFixedFormat(pdfFilePath,

    WdExportFormat.wdExportFormatPDF,

    false,

    WdExportOptimizeFor.wdExportOptimizeForPrint,

    WdExportRange.wdExportAllDocument,

    0,

    0,

    WdExportItem.wdExportDocumentContent,

    true,

    true,

    WdExportCreateBookmarks.wdExportCreateNoBookmarks,

    true,

    true,

    false,

    ref missing);

    }

    }

    catch (Exception ex)

    {

    // Respond to the error

    Console.WriteLine("Exception occurred : {0}",ex.Message);

    }

    finally

    {

    if (oWordDoc != null)

    {

    oWordDoc.Close(

    ref missing, ref missing, ref missing);

    oWord.Quit(

    ref missing, ref missing, ref missing);

    oWordDoc =

    null;

    }

    }

    Console.WriteLine("Cleaning up.....please wait");

    }

    }

    }


    STom

    Tuesday, September 3, 2013 2:07 PM
    • Edited by matcooper Monday, December 16, 2013 2:47 AM
    Monday, December 16, 2013 2:47 AM
  • Many thanks!!!!!
    Tuesday, February 25, 2014 3:34 PM
  • Hi,

    I have tried your code, unfortunately after a windows update was installed (and after some research)

    I found that the conversion to pdf has been disabled creating this error

    at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks)

    could you please shed some light on this?

    Regards,

    Adnane

    Thursday, October 23, 2014 2:40 PM