简体   繁体   English

更改 MS Word 宏中选定文本的字体

[英]Change font on selected text in macro for MS Word

I am having to add some functionality to an existing macro for MS Word.我必须为 MS Word 的现有宏添加一些功能。 This particular macro finds a specific text "#code_bar#" in an existing MS Word document and replaces it with a different text ie 3541589479 .这个特定的宏在现有的 MS Word 文档中查找特定文本“#code_bar#”,并将其替换为不同的文本,即3541589479

Now, once this text is found and replaced (which the macro is already doing correctly), it is meant to change the font to "Free 3 of 9 Extended",which is already imported into Word.现在,一旦找到并替换了此文本(宏已经正确执行了此操作),就可以将字体更改为“Free 3 of 9 Extended”,该字体已经导入到 Word 中。

This is the code, which is working properly except the part where I try to change the font and the size, which is actually not taking place.这是代码,除了我尝试更改字体和大小的部分之外,它工作正常,实际上并没有发生。 Could anyone help?有人可以帮忙吗? Thanks.谢谢。

Public Function sustituirCodigoBarras(codigo_barras)

Dim codigoDeBarras As String
Set codigoBarras = ActiveDocument.Content

'#barras_pedido# codigo de barras del pedido

    codigoDeBarras = "#code_bar#"
    With obj_Word.ActiveWindow.Selection.Find
        .Text = codigoDeBarras
        .Replacement.Text = "*" & codigo_barras & "*"
        .Replacement.Font.Name = "Free 3 of 9 Regular"
        .Replacement.Font.Size = 34
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue

    End With

End Function

After some extra digging and a good-night sleep here's the answer:经过一些额外的挖掘和睡个好觉后,答案如下:

  • .Format needs to = true in order for those changes to actually take place .Format 需要 = true 才能实际发生这些更改
  • The font name needs to be correct.字体名称需要正确。 In my case I was trying to use the Free 3 of 9 Extended (which was imported into my computer) and not the Regular (which was not imported and therefore not found) as in the code above.就我而言,我试图使用9 个扩展中免费 3 个(已导入到我的计算机中),而不是像上面的代码中那样使用常规(未导入,因此未找到)。

Rookie mistake.菜鸟失误。

Hope this can help someone else希望这可以帮助别人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM