Solved

Replace word fields with Text

Posted on 2009-07-06
6
354 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
[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
6 Comments
 

Author Comment

by:crisis_davis
ID: 24792420
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
ID: 24794510
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
ID: 31600443
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:Joanne M. Orzech
ID: 24796091
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
ID: 24796389
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
ID: 24796413
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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