Source code of WordAPI.exe

This is the part of the code that does the Word automation for Java on Windows NT.

It's open source. It's written in Pascal using Borland Delphi 4 Standard.

Open source minded translators into C++, JNI equivalent supplier or authors of Excel drivers or something like that may apply a link from here.

Parts:  WordAPI.dpr  WordAPI1.pas

(Full source code is available inside Cameleon OSP: cam2.zip - folder "windows\WordAPI"; sample application see package mkt)


WordAPI.dpr

program WordAPI;

uses
  Forms,
  WordAPI1 in 'WordAPI1.pas' {FormAction},
  Word_TLB in '..\..\Imports\Word_TLB.pas';
  //WordAPIProt in 'WordAPIProt.pas' {FormProtocol},
  //WordAPIHelp in 'WordAPIHelp.pas' {FormHelp},
  //WinApi in '..\Shared\WinApi.pas';

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TFormAction, FormAction);
  // Application.CreateForm(TFormProtocol, FormProtocol);
  // Application.CreateForm(TFormHelp, FormHelp);
  Application.Run;
end.


WordAPI1.pas

unit WordAPI1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, ComObj, ExtCtrls;

type
  TFormAction = class(TForm)
    ButtonCancel: TButton;
    StatusBar1: TStatusBar;
    TimerQuitDelay: TTimer;
    procedure ButtonCancelClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure TimerQuitDelayTimer(Sender: TObject);
  private
    procedure act;
    procedure showDocumentProgress;
  public
  end;

var
  FormAction: TFormAction;

implementation

{$R *.DFM}

uses Word_TLB{, WordAPIProt, WordAPIHelp};
// use menu "Project", "Import Type Library", "Microsoft Word Object Library" to generate Word_TLB

var
  MyWord: Variant;
  DocumentCount: integer;
  canceled: boolean;
  isToQuit: boolean;
  quitDelayInterval: integer;

procedure TFormAction.act;
var
  WordInp: TextFile;
  line, key, value: string;
  templateName: string;
  noteNotMatchingBookmarks: boolean;
  ActiveDocumentDefined: boolean;

begin
  DocumentCount := 0;
  ActiveDocumentDefined := false;
  noteNotMatchingBookmarks := true;
  isToQuit := false;

  Refresh;

  try
    AssignFile(WordInp, 'WordInp.txt');
    Reset(WordInp);
  except
    // FormHelp.ShowModal;
    exit;
  end;

  try
    MyWord := GetActiveOleObject('Word.Application');
  except
    try
      MyWord := CreateOleObject('Word.Application');
    except
      // FormProtocol.addEntry('Can''t start Word');
      exit;
    end;
  end;

  try
    MyWord.Application.Visible := true;
  except
    // FormProtocol.addEntry('Can''t make Word visible');
    exit;
  end;

  while not Eof(WordInp) and not canceled do
  begin
    Readln(WordInp, line);
    key := Trim(Copy(line, 1, 40));
    value := Trim(Copy(line, 41, 999999));
    if key = '@noteNotMatchingBookmarks' then
    begin
      if value = 'FALSE' then noteNotMatchingBookmarks := false
      else noteNotMatchingBookmarks := true;
    end
    else
    //--------------------------------------------------------------------------
    if key = '@createNewDocumentFromTemplate' then
    begin
      if value = 'TEMPLATE_TO_SELECT_BY_USER' then
      begin
        try
          if MyWord.Dialogs.Item(wdDialogFileNew).Show then ActiveDocumentDefined := true
          else ActiveDocumentDefined := false;
        except
          ActiveDocumentDefined := false;
        end;
      end
      else
      begin
        templateName := value;
        try
          MyWord.Documents.Add(value);
          ActiveDocumentDefined := true;
          showDocumentProgress;
        except
          // FormProtocol.addEntry('Template ' + value + ' not found');
          ActiveDocumentDefined := false;
        end;
      end;
    end
    //--------------------------------------------------------------------------
    else if key = '@changeDocumentDirectory' then
    begin
      try
        MyWord.Application.ChangeFileOpenDirectory(value);
      except
        // FormProtocol.addEntry('Cannot change directory to ' + value);
      end;
    end
    else if key = '@saveDocumentAs' then
    begin
      if ActiveDocumentDefined then
      begin
        try
          MyWord.ActiveDocument.SaveAs(value);
        except
          // FormProtocol.addEntry('Cannot save document as ' + value);
        end;
      end
      else // FormProtocol.addEntry('There is no document active to be saved as ' + value);
    end
    //--------------------------------------------------------------------------
    else if key = '@saveDocumentAsAndClose' then
    begin
      if ActiveDocumentDefined then
      begin
        try
          MyWord.ActiveDocument.SaveAs(value);
        except
          // FormProtocol.addEntry('Cannot save document as ' + value);
        end;
        try
          MyWord.ActiveDocument.Close();
          ActiveDocumentDefined := false;
        except
          // FormProtocol.addEntry('Cannot close after saving');
        end;
      end
      else // FormProtocol.addEntry('There is no document active to be saved as ' + value);
    end
    //--------------------------------------------------------------------------
    else if key = '@closeDocument' then
    begin
      if ActiveDocumentDefined then
      begin
        try
          MyWord.ActiveDocument.Close();
          ActiveDocumentDefined := false;
        except
          // FormProtocol.addEntry('Cannot close after mailing');
        end;
      end
      else // FormProtocol.addEntry('There is no document active to be closed');
    end
    //--------------------------------------------------------------------------
    else if key = '@quitApplication' then
    begin
      isToQuit := true;
      quitDelayInterval := 0;
    end
    //--------------------------------------------------------------------------
    else if key = '@quitApplicationAfterWaiting' then
    begin
      isToQuit := true;
      quitDelayInterval := StrToInt(value);
    end
    //--------------------------------------------------------------------------
    else if key = '@faxAndForget' then
    begin
      if ActiveDocumentDefined then
      begin
        try
          MyWord.ActiveDocument.SendFax(Address:=value);
        except
          // FormProtocol.addEntry('Cannot fax to ' + value);
        end;
        try
          MyWord.ActiveDocument.Close(false);
          ActiveDocumentDefined := false;
        except
          // FormProtocol.addEntry('Cannot close after fax');
        end;
      end
      else // FormProtocol.addEntry('There is no document active to be faxed');
    end
    //--------------------------------------------------------------------------
    else if key = '@printAndForget' then
    begin
      if ActiveDocumentDefined then
      begin
        if value = 'PRINTER_TO_SELECT_BY_USER' then
        begin
          try
            MyWord.Dialogs.Item(wdDialogFilePrint).Show;
          except
            // FormProtocol.addEntry('Cannot print');
          end;
          try
            MyWord.ActiveDocument.Close(false);
            ActiveDocumentDefined := false;
          except
            // FormProtocol.addEntry('Cannot close after printing');
          end;
        end
        else
        begin
          try
            MyWord.ActivePrinter := value;
          except
            // FormProtocol.addEntry('Cannot access printer ' + value);
          end;
          try
            MyWord.Application.PrintOut();
          except
            // FormProtocol.addEntry('Cannot print');
          end;
          try
            MyWord.ActiveDocument.Close(false);
            ActiveDocumentDefined := false;
          except
            // FormProtocol.addEntry('Cannot close after printing');
          end;
        end;
      end
      else // FormProtocol.addEntry('There is no document active to be printed');
    end
    //--------------------------------------------------------------------------
    else if key = '@executeMacro' then
    begin
      try
        MyWord.Application.Run(MacroName:=value{, Wait:=True});
      except
        // FormProtocol.addEntry('Cannot execute Macro ' + value);
      end;
    end // end of special commands!
    else if Copy(key, 1, 1) = '@' then // FormProtocol.addEntry('Command ' + key + ' is not supported')
    //--------------------------------------------------------------------------
    else // beginning of bookmark work
    begin
      if ActiveDocumentDefined then
      begin
        try
          MyWord.Selection.GoTo(What:=wdGoToBookmark, Name:=key);
          MyWord.Selection.TypeText(Text:=value);
        except
          if noteNotMatchingBookmarks then // FormProtocol.addEntry('Bookmark ' + key + ' in template ' + templateName + ' not found.');
        end;
        try
          MyWord.ActiveDocument.Saved := true;
        except
        end;
      end;
    end;
  end;

  CloseFile(WordInp);

  if ActiveDocumentDefined then
  begin
    try
      MyWord.ActiveDocument.Activate;
    except
      // FormProtocol.addEntry('Can''t activate Document');
      exit;
    end;
  end;

end;

procedure TFormAction.showDocumentProgress;
begin
  DocumentCount := DocumentCount + 1;
  StatusBar1.SimpleText := IntToStr(DocumentCount) + '. Dokument';
end;

procedure TFormAction.ButtonCancelClick(Sender: TObject);
begin
  canceled := true;
  close;
end;

procedure TFormAction.FormActivate(Sender: TObject);
begin
  canceled := false;
  act;
  // FormProtocol.presentIfNeccessary;
  if isToQuit then
  begin
    TimerQuitDelay.Interval := quitDelayInterval;
    TimerQuitDelay.Enabled := true
  end
  else Close;

end;

procedure TFormAction.TimerQuitDelayTimer(Sender: TObject);
begin
  try
    MyWord.Application.Quit;
  except
  end;
  Close;

end;

end.


[ Home ]