Replace any text between tags

S

Sol

I have this huge document (3,756 pages with the tags) and I want to replace
the words between the tags with Word-formatted style, then deleting the
tags, for example I want "<I>any text</I>" to become "any text" in italics
(a style called G-italics), ditto <I>strange transformations</I> to become
"strange transformations" in G-italics in the same search. Then doing
another search for <B>...</B> and so forth.

Nisus Writer Pro does have the Find/Replace to do this, but not the capacity
to go through so many pages, and I'd prefer not to break up this document,
because I'll have repeat all this over and over again (don't know Perl at
all!!).

I've already got Word to make 75,000 changes for a single accented text and
tried using Range which did not work because I guess Range is for a single
part of a text and <I> etc is not a correct term within Range.

Set myRange = ActiveDocument.Range(Start:="<I>", End:="</I>")

Can someone help with this? I have no idea what the VB for this will be,
though I have done quite a few <simple> macros
 
D

Doug Robbins - Word MVP

Try a macro containing the following code:

Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="<I>", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
myrange.End = myrange.Start + InStr(myrange, "</I>") + 3
myrange.Select
myrange.Text = Mid(myrange.Text, 4, Len(myrange.Text) - 7)
myrange.Style = "G-Italics"
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
M

macropod

Hi Sol,

You can dothis without a macro, using Find/Replace.
For the 'Find' expression, use-
(\<?\>)(*)(\</?\>)
For the 'Replace' expression, use-
\2
and format the replacement Style as 'G-Italics'
Check the 'use wildcards' option, then 'replace all'.

A macro equivalent is:
Sub ReFormat()
With Selection.Find
.ClearFormatting
.Text = "(\<?\>)(*)(\</?\>)"
With .Replacement
.Text = "\2"
.ClearFormatting
.Style = ActiveDocument.Styles("G-Italics")
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub
 
D

Doug Robbins - Word MVP

Hi Paul,

That would of course format everything within any tags with the G-Italics
style. Probably not what the user wants as in addition to "<I>...</I>" they
want to do another search for <B>...</B>

They would need to do one search for

(\<I\>)(*)(\</I\>)

using the replacement that you mentioned and then another for

(\<B\>)(*)(\</B\>)

with the appropriate replacement.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
macropod said:
Hi Sol,

You can dothis without a macro, using Find/Replace.
For the 'Find' expression, use-
(\<?\>)(*)(\</?\>)
For the 'Replace' expression, use-
\2
and format the replacement Style as 'G-Italics'
Check the 'use wildcards' option, then 'replace all'.

A macro equivalent is:
Sub ReFormat()
With Selection.Find
.ClearFormatting
.Text = "(\<?\>)(*)(\</?\>)"
With .Replacement
.Text = "\2"
.ClearFormatting
.Style = ActiveDocument.Styles("G-Italics")
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub


--
Cheers
macropod
[Microsoft MVP - Word]


Sol said:
I have this huge document (3,756 pages with the tags) and I want to
replace
the words between the tags with Word-formatted style, then deleting the
tags, for example I want "<I>any text</I>" to become "any text" in
italics
(a style called G-italics), ditto <I>strange transformations</I> to
become
"strange transformations" in G-italics in the same search. Then doing
another search for <B>...</B> and so forth.

Nisus Writer Pro does have the Find/Replace to do this, but not the
capacity
to go through so many pages, and I'd prefer not to break up this
document,
because I'll have repeat all this over and over again (don't know Perl at
all!!).

I've already got Word to make 75,000 changes for a single accented text
and
tried using Range which did not work because I guess Range is for a
single
part of a text and <I> etc is not a correct term within Range.

Set myRange = ActiveDocument.Range(Start:="<I>", End:="</I>")

Can someone help with this? I have no idea what the VB for this will be,
though I have done quite a few <simple> macros
 
M

macropod

Hi Doug,

An alternative way to limit the scopy with a single Find/Replace is to make the Find string:
(\<[IB]\>)(*)(\</[IB]I\>)
adding any other desired formatting expression letters between the square brackets.

--
Cheers
macropod
[Microsoft MVP - Word]


Doug Robbins - Word MVP said:
Hi Paul,

That would of course format everything within any tags with the G-Italics
style. Probably not what the user wants as in addition to "<I>...</I>" they
want to do another search for <B>...</B>

They would need to do one search for

(\<I\>)(*)(\</I\>)

using the replacement that you mentioned and then another for

(\<B\>)(*)(\</B\>)

with the appropriate replacement.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
macropod said:
Hi Sol,

You can dothis without a macro, using Find/Replace.
For the 'Find' expression, use-
(\<?\>)(*)(\</?\>)
For the 'Replace' expression, use-
\2
and format the replacement Style as 'G-Italics'
Check the 'use wildcards' option, then 'replace all'.

A macro equivalent is:
Sub ReFormat()
With Selection.Find
.ClearFormatting
.Text = "(\<?\>)(*)(\</?\>)"
With .Replacement
.Text = "\2"
.ClearFormatting
.Style = ActiveDocument.Styles("G-Italics")
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub


--
Cheers
macropod
[Microsoft MVP - Word]


Sol said:
I have this huge document (3,756 pages with the tags) and I want to
replace
the words between the tags with Word-formatted style, then deleting the
tags, for example I want "<I>any text</I>" to become "any text" in
italics
(a style called G-italics), ditto <I>strange transformations</I> to
become
"strange transformations" in G-italics in the same search. Then doing
another search for <B>...</B> and so forth.

