The following code will search for a '<' in a document. If found, it will
replace the characters at that position with the string 'merged stuff'. The
logic assumes there are no more than 20 characters between '<' and '>'
pairs.
My question -- is there any way to make this go any faster? The reason why
i'm concerned is that i'm finding equivalent late-binding logic to be faster
than this early binding logic. What gives??
(please note that i'm not interested in using ms-word's merge capabilities)
Thanks very much for the attention
Charles McAllister
procedure TForm1.NewButtonClick(Sender: TObject);
var
WordApp : WordApplication;
TextRange : Range;
Text_variant : OleVariant;
File_variant : OleVariant;
Index : Integer;
Variable_str : string;
Index_pos : Integer;
Boolean_variant : OleVariant;
Changes_variant : OleVariant;
RevertVariant : OleVariant;
CopiesVariant : OleVariant;
BackgroundVariant : OleVariant;
begin
ElapsedCount := GetTickCount;
WordApp := CoWordApplication.Create;
WordApp.ActivePrinter := Printer.Printers[0];
File_variant := 'C:\SOMEDOC.DOC';
RevertVariant := TRUE;
CopiesVariant := 1;
BackgroundVariant := False;
for Index := 1 to TimesEdit.Value do begin
WordApp.Documents.Open (File_variant, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, RevertVariant, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam);
repeat
TextRange := WordApp.ActiveDocument.Content;
Text_variant := '<';
TextRange.Find.Execute (Text_variant, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, Boolean_variant, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
if TextRange.Find.Found then begin
TextRange.End_ := TextRange.Start + 20;
Variable_str := TextRange.Text;
Boolean_variant := True;
Index_pos := Pos('>', Variable_str);
if Index_pos > 0 then begin
Variable_str := LowerCase(Copy(Variable_str, 1, Index_pos));
Text_variant := Variable_str;
TextRange.Find.Execute (Text_variant, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, Boolean_variant, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
TextRange.Text := 'merged stuff';
end;
end else
Break;
until False;
{WordApp.ActiveDocument.PrintOut (BackgroundVariant, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, CopiesVariant,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam);}
end;
Changes_Variant := wdDoNotSaveChanges;
WordApp.Quit (Changes_Variant, EmptyParam, EmptyParam);
WordApp := nil;
ShowMessage ('done' + InttoStr(GetTickCount - ElapsedCount));
end;
procedure TForm1.OldButtonClick(Sender: TObject);
var
WordVariant : Variant;
Index : Integer;
Variable_str : string;
Index_pos : Integer;
Temp_position : Integer;
begin
ElapsedCount := GetTickCount;
WordVariant := CreateOLEObject ('WORD.BASIC');
for Index := 1 to TimesEdit.Value do begin
WordVariant.FileOpen (Name := 'C:\SOMEDOC.DOC', Revert := 1, ReadOnly :=
1);
{WordVariant.StartOfDocument;}
repeat
WordVariant.EditFind (Find := SearchEdit.Text);
if WordVariant.EditFindFound then begin
Temp_position := WordVariant.GetSelStartPos;
Variable_str := WordVariant.GetText(Temp_position, Temp_position +
19);
Index_pos := Pos('>', Variable_str);
if Index_pos > 0 then begin
Variable_str := LowerCase(Copy(Variable_str, 1, Index_pos));
WordVariant.CharLeft (1, 1); // De-select first character of
variable so Find will work
WordVariant.EditReplace (Find := Variable_str, Replace := '',
ReplaceOne := 1);
WordVariant.Insert ('merged stuff');
end;
end else
Break;
until False;
{//WordVariant.FilePrint (NumCopies := 1, Background := 0);}
WordVariant.FileClose (2);
end;
WordVariant.AppClose;
ShowMessage ('done' + InttoStr(GetTickCount - ElapsedCount));
end;