views:

43

answers:

1

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 could 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.

+1  A: 

I think this answer will help you achieve your goal: http://stackoverflow.com/questions/2339972/script-for-changing-fonts-in-a-word-document/3127795#3127795

Mahin
How is that going to help with the OP's problem? The linked answer just provides a means to replace one font with another, doesn't it?
Pekka
I guess because my answer was referenced, I'll say that it will would only help if the character is identified in the loop and then replaced. The recursion is definetely faster than `Selection.Find`, which uses a RegEx engine of sorts, which puts the character detection through at least one extra pass. But the above code could also be modified to use an array and something as simple as a `Select Case Case Array(ChrW(10088), ChrW(10087)) Case Else 'Do nothing End Select` and it would get the job done.
Otaku
I was just trying to help. Sorry for all the trouble I caused :|
Mahin