下面的代码回答了
我添加注释来描述发生了什么“格式化所有的这些话会发现有斜体至少有一个字母的所有单词,并应用斜体”的问题。我自动化Word很多,我发现它有很多怪癖,所以下面的工作是在一个简单的测试,但你应该彻底测试,然后释放代码到野外。
Public Sub Sample()
Dim WdDoc As Word.Document
Dim WdSlct As Word.Selection
Dim WdFnd As Word.Find
Set WdDoc = ThisDocument
WdDoc.Content.Select
Set WdSlct = Selection
WdSlct.SetRange 0, 0
Set WdFnd = WdSlct.Find
'Clear any previous find settings
WdFnd.ClearAllFuzzyOptions
WdFnd.ClearFormatting
WdFnd.ClearHitHighlight
'Set the find to look for italic text
WdFnd.Font.Italic = True
'Look for any italic character
Do Until Not WdFnd.Execute(FindText:="?", MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop, Format:=True, Replace:=wdReplaceNone)
'Expand the selection to the whole word
WdSlct.Expand wdWord
'Set the word to be italic
WdSlct.Font.Italic = True
'Move past the word
WdSlct.SetRange WdSlct.End, WdSlct.End
Loop
Set WdFnd = Nothing
Set WdSlct = Nothing
Set WdDoc = Nothing
End Sub
从编辑请求改编自@peterv(在OP)
要在脚注,页眉和其他storyranges这项工作,我适应通过与this trick结合加里的解决方案:
Sub RemedyPartialItalics()
Dim WdDoc As Word.Doc
Dim WdFnd As Word.Find
Dim WdRng As Word.Range
Dim WdSlct As Word.Selection
Set WdDoc = ActiveDocument
For Each WdRng In WdDoc.StoryRanges
wdRng.Select
Set WdSlct = Selection
WdSlct.SetRange 0, 0
Set WdFnd = WdSlct.Find
WdFnd.ClearAllFuzzyOptions
WdFnd.ClearFormatting
WdFnd.ClearHitHighlight
WdFnd.Font.Italic = True
Do Until Not WdFnd.Execute(FindText:="?", MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop, Format:=True, Replace:=wdReplaceNone)
WdSlct.Expand wdWord
WdSlct.Font.Italic = True
WdSlct.SetRange WdSlct.End, WdSlct.End
Loop
Set WdFnd = Nothing
Set WdSlct = Nothing
Next
End Sub
另一个问题peterv在哪里?如果我们能够看到该代码并解决该问题,那将会更好。 –
对不起,无法同时发布两个问题。它在这里:http://stackoverflow.com/questions/38742435/some-characters-lose-formatting-in-vba-macro-others-dont – peterv