使用VBA在Word文档中查找斜体字体 [英] find italic fonts in word document using vba

查看:462
本文介绍了使用VBA在Word文档中查找斜体字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Find函数(Ctrl+F),我可以从文档中搜索并选择斜体字体的所有单词. 用vba怎么办?

With the Find function(Ctrl+F) I can search and select all words in Italicized font from a document. How would this be done with vba?

我尝试了宏记录器,但是到达那里的代码不起作用.

I tried the macro recorder but the code I get there does not work.

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

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

The goal would be to have all italic font words being selected/highlighted in the document.

谢谢, 凯

推荐答案

您可能需要添加:

Selection.Find.Font.Italic = True

那可能变成了:

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

编辑:另一次尝试(虽然未完成)

EDIT: another try (not complete though)

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

但是,替换确实可以正常工作,但是如果没有文本,则突出显示将不起作用.有人有主意吗?

But yet, the replace does work well but highlight does not work if there is no text. Anyone has an idea ?

编辑2 :找到了一个可行的解决方案,即使我并没有设法发挥重要作用

EDIT 2: Found a working solution, even if i did not manage to have hithighlight working though

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

此致

最大

这篇关于使用VBA在Word文档中查找斜体字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