Solved

Fill Word Forms

Posted on 2004-10-18
7
520 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

19 Experts available now in Live!

Get 1:1 Help Now