Solved

Replace word fields with Text

Posted on 2009-07-06
6
347 Views
Last Modified: 2012-05-07
I am trying to replace all fields in a word doc with static text using perl so that the replaced text position is the same as the field.  i.e. remove the field and in its place put some text.

All I seem to be able to do is to replace the content of the field to the desired text.
my $listOfFields = $wordApp->ActiveDocument->Fields();

my $count = $wordApp->ActiveDocument->Fields->Count;

for (my $i = 1; $i <= $count; $i++) {

             my $field = $listOfFields->Item($i);

              $field->{Name} = 'MyFieldName';    #thought this might be useful - but doesn't set the name.

              $field->{Code}->{Text} = 'StaticText';

              $field->Cut;      #doesn't seem to remove the field

}

Open in new window

0
Comment
Question by:crisis_davis
  • 3
  • 3
6 Comments
 

Author Comment

by:crisis_davis
Comment Utility
By using mailmerge to merg the document with a list that is pre-configured works a dream.  Now all I need to do is to automate the merge process.  Does anyone know how to run the  merge process from a perl script?
0
 
LVL 37

Accepted Solution

by:
Joanne M. Orzech earned 500 total points
Comment Utility
If you're just replacing the content of the field with the exact text contained in the field, why don't you just do Control A (Select All) and Control-Shift F9 to turn the fields to text...?  It's called unlinking the fields.

0
 

Author Closing Comment

by:crisis_davis
Comment Utility
Love it, - nice and simple - thanx  - will automate it with Perl now that I know what I am trying to do.  many thanks for help.
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 37

Expert Comment

by:Joanne M. Orzech
Comment Utility
Excellent!  I was hoping that was what you were looking for!  So pleased to have been able to assist you.  Thanks so much.

Joanne Orzech
MS Word MVP
EE Zone Advisor
0
 

Author Comment

by:crisis_davis
Comment Utility
So code now looks like:
my $listOfFields = $wordApp->ActiveDocument->Fields();

my $count = $wordApp->ActiveDocument->Fields->Count;

for (my $i = 1; $i <= $count; $i++) {

             my $field = $listOfFields->Item($i);

              $field->{Code}->{Text} = 'StaticText';

}

$listOfFields->Unlink();

Open in new window

0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
Comment Utility
Thanks for posting that!  It's all greek to me since I only use VBA (code below to update and unlink fields), but I'm sure will be helpful to anyone who knows perl script  :)  

So thanks!

    Selection.WholeStory

    Selection.Fields.Update

    Selection.Fields.Unlink

    Selection.HomeKey Unit:=wdStory

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Free Online PDF Converter 2 35
stop word moving down 2 39
How to Adjust Amount of Word Outline 9 24
Convert .PDF 6 38
Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

771 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