You are on page 1of 4

RxRicks Blog

Ricks Rants

Word automation with Delphi


Posted on December 14, 2010 by tharpr
In current versions of my Windows Pk programs I have hard-coded a monitoring form which seem to
satisfy no one. I have been wanting to add some flexibility to allow users to utilize their own
monitoring forms. It occurred to me that the easiest way to do this would be to start with a template
in M$ Word format which the user could customize to fit their needs. Considering the ubiquity of M$
Word, it is quite likely that most users have already designed their own monitoring forms in Word
format. There would be specific words within the document that would be automatically replaced with
patient specific data before printing.
The only part I would need to code is the search and replace function in M$ Word. Unfortunately, just
as I found when I tried to implement Excel OLE functionality, the solution is not so simple. In the M$
world, there is always more than one way to skin a cat. And there are many experts on various
support groups who have posted object pascal code which claims to automate this functionality in
Word. None of these code samples worked for me, probably because I am still using Microsoft Office
97. Dont judge me, it still works and does everything that I need a so-called word processor to do.
After hours of googling and trial and error, I found the following code works in most versions of M$
Word.
First add Variants and ComObj to the uses clause:
[delphi]
uses
Variants, ComObj;
[/delphi]
Here is the function to open, search and replace, and print a Word document in Delphi:
[delphi]
procedure PrintMonitor(PrinterName:String, WordDocName:String);
var
WordApp, WordRange, SearchString, ReplaceString: OLEVariant;
WordDoc, strTemp: string;
z, I: Integer;
begin

// Create base word doc with path


WordDoc := ExtractFilePath(Application.ExeName) + WordDocName;
// Exit if WordDoc not found
if not FileExists(WordDoc) then
begin
strTemp := WordDocName + not found;
MessageBox(0, PChar(strTemp), File not found, MB_ICONERROR or MB_OK);
Exit;
end;
// Create the OLE Object
try
WordApp := CreateOLEObject(Word.Application);
except
on E: Exception do
begin
E.Message := Word is not available.;
raise;
end;
end;
// Replace
try
{ Hide Word }
WordApp.Visible := False;
// Open the document enclose in double quotes
WordApp.Documents.Open(" + WordDoc + ");
for I := 1 to 3 do
begin
// Initialize search/replace parameters
case I of
1:
begin
SearchString := C_PTNAME;
ReplaceString := frmMain.EditName.Text;
end;
2:
begin
SearchString := C_ROOM;
ReplaceString := frmMain.EditRoom.Text;
end;
3:
begin
SearchString := C_MD;
ReplaceString := frmMain.EditMD.Text;
end;

end;
WordRange := WordApp.ActiveDocument.Content;
WordRange.Find.Execute(SearchString,
EmptyParam, // match case
EmptyParam, // match whole word
EmptyParam, // match wildcards
EmptyParam, // match sounds like
EmptyParam, // match all word forms
EmptyParam, // forward
EmptyParam, // wrap
EmptyParam, // format
ReplaceString, // replace with
2); // replace all
end;
// Set printer
WordAPP.Application.WordBasic.FilePrintSetup (Printer:=PrinterName, DoNotSetAsSysDefault:=1);
// Print
WordApp.ActiveDocument.PrintOut;
// Close the document, do not save original
WordApp.ActiveDocument.Close(0);
// Check that the print has finished before exiting the procedure
while WordApp.BackgroundPrintingStatus>0 do
begin
Sleep(100);
Application.ProcessMessages;
end;
except
on E : Exception do begin
ShowMessage(E.ClassName+ error raised, with message : +E.Message);
WordApp.Quit;
WordApp := Unassigned;
Exit;
end;
end;
// Quit Word
WordApp.Quit;
WordApp := Unassigned;
end;
[/delphi]

And here is the call to the PrintMonitor function:


[delphi]
strWordDocName := PK_monitor.doc;
// Call Windows Print dialog and pass printer name
if PrintDialog1.Execute then
PrintMonitor(Printer.Printers[Printer.PrinterIndex], strWordDocName);
[/delphi]
The code works on my ancient version of Word (97), and Word 2003, but I cant guarantee that it will
work in all versions.
This entry was posted in software by tharpr. Bookmark the permalink
[http://rxkinetics.com/blog/?p=353] .

Comments are closed.

You might also like