如何在Word VBA中的注释文本中添加set text bold,italic,super script和sub script?

时间:2016-08-29 06:44:00

标签: vba word-vba

我想使用VBA格式化Word模板中的注释文本。我可以使用以下代码添加简单文本。但我不知道如何格式化文本

CommentText = "Test Bold: Bold Text & vbNewLine & Test Italic: Italic Text & vbNewLine & Test Bold Italic: Bold Italic Text & vbNewLine &  Test Superscript: My BrandTM & vbNewLine & Test Subscript: H2O"
Selection.Comments.Add Range:=Selection.Range
With Selection
        .TypeText (CommentText)                        'Add comment text
End With

输出应如下图所示: enter image description here

1 个答案:

答案 0 :(得分:2)

下面的代码会将您的格式化评论添加到所选文本中。在Word 2007中测试,并按预期运行。

顺便说一句,如果您将来需要做类似的事情,请开始录制宏,添加评论,然后停止录制。生成的宏代码应该可以帮助您完成大部分工作。

Public Sub AddComment()

  Selection.Comments.Add Range:=Selection.Range

  With Selection
    .TypeParagraph

    .TypeText Text:="Test Bold: Bold Text"
    .MoveLeft Unit:=wdCharacter, Count:=9, Extend:=wdExtend
    .Font.Bold = wdToggle
    .EndKey Unit:=wdLine
    .Font.Bold = wdToggle

    .TypeParagraph

    .TypeText Text:="Test Italic: Italic Text"
    .MoveLeft Unit:=wdCharacter, Count:=11, Extend:=wdExtend
    .Font.Italic = wdToggle
    .EndKey Unit:=wdLine
    .Font.Italic = wdToggle

    .TypeParagraph

    .TypeText Text:="Test Bold Italic: Bold Italic Text"
    .MoveLeft Unit:=wdCharacter, Count:=16, Extend:=wdExtend
    .Font.Bold = wdToggle
    .Font.Italic = wdToggle
    .EndKey Unit:=wdLine
    .Font.Italic = wdToggle
    .Font.Bold = wdToggle

    .TypeParagraph

    .TypeText Text:="Test Superscript: My BrandTM"
    .MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdExtend
    .Font.Superscript = True
    .EndKey Unit:=wdLine
    .Font.Superscript = False

    .TypeParagraph

    .TypeText Text:="Test Subscript: H20"
    .MoveLeft Unit:=wdCharacter, Count:=1
    .MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    .Font.Subscript = True
    .EndKey Unit:=wdLine
    .Font.Subscript = False
  End With

End Sub