2011-06-16 130 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