Forum Discussion

leandropoblet's avatar
leandropoblet
Frequent Contributor
9 years ago

Need to save an .rtf file as .pdf using MS Word. Which version would you recommend?

Hi there,   I tried recording scripts on MS Word 2010 64 and 32 bits and non of them work. I also tried Office 365 although I knew it wouldn't.   Any suggestions? I only need to open a file, sa...
  • HKosova's avatar
    HKosova
    9 years ago

    Hi Leandro,

     

    All Microsoft Office products provide an API in the form of COM objects that let you automate all kinds of things, including saving files to a different format. Word's COM object is Word.Application. You can use the Application.Documents.Open(FileName) method to open a file and the Document.SaveAs(FileName, FileFormat) method to save it to a specific format. Here's an example:

    // JScript
    function Test()
    {
      RTF2PDF("C:\\Work\\Example.rtf", "C:\\Work\\Example.pdf");
    }
    
    function RTF2PDF(RTFFileName, PDFFileName)
    {
      var wdFormatPDF = 17;
      var oWord = Sys.OleObject("Word.Application");
      var oDoc = oWord.Documents.Open(RTFFileName);
      oDoc.SaveAs(PDFFileName, wdFormatPDF);
      oDoc.Close();
      oWord.Quit();
    }

    This works for me in Word 2010.

     

    Helpful links:

    Word object model reference

    Understanding the Word Object Model