Microsoft Word:更改默认字体中缺少的字符的字体

发布于 2024-09-14 09:39:50 字数 1085 浏览 3 评论 0原文

我正在尝试在 Microsoft Word 中使用 VBA 自动执行大型文档的高度重复操作。

情况是这样的:我有一篇技术文本,其中包含很多特殊字符,例如❨(U+2768)和❩(U+2769)。文字主要用Cambria排版,但有些特殊字符在该字体中找不到。然而,我确实知道一个事实,文档中使用的所有字符都存在于 DejaVu Sans Mono 中。因此,我希望所有字符都在 Cambria 中,除了那些无法以这种字体显示的字符 - 我希望在 DejaVu Sans Mono 中显示的那些字符。

在 Microsoft Word 2007 中,我可以通过 Ctrl+A 轻松实现此目的,将字体设置为 DejaVu Sans Mono,将字体设置为 Cambria。第二次更改字体只会更改 Cambria 中可以显示的字符的字体。

然而,在 Microsoft Word 2010 中,这显然不起作用。每个字符都使用 Cambria 字体,并且不会显示 Cambria 中未找到的字符,而是显示带框的问号符号。

为了克服这个问题,我转向了 VBA。更改给定字符的字体非常容易,例如

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

,但是,为 (C(DejaVu) ∖ C(Cambria)) ∩ C(Doc) 中的每个字符编写上述代码并不是很有趣,其中 C (DejaVu)、C(Cambria) 和 C(Doc) 分别是 DejaVu Sans Mono、Cambria 和我的文档中所有字符的集合。

有没有什么(相当简单)的方法可以自动完成这个任务?当然,Microsoft Word 知道哪些字符可以在 Cambria 中找到,哪些不能。我希望在 Cambria 中找不到这些字符的所有实例都被赋予字体 DejaVu Sans Mono。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性 2024-09-21 09:39:50

我认为这个答案将帮助您实现您的目标: 脚本用于更改 Word 文档中的字体

I think this answer will help you achieve your goal: Script for changing fonts in a Word document

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文