J
Jon Robertson
We use Word automation to generate new Word documents using
information from our database. We start Word, load a template
that has form fields, iterate through the form fields and
replace each one with a value from the database.
Note that particular fields may represent a graphic or a table.
In this case, we insert the graphic or table at the anchor of
the field and then delete the field. For all other fields, we
simply replace the Result property of the field with the value
from the database.
Below is a snip of code, although it's Delphi. I haven't tried
to reproduce this from VB or VBA. Much error/exception handling
has been removed from the snip to make the code easier to read
here.
The problem is that FormFields.Item(I) will frequently return
"Server threw an exception". It happens on different templates,
each having a different number of form fields and a different
list of form fields from our software. Yet the exception always
occurs on field 10 of the collection.
To repeat, the exception ONLY occurs when I = 10.
We've seen this happen with Word 2000, 2002 (XP), and 2003.
Any suggestions would be greatly appreciated.
Code snip:
procedure TWordDoc.FillTemplateFields(FormFields: OleVariant);
var
I : Integer;
LinkToFile : OleVariant;
SaveWithDocument : OleVariant;
Anchor : OleVariant;
MyField : OleVariant;
cdsDocFields : TClientDataSet;
FieldList : TStringList;
Sig : OleVariant;
begin
I := 1;
iFieldCount := -1;
while I <= FormFields.Count do begin
MyField := FormFields.Item(I);
if MyField.Result = 'DR_Signature' then begin
LinkToFile := False;
SaveWithDocument := True;
Anchor := MyField.Range;
Sig := FWordDoc.InlineShapes.AddPicture(FSigFile,
LinkToFile, SaveWithDocument, Anchor);
Sig.Height := 0.75 * 72.0;
Sig.Width := 3.00 * 72.0;
MyField.Delete;
Continue;
end //if dr_signature
MyField.Range.Fields.Item(1).Result.Text :=
cdsDocFields.FieldByName(FieldList[J]).DisplayText;
// Since MyField was not deleted, increment I to the next field.
Inc(I);
end;
end;
information from our database. We start Word, load a template
that has form fields, iterate through the form fields and
replace each one with a value from the database.
Note that particular fields may represent a graphic or a table.
In this case, we insert the graphic or table at the anchor of
the field and then delete the field. For all other fields, we
simply replace the Result property of the field with the value
from the database.
Below is a snip of code, although it's Delphi. I haven't tried
to reproduce this from VB or VBA. Much error/exception handling
has been removed from the snip to make the code easier to read
here.
The problem is that FormFields.Item(I) will frequently return
"Server threw an exception". It happens on different templates,
each having a different number of form fields and a different
list of form fields from our software. Yet the exception always
occurs on field 10 of the collection.
To repeat, the exception ONLY occurs when I = 10.
We've seen this happen with Word 2000, 2002 (XP), and 2003.
Any suggestions would be greatly appreciated.
Code snip:
procedure TWordDoc.FillTemplateFields(FormFields: OleVariant);
var
I : Integer;
LinkToFile : OleVariant;
SaveWithDocument : OleVariant;
Anchor : OleVariant;
MyField : OleVariant;
cdsDocFields : TClientDataSet;
FieldList : TStringList;
Sig : OleVariant;
begin
I := 1;
iFieldCount := -1;
while I <= FormFields.Count do begin
MyField := FormFields.Item(I);
if MyField.Result = 'DR_Signature' then begin
LinkToFile := False;
SaveWithDocument := True;
Anchor := MyField.Range;
Sig := FWordDoc.InlineShapes.AddPicture(FSigFile,
LinkToFile, SaveWithDocument, Anchor);
Sig.Height := 0.75 * 72.0;
Sig.Width := 3.00 * 72.0;
MyField.Delete;
Continue;
end //if dr_signature
MyField.Range.Fields.Item(1).Result.Text :=
cdsDocFields.FieldByName(FieldList[J]).DisplayText;
// Since MyField was not deleted, increment I to the next field.
Inc(I);
end;
end;