使用vba在word文档中找到斜体字体

时间:2011-06-16 08:42:08

标签: vba fonts ms-word

使用Find函数(Ctrl+F),我可以从文档中搜索并选择斜体字体中的所有单词。 如何用vba完成这项工作?

我尝试过宏录制器,但是我到那里的代码不起作用。

Sub Makro1()
'
' Makro1 Makro
' Makro aufgezeichnet am 16.06.2011 von u0327336
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
End Sub

目标是在文档中选择/突出显示所有斜体字体。

感谢, 琦

4 个答案:

答案 0 :(得分:3)

最后的努力在Word 2010中实际上是一种享受。我不确定为什么该报告不起作用。

这里改为ASCIIfy斜体,这就是我想要的基于文本的新闻组:

 Sub ASCIIfy()
    Dim myString As Word.Range
    Set myString = ActiveDocument.Content
    With myString.Find
        '// ensure unwanted formats aren't included as criteria
        .ClearFormatting
        '// we don't care what the text is
        .Text = ""
        '// find the italic text
        .Font.Italic = True
        '// loop for each match and surround with "_"
        While .Execute
            myString.Text = "_" & myString & "_"
            myString.Font.Italic = False
            myString.Collapse wdCollapseEnd
        Wend
    End With
End Sub

答案 1 :(得分:2)

您可能需要添加:

Selection.Find.Font.Italic = True

那可能会成为:

With Selection.Find
   .Text = ""
   .FOnt.Italic = True
   'other search stuff
End with

编辑:另一次尝试(虽然不完整)

Sub hilightItalic()
    With ActiveDocument.Content.Find
        ' to ensure that unwanted formats aren't included as criteria
        .ClearFormatting
        'You don't care what the text is
        .Text = ""
        'Find the italic text
        .Font.Italic = True
        'Delete the text found
        .Replacement.Text = ""
        'delete all italic text
        .Execute Replace:=wdReplaceAll
        '.HitHighlight "", vbYellow, vbRed
    End With
End Sub

但是,替换确实运行良好但是如果没有文本则突出显示不起作用。有人有想法吗?

编辑2 :找到一个可行的解决方案,即使我没有设法让hithighlight工作

Sub hilightItalic()
    Dim oRng As Word.Range
    Set oRng = ActiveDocument.Content
    With oRng.Find
        ' to ensure that unwanted formats aren't included as criteria
        .ClearFormatting
        'You don't care what the text is
        .Text = ""
        'Find the italic text
        .Font.Italic = True
        'Loop for each match and set a color
        While .Execute
            oRng.HighlightColorIndex = wdDarkYellow
            oRng.Collapse wdCollapseEnd
        Wend
    End With
End Sub

此致

最高

答案 2 :(得分:2)

设置Selection.Find.Font.Italic = True

Selection.Find.ClearFormatting

' The next line does the trick.
Selection.Find.Font.Italic = True

With Selection.Find
    .Text = "YourText"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute

下次提示:录制宏,执行要自动执行的操作,并查看录制的代码。这就是我发现这一点的方式。 :d

[编辑]

我看到你试过录音了。很奇怪,这不起作用..: - S

答案 3 :(得分:1)

您需要遍历要检查的范围内的单元格,并专门检查其字体是否为斜体。 AFAIK .Italic不是“可查找”选项。

以下代码是迭代单元格以找到所需内容的示例。

Sub TestMe2()

Dim rng As Range

'// change as needed to the proper worksheet reference
With ThisWorkbook.Worksheets(1)

    '// replace the .Range statement with an appropriate range for your data
    For Each rng In .Range(.Cells(1, 1), .Cells(100, 100))

        If rng.Font.Italic = True Then
            '// uses the yellow highlight color, change to suit your needs
            rng.Interior.Color = 65535
        End If

    Next rng
End With

End Sub