I am trying to use VBA in Microsoft Word to automatize highly repetative operations on large documents.
The situation is this: I have a technical text containing a lot of special characters, e.g. ❨ (U+2768) and ❩ (U+2769). The text is chiefly typeset in Cambria, but some of the special characters are not found in this font. However, I do know for a fact, that all characters used in the document are present in DejaVu Sans Mono. Hence, I would like all characters to be in Cambria except for those that cannot be displayed in this font -- those characters I want displayed in DejaVu Sans Mono.
In Microsoft Word 2007, I c开发者_StackOverflowould easily achieve this by Ctrl+A, set font to DejaVu Sans Mono, set font to Cambria. The second change of font would only change the font of those characters that can be displayed in Cambria.
However, in Microsoft Word 2010, this apparently does not work. Every single character gets the font Cambria, and instead of displaying the characters not found in Cambria, the boxed question mark symbol is displayed.
To overcome this, I have turned to VBA. It is very easy to change the font of a given character, e.g.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Text = ChrW(10088)
Selection.Find.Replacement.Text = ChrW(10088)
Selection.Find.Replacement.Font.Name = "DejaVu Sans Mono"
Selection.Find.Execute Replace:=wdReplaceAll
However, it is not exceedingly fun to write the above code for each and every character in (C(DejaVu) ∖ C(Cambria)) ∩ C(Doc) where C(DejaVu), C(Cambria), and C(Doc) is the set of all characters in DejaVu Sans Mono, Cambria, and my document, respectively.
Is there any (reasonably simple) way to accomplish this automatically? Surely Microsoft Word knows which characters are found in Cambria and which are not. I want all instances of those characters that are not found in Cambria, to be given the font DejaVu Sans Mono.
I think this answer will help you achieve your goal: Script for changing fonts in a Word document
精彩评论