Limited time promo

Delphi Sample - Word OLE Delphi

English

This help topic applies only to novaPDF. If you don't have it yet, you must download it first.

Download now Buy licenses
Mar 29, 2022
novaPDF 11.x
We strive to keep our help as accurate as possible. If you notice any inconsistencies or outdated info please let us know.
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 in '..\..\..\include\novaOptions.pas',
  novapiLIB80_TLB in '..\..\..\include\novapiLIB11_TLB.pas';

const

  //name of novaPDF Printer
  PRINTER_NAME      = 'novaPDF SDK 11';

  //Print profile name
  PROFILE_NAME      = 'Test OLE Delphi Profile';
  PROFILE_IS_PUBLIC = 0;

var
  hr : HRESULT;
  pNova : INovaPdfOptions11;
  strOldActiveProfileID : WideString;
  strNewProfileID : WideString;
  Word : VARIANT;
  NewDoc : VARIANT;
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_NovaPdfOptions11, //CLSID_CNovaPdfSource,
                                 nil,
                                 CLSCTX_INPROC_SERVER,
                                 IID_INovaPdfOptions11,
                                 pNova);
  if (FAILED(hr))   then begin
    System.Writeln('Failed to create novaPDF COM object');
    exit;
  end;

  //initialize NovaPdfOptions and pass printer name
  pNova.Initialize2( PRINTER_NAME, '' );

  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);

  //create a new profile with default settings
  pNova.AddProfile2(PROFILE_NAME, PROFILE_IS_PUBLIC, strNewProfileID);

  //load the newly created profile
  if (Length(strNewProfileID) > 0) then begin
    pNova.LoadProfile2(strNewProfileID);
  end else begin
    System.Writeln('Failed to create profile');
    exit;
  end;

  // set PDF document Title
  pNova.SetOptionString2( NOVAPDF_DOCINFO_TITLE, 'Hello World Delphi Sample');
  // set resulting file name
  pNova.SetOptionString2(NOVAPDF_SAVE_FOLDER, 'C:\');
  //do not show prompt dialog
  pNova.SetOptionLong(NOVAPDF_SAVE_PROMPT_TYPE, PROMPT_SAVE_NONE);
  //if file exists, override
  pNova.SetOptionLong(NOVAPDF_SAVE_FILEEXIST_ACTION, FILE_CONFLICT_STRATEGY_OVERWRITE);
  //do not open document in PDF viewer
  pNova.SetOptionBool(NOVAPDF_ACTION_DEFAULT_VIEWER, 1);

  //save profile changes
  pNova.SaveProfile();

  //get current default profile id
  pNova.GetActiveProfile2(strOldActiveProfileID);
  //set as active profile for printer
  pNova.SetActiveProfile2(strNewProfileID);

  //Print Word Document
  try
    pNova.InitializeOLEUsage('Word.Application');
    Word := CreateOleObject('Word.Application');
    Word.DisplayAlerts := 0;
    pNova.LicenseOLEServer();
    NewDoc:= Word.Documents.Open('C:\temp\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(strOldActiveProfileID);
  pNova.DeleteProfile2(strNewProfileID);
  //resore default printer
  pNova.RestoreDefaultPrinter();

  //release NovaPdfOptions
  //pNova._Release();

  ActiveX.CoUninitialize();

end.