1

I want to format the comment text in Word Template using VBA. I can add simple text using following code. But I don't know how to format the text

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

Output should be like following Image: enter image description here

Nanji Mange
  • 2,155
  • 4
  • 29
  • 63

1 Answers1

2

The code below will add your formatted comment to the selected text. Tested in Word 2007, and functioning as expected.

By the way, in case you need to do something similar in the future, start recording a macro, add your comment, then stop recording. The resulting macro code should get you most of the way there.

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
MJH
  • 2,301
  • 7
  • 18
  • 20
  • This is really great example. But in my case the sequence can be change. I have array of 200 elements. In some cases, there can be only Bold, in some cases there can be only italic. I think I just need to manage few cases and loop to execute your code. Thank you. It will be greatly useful. – Nanji Mange Aug 29 '16 at 13:53
  • I was just providing examples of doing each bit of formatting you mentioned, and wouldn't actually expect you to use my code exactly as-is. If my answer helped you get where you need to be, would you please mark it as Accepted? Thanks. – MJH Aug 29 '16 at 13:58
  • Yeah!. Thanks buddy. – Nanji Mange Aug 30 '16 at 04:24
  • Can you suggest me how to add bullet in text? I have added separate question because I don't know whether I should ask multiple question in same question. Here is the link: http://stackoverflow.com/questions/39292065/how-to-add-bullet-in-comment-using-word-vba – Nanji Mange Sep 02 '16 at 12:09