Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Italic Text

Status
Not open for further replies.

aw23

Programmer
Nov 26, 2003
544
IL
I have a word document with several words or paragraphs dispersed that are in italics. I need to find these Italics. How do I go about doing this? The following is my code which does not work:

Dim italicText As String
italicText = Selection.Font.Italic
With Selection.Find
.ClearFormatting
.Text = italicText
If .Execute = True Then
MessageBox.Show ("Text found.")
Else
MessageBox.Show ("The text could not be located.")
End If
End With

Thanks!


 
Have you tried the macro recorder ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yes, that's how I got selection.font.italic
However, I am getting an error, it is not finding it.
 
Hi aw23,

The posted code has not come from the Macro Recorder, not directly anyway.

Font.Italic is Boolean; it is either True or False. When you assign it to your String variable you get the string representation of True ("-1") or False ("0") depending on the font of the Selection BEFORE the Find.

Having got a string, you are searching for it as Text and either finding it or not depending on the content of your document, but you are not looking for italicised text at all.

I just recorded looking for italics and got this ..

Code:
[blue]Sub Macro1()
[green]'
' Macro1 Macro
' Macro recorded 17/05/2004 by Tony
'[/green]
    Selection.Find.ClearFormatting
    Selection.Find.Font.Italic = True
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
End Sub[/blue]

There are a couple of significant differences. The Font.Italic Property of the Find Object is set to True, and the Text being searched for is set to an empty string, with these two lines of code ..

[blue][tt] Selection[highlight].Find[/highlight].Font.Italic = [highlight]True[/highlight][/tt][/blue]

.. and ..

[blue][tt] .Text = ""[/tt][/blue]

One further point about your code is the [purple][tt]MessageBox.Show[/tt][/purple] statement. The correct VBA statement is just [blue][tt]MsgBox[/tt][/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thank you Tony,
I haven't looked at my code in a while. I'm going back to it now. I have a couple of questions. First of all, how did you record such a macro? Second, why is the text being search for set to ""? I need to put the found text in a variable once I find it because I want to add italic tages.

For example italicText="SomeText"
I want to replace it with italicText="<i>SomeText</i>"
Wouldn't it be something like this?

.Text = italicText
.Replacement.Text = "<i>"&italicText&"</i>"

Thanks
 
Hi aw23,

I recorded (Tools > Macros > Record > OK):

Ctrl-F (open Find Dialog)
{More > } Format > Font > Italic > OK > Find Next

It is searching for "" (i.e. no particular text) - just ANYTHING in italics. After the Find has been Executed (successfully - i.e. Selection.Find.Found = True), the found italic text is the Selection.

I have had great difficulty trying to get the Replace option to work as I want when searching for formatted text like this so I would suggest you do the replacement by adding a line like t his after the .Execute ..

[blue][tt]Selection.TypeText "<i>" & Selection & "</i>"[/tt][/blue]

and leave both .Text and .Replacement.Text empty

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thanks so much but that doesn't work. It's replacing the text with "".
 
Also, it's finding all italic text and all breaks between paragrahs. Any suggestion on how to avoid that?

Thanks
 
All right. I got it, here's the code:


Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = "<i>^&</i>"
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
 
Hi aw23,

What is replacing text with ""? Can you post the exact code you're using?

There might be a little bit of trial and error here, depending on how your document is formatted and what you want to achieve. Are you combining this with some other formatting?

The Find with [purple].Text=""[/purple] should find a block of italic code and Select it - that block of text might include paragraph marks. You might be able to avoid that by either working with a character at a time (which could be very slow) or possibly working on each paragraph separately but I would need to experiment to get it working so, again, can you give as much detail as possible please?

Or, but I'd prefer to avoid this, can you e-mail me a sample document to Tony at Jollans dot com.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top