Word OLE Delphi

Top  Previous  Next

The Word OLE Delphi sample is a simple Windows console application that converts a MS Word document (C:\Test.doc) to PDF using Word OLE automation.

 

Source code

 

program WordOLEDelphi;

 

{$APPTYPE CONSOLE}

 

uses

  ActiveX,

  Printers,

  ComObj,

  SysUtils,

  Dialogs,

  novaOptions,

  novapiLIB_TLB;

 

Const

 

  //name of novaPDF Printer

  PRINTER_NAME    = '<%SDK_SAMPLE_PRINTER%>';

 

  //Print profile name

  PROFILE_NAME    = 'Test OLE Delphi Profile';

  PROFILE_IS_Public = 0;

 

var

  hr : HRESULT;

  pNova : INovaPdfOptions;

  strDefaultProfile : WideString;

  WORD : VARIANT;

  NewDoc : VARIANT;

  bPublicProfile: INTEGER;

 

BEGIN

  //initialize COM

  hr := ActiveX.CoInitialize(NIL);

  IF (FAILED (hr)) then BEGIN

    System.Writeln('Failed to initialize COM');

    EXIT;

  END;

 

  //create one NovaPdfOptions instance

  pNova := NIL;

  hr := ActiveX.CoCreateInstance(

              CLASS_NovaPdfOptions, //CLSID_CNovaPdfSource,

              NIL,

              CLSCTX_INPROC_SERVER,

              IID_INovaPdfOptions,

              pNova);

  IF (FAILED(hr))    then BEGIN

    System.Writeln('Failed to create novaPDF COM object');

    EXIT;

  END;

 

  //initialize NovaPdfOptions and pass printer name

  //if you have an application license for novaPDF SDK,

  //pass both the registration name and the license key to the Initialize2() function

  //hr := pNova.Initialize2( PRINTER_NAME, '<registration name>', '<license key>');

  hr := pNova.Initialize2( PRINTER_NAME, '', '', '' );

 

  IF (SUCCEEDED(hr)) then BEGIN

 

    pNova.SetDefaultPrinter();

    // now the default printer is novaPDF printer but the Printer object is not updated

    // here is a workaround to update the Printer object with the default printer

    // you only need this code if you check later on the Printer.PrinterIndex to find out the default printer

    //Printer.GetPrinter(Device, Driver, Port, DevMode);

    //Printer.SetPrinter(PRINTER_NAME, Driver, Port, 0);

 

    // set optional PDF settings

    // create a temporary profile for the current print job,

    // in order to not modify the default profile settings

    pNova.AddProfile2(PROFILE_NAME, PROFILE_IS_PUBLIC);

    // set PDF document Title

    pNova.SetOptionString2(NOVAPDF_INFO_TITLE,

                           'Hello World Delphi Sample', PROFILE_NAME, PROFILE_IS_PUBLIC);

    // set resulting file name

    pNova.SetOptionString2(NOVAPDF_SAVE_FOLDER, 'C:\', PROFILE_NAME, PROFILE_IS_PUBLIC);

    //do not show prompt dialog

    pNova.SetOptionLong2(NOVAPDF_SAVE_PROMPT, 0, PROFILE_NAME, PROFILE_IS_PUBLIC);

    //if file exists, override

    pNova.SetOptionLong2(NOVAPDF_SAVE_CONFLICT_STRATEGY,

                         FILE_CONFLICT_STRATEGY_OVERWRITE,

                         PROFILE_NAME, PROFILE_IS_PUBLIC);

    //open document in PDF viewer

    pNova.SetOptionLong2(NOVAPDF_ACTION_OPEN_DOCUMENT, 1, PROFILE_NAME, PROFILE_IS_PUBLIC);

    // set active profile

    strDefaultProfile := '';

    pNova.GetActiveProfile2(strDefaultProfile, bPublicProfile);

    pNova.SetActiveProfile2(PROFILE_NAME, PROFILE_IS_PUBLIC);

 

    //Print Word Document

    try

      pNova.InitializeOLEUsage('Word.Application');

      WORD := CreateOleObject('Word.Application');

      WORD.DisplayAlerts := 0;

      pNova.LicenseOLEServer();

      NewDoc:= WORD.Documents.Open('C:\Test.doc', FALSE, TRUE);

      NewDoc.PrintOut(FALSE);

      NewDoc.Close(FALSE);

      WORD.Quit(FALSE);

    except

      on E: Exception DO

        ShowMessage(E.Message);

    END;

 

    //restore default profile

    pNova.SetActiveProfile2(strDefaultProfile, bPublicProfile);

    pNova.DeleteProfile2(PROFILE_NAME, PROFILE_IS_PUBLIC);

    //restore default printer

    pNova.RestoreDefaultPrinter();

  END ELSE BEGIN

    System.Writeln('Failed to initialize novaPDF Printer');

  END;

 

  ActiveX.CoUninitialize();

 

END.