Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2008

Tip: Looking for answers? Try searching our database.

Modifying field contents using vba

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
toth.usenet@gmail.com - 24 Jan 2008 10:48 GMT
hi,

i've got a word document which has been generated automatically and
the references/links/sources for table of contents and index have been
generated using field information. so a header would look like this

1.1.  Header\Title\Chapter { XE "Header\Title\Chapter" }

i have a vba script which will modify the header text just as desired
(e.g.  to Chapter). however, i would also need to modify the content
of the field to also be only { XE "Chapter" } for it to show in the
table of contents and in the index. how do i go about changing the
text inside the fields?

one possibility is to modify the string inside the field, the other is
to copy the header text and paste it into the field. unfortunately i
don't have any experience with fields at all so i couldn't tell which
one is easier, and being a newbee to vba in general i wouldn't know
how to implement either or them.

any help is appreciated.

cheers,
thomas
David Sisson - 25 Jan 2008 15:43 GMT
On Jan 24, 4:48 am, toth.use...@gmail.com wrote:
> hi,
>
> i have a vba script which will modify the header text just as desired
> (e.g.  to Chapter). however, i would also need to modify the content

Show us the relevant code.
toth.usenet@gmail.com - 04 Feb 2008 09:21 GMT
hi david,

sorry for the late reply, i can't always access the newsgroups.

the code i have is the following:

Dim oPrg As Paragraph
Dim rTmp As Range
For Each oPrg In ActiveDocument.Paragraphs
  Set rTmp = oPrg.Range
  With rTmp.Find
     .Text = "\"
     .Forward = False '!
     While .Execute
        rTmp.Start = oPrg.Range.Start
        rTmp.Select  ' for testing
        rTmp.Text = ""
     Wend
  End With
Next

i hoped that i could re-use the same code but using
ActiveDcoument.Field instead of the paragraphs. however, when i tried
it it failed which i'm not surprised about as i have the feeling a
slightly different approach would need to be taken. any ideas?

thanks for your help,
thomas

> On Jan 24, 4:48 am, toth.use...@gmail.com wrote:
>
[quoted text clipped - 4 lines]
>
> Show us the relevant code.
David Sisson - 04 Feb 2008 16:56 GMT
Try this

Sub ReplaceXETextInFields()

Dim aDoc As Document
Dim MyField As Field

Set aDoc = ActiveDocument
For Each MyField In aDoc.Fields
   If MyField.Type = wdFieldIndexEntry Then
   MyField.Select
       With Selection.Find
           .Text = "Header\Title\Chapter"
           .Replacement.Text = "Chapter"
           .Forward = True
           .Wrap = wdFindStop
           .Format = False
           .Execute Replace:=wdReplaceAll
       End With
   End If
Next
End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.