vba 使用vba在word文档中找到斜体字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6369177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find italic fonts in word document using vba
提问by Kay
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?
使用Find
函数( Ctrl+F
) 我可以从文档中搜索和选择斜体字体的所有单词。这将如何用 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.
目标是在文档中选择/突出显示所有斜体字。
thanks, kay
谢谢,凯
采纳答案by JMax
You may need to add :
您可能需要添加:
Selection.Find.Font.Italic = True
That could became :
那可能变成:
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 ?
但是,替换确实有效,但如果没有文本,则突出显示不起作用。有人有想法吗?
EDIT 2: Found a working solution, even if i did not manage to have hithighlight working though
编辑 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
Regards,
问候,
Max
最大限度
回答by Dallman Ross
The last effort actually works a treat in Word 2010. I'm not sure why the report was that it didn't work.
最后的努力实际上在 Word 2010 中起作用了。我不知道为什么报告是它不起作用。
Here it is changed to ASCIIfy italics, which is what I want for text-based newsgroups:
这里改为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
回答by GolezTrol
Set Selection.Find.Font.Italic = True
.
设置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
Hint for the next time: Record a macro, perform the actions you want to automate, and look what code is recorded. That's how I found this. :D
下次提示:录制宏,执行您想要自动化的操作,并查看录制了哪些代码。我就是这样发现的。:D
[edit]
[编辑]
I see that you tried recording it. Weird that that didn't work.. :-S
我看到你尝试记录它。奇怪的是那不起作用.. :-S
回答by Hari Seldon
You need to iterate through the cells in the range that you want to check, and specifically check if it has its font italicized. AFAIK .Italic
is not a "findable" option.
您需要遍历要检查的范围内的单元格,并特别检查它的字体是否为斜体。AFAIK.Italic
不是“可找到”的选项。
The following code is an example of iterating through the cells to find what you need.
以下代码是遍历单元格以查找所需内容的示例。
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