turning itlics on and off with a macro

M

Mike O.

I have a string of text that I would like a macro to
selectively italicize. An example of the text is as
follows (these are botanical names):

{\i Salix exigua} ssp. {\i exigua} var. {\i exigua}
{\i Salix wolfii} var. {\i idahoensis}
{\i Centaurea repens} 'Summer Beauty'
{\i Prunus laurocerasus} var. {\i zabeliana}

The macro should turn italicizing on at {\i and off at }

Is this possible? If not, is it possible to do a search-
and-replace?

Thank you very much.
 
J

Jay Freedman

Mike said:
I have a string of text that I would like a macro to
selectively italicize. An example of the text is as
follows (these are botanical names):

{\i Salix exigua} ssp. {\i exigua} var. {\i exigua}
{\i Salix wolfii} var. {\i idahoensis}
{\i Centaurea repens} 'Summer Beauty'
{\i Prunus laurocerasus} var. {\i zabeliana}

The macro should turn italicizing on at {\i and off at }

Is this possible? If not, is it possible to do a search-
and-replace?

Thank you very much.

Hi Mike,

Either way. You can do it with a wildcard replacement, and you can have a
macro that carries out exactly that replacement.

To do it manually, open the Replace dialog, click the More button, and check
the Use Wildcards box. Copy this odd-looking string and paste it in the Find
What box (use Ctrl+V):
\{\\i ([!\}]@)\}
Type this in the Replace With box:
\1
and press Ctrl+I to turn on italic formatting. Click the Replace All button,
and you're all set.

To see how it works, compare these bits to
http://word.mvps.org/FAQs/General/UsingWildcards.htm. The central expression
[!\}] means "any character that isn't }". (The backslash means "treat this
as an actual brace, not as a special serch character".) The @ after it means
"one or more of those characters". The () around all that means "this stuff
will be expression \1 in the replacement". The stuff before and after
matches to the leading {\i and the trailing }, all of which will *not* be
included in the replacement.

The equivalent macro is this:

Sub ItalicTaxonomy()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Forward = True
.Format = False
.Wrap = wdFindContinue
.Text = "\{\\i ([!\}]@)\}"
.Replacement.Text = "\1"
.Replacement.Font.Italic = True
.Execute Replace:=wdReplaceAll
End With
End Sub

For installation instructions if necessary, see
http://www.gmayor.com/installing_macro.htm.
 
M

Mike O

Thank you for the very quick reply!

Mike O.
-----Original Message-----
Mike said:
I have a string of text that I would like a macro to
selectively italicize. An example of the text is as
follows (these are botanical names):

{\i Salix exigua} ssp. {\i exigua} var. {\i exigua}
{\i Salix wolfii} var. {\i idahoensis}
{\i Centaurea repens} 'Summer Beauty'
{\i Prunus laurocerasus} var. {\i zabeliana}

The macro should turn italicizing on at {\i and off at }

Is this possible? If not, is it possible to do a search-
and-replace?

Thank you very much.

Hi Mike,

Either way. You can do it with a wildcard replacement, and you can have a
macro that carries out exactly that replacement.

To do it manually, open the Replace dialog, click the More button, and check
the Use Wildcards box. Copy this odd-looking string and paste it in the Find
What box (use Ctrl+V):
\{\\i ([!\}]@)\}
Type this in the Replace With box:
\1
and press Ctrl+I to turn on italic formatting. Click the Replace All button,
and you're all set.

To see how it works, compare these bits to
http://word.mvps.org/FAQs/General/UsingWildcards.htm. The central expression
[!\}] means "any character that isn't }". (The backslash means "treat this
as an actual brace, not as a special serch character".) The @ after it means
"one or more of those characters". The () around all that means "this stuff
will be expression \1 in the replacement". The stuff before and after
matches to the leading {\i and the trailing }, all of which will *not* be
included in the replacement.

The equivalent macro is this:

Sub ItalicTaxonomy()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Forward = True
.Format = False
.Wrap = wdFindContinue
.Text = "\{\\i ([!\}]@)\}"
.Replacement.Text = "\1"
.Replacement.Font.Italic = True
.Execute Replace:=wdReplaceAll
End With
End Sub

For installation instructions if necessary, see
http://www.gmayor.com/installing_macro.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP


.
 

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