tagging a specific styled words

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

tagging a specific styled words

Post by Robie »

Hi

I have large documents with different styles for words/paragraphs/etc within or outside of the tables.
I have written a macro which searching for a particular style and replaces that with a tag. For tables, it seems to do this on a per cell basis. This makes the process so slow. Foer example, I had the macro running overnight replacing styles with tags and it is still going18 hours later. :(.

So, is there any way of sort of 'looking ahead' in a table to see if 'all' the words are for a specific style? This would mean that we could select the whole table and reset it to a tag.
Thanks.

Robie

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: tagging a specific styled words

Post by HansV »

Will the target style always be applied to entire cells? Or could it be applied to part of a cell?
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: tagging a specific styled words

Post by Robie »

HansV wrote:Will the target style always be applied to entire cells? Or could it be applied to part of a cell?
That's the problem. It could be applied to part of the cell but for 90% of the cells it is applied to the whole cell.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: tagging a specific styled words

Post by HansV »

I'll see if I can come up with something, but it might not be easy.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: tagging a specific styled words

Post by Robie »

HansV wrote:I'll see if I can come up with something, but it might not be easy.
Thanks Hans but don't worry too much.
If I have to I will do some of it by hand :(

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: tagging a specific styled words

Post by HansV »

The problem is that the end-of-row marker in a table is not formatted with the style of the surrounding text. I wrote a macro, but it was extremely slow - it took a minute or more to run on a one-page document, and it turned out to be unreliable too. So I'm afraid I have no solution for you.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: tagging a specific styled words

Post by Robie »

HansV wrote:The problem is that the end-of-row marker in a table is not formatted with the style of the surrounding text. I wrote a macro, but it was extremely slow - it took a minute or more to run on a one-page document, and it turned out to be unreliable too. So I'm afraid I have no solution for you.
No problem Hans, at least you tried.

Thanks anyway. I will just have to do it by hand :hairout:

User avatar
Jay Freedman
Microsoft MVP
Posts: 1320
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: tagging a specific styled words

Post by Jay Freedman »

It should be possible to do this with a Replace All operation instead of a loop. Because it's all machine code, it will be much faster than a VBA loop.

In the user interface, put the cursor in the Find What box of the Replace dialog. Click the More button, then click the Format button, click Style in the menu, and select the style you want to replace. Then click in the Replace With box, type the code ^& (which represents the found text) surrounded by the tag, and click Format > Style and choose either Normal (if the style being replaced is a paragraph style or a linked style) or Default Paragraph Font (if you're replacing a character style). Finally, click the Replace All button.

If you're writing a macro for some other purpose or to process multiple styles, and want to include this, we can guide you to code a similar procedure.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: tagging a specific styled words

Post by HansV »

That's indeed easy, Jay:

Code: Select all

Sub Test()
    Call TagStyle("MyStyle")
End Sub

Sub TagStyle(StyleName As String)
    Application.ScreenUpdating = False
    With ActiveDocument.Content.Find
        .Text = ""
        .ClearFormatting
        .Format = True
        .Style = ActiveDocument.Styles(StyleName)
        .Replacement.Text = "<tag>^&</tag>"
        .Replacement.ClearFormatting
        .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
End Sub
But such a macro will surround the text in individual cells with tags, and my impression was that Robie doesn't want that.
Best wishes,
Hans