Nisus Writer Pro does have the Find/Replace to do this, but not the
capacity
to go through so many pages, and I'd prefer not to break up this
document,
because I'll have repeat all this over and over again (don't know Perl at
all!!).

I've already got Word to make 75,000 changes for a single accented text
and
tried using Range which did not work because I guess Range is for a
single
part of a text and <I> etc is not a correct term within Range.

Set myRange = ActiveDocument.Range(Start:="<I>", End:="</I>")

Can someone help with this? I have no idea what the VB for this will be,
though I have done quite a few <simple> macros
 
M

macropod

make that:
(\<[IB]\>)(*)(\</[IB]\>)

--
Cheers
macropod
[Microsoft MVP - Word]


macropod said:
Hi Doug,

An alternative way to limit the scopy with a single Find/Replace is to make the Find string:
(\<[IB]\>)(*)(\</[IB]I\>)
adding any other desired formatting expression letters between the square brackets.

--
Cheers
macropod
[Microsoft MVP - Word]


Doug Robbins - Word MVP said:
Hi Paul,

That would of course format everything within any tags with the G-Italics
style. Probably not what the user wants as in addition to "<I>...</I>" they
want to do another search for <B>...</B>

They would need to do one search for

(\<I\>)(*)(\</I\>)

using the replacement that you mentioned and then another for

(\<B\>)(*)(\</B\>)

with the appropriate replacement.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
macropod said:
Hi Sol,

You can dothis without a macro, using Find/Replace.
For the 'Find' expression, use-
(\<?\>)(*)(\</?\>)
For the 'Replace' expression, use-
\2
and format the replacement Style as 'G-Italics'
Check the 'use wildcards' option, then 'replace all'.

A macro equivalent is:
Sub ReFormat()
With Selection.Find
.ClearFormatting
.Text = "(\<?\>)(*)(\</?\>)"
With .Replacement
.Text = "\2"
.ClearFormatting
.Style = ActiveDocument.Styles("G-Italics")
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub


--
Cheers
macropod
[Microsoft MVP - Word]


I have this huge document (3,756 pages with the tags) and I want to
replace
the words between the tags with Word-formatted style, then deleting the
tags, for example I want "<I>any text</I>" to become "any text" in
italics
(a style called G-italics), ditto <I>strange transformations</I> to
become
"strange transformations" in G-italics in the same search. Then doing
another search for <B>...</B> and so forth.

Nisus Writer Pro does have the Find/Replace to do this, but not the
capacity
to go through so many pages, and I'd prefer not to break up this
document,
because I'll have repeat all this over and over again (don't know Perl at
all!!).

I've already got Word to make 75,000 changes for a single accented text
and
tried using Range which did not work because I guess Range is for a
single
part of a text and <I> etc is not a correct term within Range.

Set myRange = ActiveDocument.Range(Start:="<I>", End:="</I>")

Can someone help with this? I have no idea what the VB for this will be,
though I have done quite a few <simple> macros
 
D

Doug Robbins - Word MVP

Hi Paul,

I think however that it is most likely that a different Style or format is
to be applied to each pair of tags.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
macropod said:
make that:
(\<[IB]\>)(*)(\</[IB]\>)

--
Cheers
macropod
[Microsoft MVP - Word]


macropod said:
Hi Doug,

An alternative way to limit the scopy with a single Find/Replace is to
make the Find string:
(\<[IB]\>)(*)(\</[IB]I\>)
adding any other desired formatting expression letters between the square
brackets.

--
Cheers
macropod
[Microsoft MVP - Word]


Doug Robbins - Word MVP said:
Hi Paul,

That would of course format everything within any tags with the
G-Italics style. Probably not what the user wants as in addition to
"<I>...</I>" they want to do another search for <B>...</B>

They would need to do one search for

(\<I\>)(*)(\</I\>)

using the replacement that you mentioned and then another for

(\<B\>)(*)(\</B\>)

with the appropriate replacement.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Hi Sol,

You can dothis without a macro, using Find/Replace.
For the 'Find' expression, use-
(\<?\>)(*)(\</?\>)
For the 'Replace' expression, use-
\2
and format the replacement Style as 'G-Italics'
Check the 'use wildcards' option, then 'replace all'.

A macro equivalent is:
Sub ReFormat()
With Selection.Find
.ClearFormatting
.Text = "(\<?\>)(*)(\</?\>)"
With .Replacement
.Text = "\2"
.ClearFormatting
.Style = ActiveDocument.Styles("G-Italics")
End With
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub


--
Cheers
macropod
[Microsoft MVP - Word]


I have this huge document (3,756 pages with the tags) and I want to
replace
the words between the tags with Word-formatted style, then deleting
the
tags, for example I want "<I>any text</I>" to become "any text" in
italics
(a style called G-italics), ditto <I>strange transformations</I> to
become
"strange transformations" in G-italics in the same search. Then doing
another search for <B>...</B> and so forth.

Nisus Writer Pro does have the Find/Replace to do this, but not the
capacity
to go through so many pages, and I'd prefer not to break up this
document,
because I'll have repeat all this over and over again (don't know Perl
at
all!!).

I've already got Word to make 75,000 changes for a single accented
text and
tried using Range which did not work because I guess Range is for a
single
part of a text and <I> etc is not a correct term within Range.

Set myRange = ActiveDocument.Range(Start:="<I>", End:="</I>")

Can someone help with this? I have no idea what the VB for this will
be,
though I have done quite a few <simple> macros
 

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