Solved

Fill Word Forms

Posted on 2004-10-18
7
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 184
Can Viruses spread while transferring Binary data with Winsock 2 106
Performance of SQL statement 37 146
Delphi: making a BW image transparent 10 136
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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