vba 我如何使字符串加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2020016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:11:02  来源:igfitidea点击:

How do i make a string bold

vbaword-vba

提问by Kojof

I'm creating a word document programatically using VBA.

我正在使用 VBA 以编程方式创建一个 Word 文档。

1) I have a a string with value - "Strategy". I want to make it bold and to be displayed in the word document.

1)我有一个带有值的字符串 - “策略”。我想让它加粗并显示在word文档中。

I have tried this below, but the text is never changed:

我在下面尝试过这个,但文本从未改变:

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

Dim strategy As String
strategy = "STRATEGY"
Dim objWdRange As Word.Range


wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Program Files\DailyStrategy.doc")


With wrdDoc
    If wrdDoc.Bookmarks.Exists("MarketCommentry") Then
        wrdDoc.Bookmarks("MarketCommentry").Range.Text =  strategy 

    Set objWdRange = wrdDoc.Content
     With objWdRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "STRATEGY"

        'Make found bold and italic
        With .Replacement.Font
            .Bold = True
            .Italic = True
        End With
        .Execute Replace:=wdReplaceAll
    End With
End With

End If regards

结束 如果问候

Kojo

小条

采纳答案by Doc Brown

EDIT:: I should have better tried in the VBA debugger first, which I did now. This one should work:

编辑::我应该先在 VBA 调试器中尝试一下,我现在已经这样做了。这个应该有效:

With wrdDoc
   Set objWdRange = wrdDoc.Content
    With objWdRange.Find
       .ClearFormatting
       .Text = "STRATEGY"
       .Execute Replace:=wdReplaceNone
   End With
End With

If objWdRange.Find.Found Then
  'Make found bold and italic
  With objWdRange.Font
       .Bold = True
      .Italic = True
  End With
End If