Problem with Word and Document FormFields

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

Peter Huang [MSFT]

Hi,

The problem seems to be very specific.
Firstly I have to say I am not farmilar with delphi.

Anyway to isolate the problem I think we may try to do the similar job with
VBA so that we can know if the problem is with the automation layer or the
word application itself.
Here is some code snippet about how to access formfields in vba for your
reference.

Sub Test()
Dim o As FormField
For Each o In ThisDocument.FormFields
Debug.Print o.Result
Next
End Sub

Also to isolate the problem, I think you may try to add a if statement in
the while loop to skip the 10th formfield to see if the problem persists in
the specific form field.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top