Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Finding italic text in Word XP (Word XP)

    I have code to find each instance of italic text in a range. It looks something like this:

    Dim r as range, f as font
    set r = selection.range
    set f = New Font
    f.italic = True

    With r.Find
    .text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Font = f
    .format = True

    Do
    .Execute Wrap:=wdFindStop
    If Not .found Then Exit Do
    ' (Do some stuff to the italic text)
    Loop
    End With

    This works in Word 2000. In Word XP it doesn't. The ".Execute" statement never finds anything.

    I tried performing the operation manually. That worked. I tried performing the operation manually and recording a macro so that I could imitate the recorded code. The recorded code didn't refer to the font at all -- just as if I hadn't set it!

    Can anyone tell me how to perform this operation?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Finding italic text in Word XP (Word XP)

    The following works for me:

    Sub Test()
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Font.Italic = True
    .Format = True

    Do
    .Execute Wrap:=wdFindStop
    If Not .Found Then Exit Do
    Debug.Print Selection.Text
    Loop
    End With
    End Sub

    Added: The essential part is

    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""

    This gets rid of previous settings.

  3. #3
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Finding italic text in Word XP (Word XP)

    Hmm... the problem seems to be in ".Font = f" (a Font object). When I substitute that line for ".Font.Italic = True," your code also fails.

    Unfortunately, this knowledge does not lead to an obvious solution. In my real code, "f" is a parameter of the subroutine, and may define any of several properties to be searched for.

    I tried "Set .Font = f" and "Set .Font = f.Duplicate". Both yielded run time errors. ".Font = f.Duplicate" yielded no error, but did not find the match.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Finding italic text in Word XP (Word XP)

    You could use syntax like...

    .Font.Italic = f.italic
    .Font.Bold = f.bold

    In the worst case you would only need about 40 lines to cope with even the most obscure possible font property

    StuartR

  5. #5
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Finding italic text in Word XP (Word XP)

    >In the worst case you would only need about 40 lines...

    In a subroutine which is now 15 lines long.

    I won't have to add that many. With luck, the line count won't even double. :-((

    It's a way to make the program work, at least in the short run. But I hope someone knows a cleaner solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •