2011-06-16 94 views
2

找到斜体字体,我可以从文档中搜索并选择斜体字体中的所有单词。 这将如何与vba完成?在word文档中使用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 

文档中的目标将是具有被选择的所有斜体字/高亮显示。

感谢, 凯

回答

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 

问候,

最大

+0

我不认为那去上班。您正在将查找的结果更改为具有斜体字体。更不用说,如实施,该发现声明实际上没有发现任何东西... – 2011-06-16 12:09:03

+0

最后的代码不起作用突出显示查找/替换框中的所有文本,但它提供了一种解决方法。我没有成功地使hithighlight方法工作,对不起。 – JMax 2011-06-21 08:54:21

+0

最后的编辑似乎适合我! – Kay 2012-06-05 10:48:29

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 
+0

感谢您的评论Hari,我正在研究一个工作解决方案。顺便说一句,我明白,凯正在寻找一个MS WOrd解决方案。试着很快回来。 – JMax 2011-06-16 13:41:12

+0

@JMax:我现在看到'MSWord'标签...感觉有点不好意思。感谢您直接设置我... – 2011-06-16 13:46:39

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

2

最后的努力实际上在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 
+0

谢谢 - 我现在正在使用它,使用你的和JMax的代码! – Kay 2012-06-05 10:43:13