如何使字符串变粗

时间:2010-01-07 11:50:47

标签: vba word-vba

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

1)我有一个有价值的字符串 - “策略”。我想把它变成粗体并显示在word文档中。

我在下面尝试了这个,但文字永远不会改变:

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

结束如果 问候

Kojo

1 个答案:

答案 0 :(得分:1)

编辑::我应该首先尝试使用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