Macro: Search and replace but retain certain information in result

J

JulesKor

Hi,
I'm trying to set up a Word macro that will search for specific formatting
and wildcard text and will replace the formatting with html code but still
retain the wildcard text.
Our layout program uses a command character represented by the "omega"
symbol (I'll call this OS) which is ascii chr(7) followed by bf (bold face)
if (italic face) nf (normal face), etc.
Eg: "OSbf"Hello"OSnf" or "OSif"Hello"OSnf"
I need this to be replaced with <b>Hello</b> or <i>Hello</i>
I can search for chr(7) & "bf" and replace with "<b>" and I can search for
chr(7) & "if" and replace with "<i>". But it gets confusing when I need to
replace the end chr(7)nf with either "</b>" or "</i>" depending on the
opening tag.
If anyone is able to help, I would be eternally grateful.
 
D

Doug Robbins - Word MVP

You do not really need a macro if you are prepared to do it in two passes,
one for bold and one for italic

For the bold, insert

OSbf"([A-z ]{1,})"OSnf

in the Find what control of the Edit>Replace dialog (with the Use Wildcards
box checked) and in the replace with control, insert

<b>\1</b>

Likewise, for the italic, insert

OSif"([A-z ]{1,})"OSnf

in the Find what control and in the replace with control, insert

<i>\1</i>



--
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
 
J

JulesKor

Hi Doug,

Thanks for your prompt response.

I have a macro that finds and replaces a whole bunch of other things such as
multiple spaces and replaces them with a single space; removes a space at the
start of a paragraph; removes a break-back command; removes a discretionary
hyphen command; removes line advance commands, etc., which works pretty well
so I would like to incorporate this into the Vicual Basic macro.

When I add the following, I get a "compile error: Exptected: expression"
error in both the .Text = Chr(7)bf"([A-z ]{1,})"Chr(7)nf line and the
..Replacement.Text = "<b>\1</b>"

' Selection.Find.Execute Replace:=wdReplaceAll
' With Selection.Find
' .Text = Chr(7)bf"([A-z ]{1,})"Chr(7)nf
' .Replacement.Text = "<b>\1</b>"
' .Forward = True
' .Wrap = wdFindContinue
' .Format = False
' .MatchCase = False
' .MatchWholeWord = True
' .MatchWildcards = True
' .MatchSoundsLike = False
' .MatchAllWordForms = False
' End With

Cheers


Doug Robbins - Word MVP said:
You do not really need a macro if you are prepared to do it in two passes,
one for bold and one for italic

For the bold, insert

OSbf"([A-z ]{1,})"OSnf

in the Find what control of the Edit>Replace dialog (with the Use Wildcards
box checked) and in the replace with control, insert

<b>\1</b>

Likewise, for the italic, insert

OSif"([A-z ]{1,})"OSnf

in the Find what control and in the replace with control, insert

<i>\1</i>



--
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
JulesKor said:
Hi,
I'm trying to set up a Word macro that will search for specific formatting
and wildcard text and will replace the formatting with html code but still
retain the wildcard text.
Our layout program uses a command character represented by the "omega"
symbol (I'll call this OS) which is ascii chr(7) followed by bf (bold
face)
if (italic face) nf (normal face), etc.
Eg: "OSbf"Hello"OSnf" or "OSif"Hello"OSnf"
I need this to be replaced with <b>Hello</b> or <i>Hello</i>
I can search for chr(7) & "bf" and replace with "<b>" and I can search for
chr(7) & "if" and replace with "<i>". But it gets confusing when I need to
replace the end chr(7)nf with either "</b>" or "</i>" depending on the
opening tag.
If anyone is able to help, I would be eternally grateful.
 
D

Doug Robbins - Word MVP

To get the correct syntax, I would suggest that you record a macro while
doing that replace.

--
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
JulesKor said:
Hi Doug,

Thanks for your prompt response.

I have a macro that finds and replaces a whole bunch of other things such
as
multiple spaces and replaces them with a single space; removes a space at
the
start of a paragraph; removes a break-back command; removes a
discretionary
hyphen command; removes line advance commands, etc., which works pretty
well
so I would like to incorporate this into the Vicual Basic macro.

When I add the following, I get a "compile error: Exptected: expression"
error in both the .Text = Chr(7)bf"([A-z ]{1,})"Chr(7)nf line and the
.Replacement.Text = "<b>\1</b>"

' Selection.Find.Execute Replace:=wdReplaceAll
' With Selection.Find
' .Text = Chr(7)bf"([A-z ]{1,})"Chr(7)nf
' .Replacement.Text = "<b>\1</b>"
' .Forward = True
' .Wrap = wdFindContinue
' .Format = False
' .MatchCase = False
' .MatchWholeWord = True
' .MatchWildcards = True
' .MatchSoundsLike = False
' .MatchAllWordForms = False
' End With

Cheers


Doug Robbins - Word MVP said:
You do not really need a macro if you are prepared to do it in two
passes,
one for bold and one for italic

For the bold, insert

OSbf"([A-z ]{1,})"OSnf

in the Find what control of the Edit>Replace dialog (with the Use
Wildcards
box checked) and in the replace with control, insert

<b>\1</b>

Likewise, for the italic, insert

OSif"([A-z ]{1,})"OSnf

in the Find what control and in the replace with control, insert

<i>\1</i>



--
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
JulesKor said:
Hi,
I'm trying to set up a Word macro that will search for specific
formatting
and wildcard text and will replace the formatting with html code but
still
retain the wildcard text.
Our layout program uses a command character represented by the "omega"
symbol (I'll call this OS) which is ascii chr(7) followed by bf (bold
face)
if (italic face) nf (normal face), etc.
Eg: "OSbf"Hello"OSnf" or "OSif"Hello"OSnf"
I need this to be replaced with <b>Hello</b> or <i>Hello</i>
I can search for chr(7) & "bf" and replace with "<b>" and I can search
for
chr(7) & "if" and replace with "<i>". But it gets confusing when I need
to
replace the end chr(7)nf with either "</b>" or "</i>" depending on the
opening tag.
If anyone is able to help, I would be eternally grateful.
 

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