Solved

Fill Word Forms

Posted on 2004-10-18
7
519 Views
Last Modified: 2010-04-05
Hi,

I'm writing a Little Delphi App that has to take in the command line some arguments and from that fill a predefined word form. What I can't seem to do is to have access to those form fields. I tried through Bookmarks, but the only thing it does is overwrite my field and replacing it with my text. Here is a code snippet:

procedure TForm1.Button2Click(Sender: TObject);
begin

     Name := txtNom.Text + txtPrenom.Text;
     WordApp1 := CreateOleObject('Word.Application');
     WordApp1.Visible := False;
     WordApp1.Documents.Open('c:\DossierTemplate.doc');
     WordApp1.Selection.Find.ClearFormatting;
     WordApp1.Selection.Find.Replacement.ClearFormatting;
     WordApp1.Selection.Find.Execute('<NAME>',True,False,False,False,False,True,1,False,Name,2);

     WordApp1.Selection.Find.ClearFormatting;
     WordApp1.Selection.Find.Replacement.ClearFormatting;
     WordApp1.Selection.Find.Execute('<ADRESSE>',True,False,False,False,False,True,1,False,txtAdresse.Text,2);
     WordApp1.ActiveDocument.Bookmarks.Item('SomeText').Range.Text := txtAdresse.Text;

     WordApp1.ActiveDocument.SaveAs('c:\NewDoc.doc');

What I need to have is code to fill-in the fields and not replace them.

Thanks
0
Comment
Question by:dabitbol
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:vadim_ti
ID: 12339700
i tried your code with template built as

.........
........
Name: <NAME>
Address: <ADDRESSE>
........
........

after running your code i get

.........
........
Name: TxtNomTxtPrenom
Address: txtAdresse
........
........

what exactly you wanted to get?
0
 
LVL 3

Author Comment

by:dabitbol
ID: 12340327
On this solution I used manual tags to position the text I wanted to replace. This worked fine. However, now our forms use fill-in fields so placing Tags is not an option because I need to have form fields and because I might need to modify the form later.

Basically I need to know where in the Word DOM are the form fields and how to access them programatically with an example if possible.

Thanks
0
 
LVL 3

Expert Comment

by:danielluyo
ID: 12341254
you can access the fields in the document using:

Documents(1).Fields(1).Result.Text = "my text"

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Author Comment

by:dabitbol
ID: 12341270
not in COM nor in Delphi

David
0
 
LVL 3

Accepted Solution

by:
danielluyo earned 500 total points
ID: 12341449
procedure TForm1.Button1Click(Sender: TObject);
var
    WordApp1, WordDoc, WordField: Variant;
begin
    WordApp1 := CreateOleObject('Word.Application');
    WordApp1.Visible := False;
    WordDoc := WordApp1.Documents.Open('c:\Testformulario.doc');
    WordField := WordDoc.Fields.Item(1);
    WordField.Result.Text := 'myNew Text';
    WordApp1.ActiveDocument.SaveAs('c:\NewDoc.doc');
end;
0
 
LVL 3

Author Comment

by:dabitbol
ID: 12341479
Found it by myself, but I'll give you the point DanielLuyo since you were right.

David
0
 
LVL 3

Expert Comment

by:danielluyo
ID: 12341498
thanks : )
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now