Replacement function not working in a TOC field

J

Jentimus

Goal: In some lines of a TOC, I want to replace the tab, page number and
paragraph mark with a paragraph mark only. The lines are identified with by
including a specific word (in the example/code below, the identifying word is
'ONE').

For example,
PART ONE TRUST DISTRIBUTION PROVISIONS 1
is replaced by
PART ONE TRUST DISTRIBUTION PROVISIONS

The code below works if the selected text is typed text, but doesn't work if
the selected text is in the Table of Contents field:

Sub test()
' Remove the number after a 'Part' X.
With Selection.Find
.ClearFormatting
.Text = "(ONE )(*)^t[0-9]^13"
With .Replacement
.ClearFormatting
.Text = "\1\2^13"
End With
.Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=True, _
MatchWholeWord:=True, MatchWildcards:=True
End With
End Sub

Other text replacement functions work in a selected TOC, but those do not
use Expressions, so maybe that is the issue?

Any advice is appreciated; thanks in advance for your time.

Jen
 
M

macropod

Hi Jentimus,

To change the TOC contents, you need to change the headings it refers to. If you then update the TOC, it'll reflect those changes.
 
J

Jentimus

The problem is that they want the table of contents to not include page
numbers on some lines -- however, I found another post that described adding
the n\ "1-1" switch to suppress page numbers at given (consecutive) levels,
and that works.

I was 'swatting a fly with a Buick' by using replacement to do this!

However, maybe this post will help someone else ... thanks for taking the
time to respond.
--
Thanks, Jen


macropod said:
Hi Jentimus,

To change the TOC contents, you need to change the headings it refers to. If you then update the TOC, it'll reflect those changes.

--
Cheers
macropod
[Microsoft MVP - Word]


Jentimus said:
Goal: In some lines of a TOC, I want to replace the tab, page number and
paragraph mark with a paragraph mark only. The lines are identified with by
including a specific word (in the example/code below, the identifying word is
'ONE').

For example,
PART ONE TRUST DISTRIBUTION PROVISIONS 1
is replaced by
PART ONE TRUST DISTRIBUTION PROVISIONS

The code below works if the selected text is typed text, but doesn't work if
the selected text is in the Table of Contents field:

Sub test()
' Remove the number after a 'Part' X.
With Selection.Find
.ClearFormatting
.Text = "(ONE )(*)^t[0-9]^13"
With .Replacement
.ClearFormatting
.Text = "\1\2^13"
End With
.Execute Replace:=wdReplaceAll, _
Format:=True, MatchCase:=True, _
MatchWholeWord:=True, MatchWildcards:=True
End With
End Sub

Other text replacement functions work in a selected TOC, but those do not
use Expressions, so maybe that is the issue?

Any advice is appreciated; thanks in advance for your time.

Jen
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top