MS Word宏可纠正部分格式的单词 [英] MS Word macro to correct partially formatted words
问题描述
我制作了一个宏,它替换了文本中的所有字符,从旧的转录字体到unicode字体.我不知道为什么,但是有些字符会保留原始格式,而另一些字符会丢失格式(在我的情况下,多数为斜体),通常在同一单词内.这给我留下了很多单词,其中有些字母是斜体的,而其他字母不是(例如," al-Malik al-Mu ǧā hidḫuṭ ba ").失去格式的字符都是带有变音符号的字符,但并非所有带有变音符号的字符都失去了格式(例如,示例中的ḫ).
I made a macro that replaces all characters in a text, from old transcription fonts to a unicode font. I don't know why, but some characters keep the original formatting, while others lose the formatting (in my case, mostly italics), often within the same word. This leaves me with a lot of words in which some letters are italicized, and other letters aren't (e.g., "al-Malik al-Muǧāhid ḫuṭba"). The characters that lose the formatting are all characters with diacritics, but not all characters with diacritics lose their formatting (e.g., the ḫ in the example).
查找包含至少一个斜体字母的单词并将所有单词都应用斜体格式的最佳方法是什么?
What would be the best way to find all words that have at least one letter in italics, and apply italics formatting to all those words?
如果有人可以为我指出原始问题的解决方案,那当然会更好(但这是另一个问题的主题:
If someone could point me to a solution for the original problem, that would of course be even better (but that is the subject of another question: some characters lose formatting in vba macro others don't).
推荐答案
下面的代码回答了查找所有至少有一个斜体字母的单词,并将斜体格式应用于所有这些单词"的问题
The below code answers the question of 'find all words that have at least one letter in italics, and apply italics formatting to all those words'
我添加了评论来描述正在发生的事情.我已经对Word进行了很多自动化,并且发现它有很多奇怪之处,因此以下内容在一个简单的测试中起作用,但是在将代码发布到野外之前,您应该进行彻底的测试.
I've added comments to describe what is happening. I've automated Word a lot and I find it to have a lot of quirks, so the below worked in a simple test but you should test thoroughly before releasing code into the wild.
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)的编辑请求进行了修改
Adapted from edit request from @peterv (the OP)
为了在脚注,标题和其他故事情节中进行这项工作,我通过将Gary与
To make this work in footnotes, headers and other storyranges, I adapted Gary's solution by combining it with 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
这篇关于MS Word宏可纠正部分格式的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!