PDA

View Full Version : Search for Style and add tab



ABrown
05-18-2010, 01:49 AM
Hi - I wondered if anyone can help - my VBA skills are limited and I have tried to work on a macro to search for the style TOC 2 and on the second space - ie after the second word - add a tab mark. It works when I do it manually but if I record a macro and try and run it the macro does not work. I have broken it down into the steps I need:
Search TOC 2 and a space
find next
Search TOC 2 and a space, replace with a tab
Loop untill end

Any help would be much appreciated.

Thanks.

Annette

fumei
05-18-2010, 09:07 AM
Please be consistent.

On one hand you state: "on the second space - ie after the second word - add a tab mark" ADD

On the other: "TOC 2 and a space, replace with a tab " REPLACE

What do you actually want to do? Add, or replace?

Also, the TOC is directly attributed to a specific text with a specific style.

The default is TOC 2 lists paragraphs using Heading 2 style. The actual text is used in the TOC. So you now want to make then NOT match?

If you ever update the TOC, your changes will revert.

The actual text used to generate the TOC = "Yadda[space]Blah"

You want to keep that, but change the TOC entry to something different?
"Yadda[tab]Blah"

Is this correct?




Why?

What if it has only one word? Is this possible? What if it has more than two words? Is this possible?

TonyJollans
05-18-2010, 09:12 AM
With ActiveDocument.Content.Find
.Style = ActiveDocument.Styles("TOC 2")
.Text = "([! ]@ [! ]@) (*^13)"
.Replacement.Text = "\1^t\2"
.Forward = True
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

But everything Gerry said, as well :)