none
How To Get Comments in a Field with VBA? RRS feed

  • Question

  • Hi

    I inserted a comment on a field this way:

    ThisDocument.Comments.Add ThisDocument.Fields(1).Code, "My Comment"

    I can see the comment displayed in the document. 

    The comment knows about the field:

    Debug.Print ThisDocument.Comments(1).Scope.Fields.Count
     1 

    However, VBA says the field contains 0 comments:

    Debug.Print ThisDocument.Fields(1).Code.Comments.Count
     0 

    How can i retrieve the comments from the field object?

    thx

    Word 2010


    • Edited by johny w Sunday, March 6, 2016 7:50 PM
    Sunday, March 6, 2016 7:41 PM

Answers

  • It's not a bug; it's your method that's flawed:

    Sub Demo()
    Dim rng As Range
    With ActiveDocument
      .Comments.Add .Fields(1).Code, "My Comment"
      Set rng = .Fields(1).Code
      While rng.Characters.Last.Previous <> Chr(21)
        rng.MoveEnd wdCharacter, 1
      Wend
      rng.MoveEnd wdCharacter, -1
      MsgBox rng.Comments(1).Range.Text
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by David_JunFeng Thursday, March 10, 2016 6:53 AM
    • Marked as answer by David_JunFeng Thursday, March 17, 2016 1:41 AM
    Thursday, March 10, 2016 1:32 AM

All replies

  • If not retrieving from the Field object, but from Document,
    how about this:
    ' --- get comment in active document
    Sub GetComment()
        Dim cmt As Comment
        For Each cmt In ActiveDocument.Comments
            Debug.Print cmt.Range.Text
        Next cmt
    End Sub
    ' --- get comment and page number in active document
    Sub GetCommentPage()
        Dim cmt As Comment
        For Each cmt In ActiveDocument.Comments
            With cmt
            Debug.Print _
                .Reference.Information(wdActiveEndAdjustedPageNumber) & _
            vbTab & .Range.Text
            End With
        Next cmt
    End Sub
    Sorry, if I misunderstand your need.
    Monday, March 7, 2016 8:42 AM
  • thx, but that does not answer the question. 

    i want to get the comment attached to a field with:

    ThisDocument.Fields(1).Code.Comments(1)

    thx

    Monday, March 7, 2016 10:32 AM
  • Hi, johny w

    Thanks very much for your sample codes to help us reproduce this issue. 
    I’m able to reproduce this issue now. Unfortunately, I did not see any workaround to overcome this. 
    So I suggest that you could submit any feedback to Word User Voice:

    http://word.uservoice.com/

    Thanks for your understanding.

    Wednesday, March 9, 2016 8:10 AM
  • so you don't consider that a bug?

    we can create a comment inside a field, but cannot retrieve that comment.

    the property 

    ThisDocument.Fields(1).Code.Comments(1)

    does not work. Seems like a bug, to be reported to dev team, not a "feature request" to post on UserVoice. 

    thx

    Wednesday, March 9, 2016 11:12 PM
  • It's not a bug; it's your method that's flawed:

    Sub Demo()
    Dim rng As Range
    With ActiveDocument
      .Comments.Add .Fields(1).Code, "My Comment"
      Set rng = .Fields(1).Code
      While rng.Characters.Last.Previous <> Chr(21)
        rng.MoveEnd wdCharacter, 1
      Wend
      rng.MoveEnd wdCharacter, -1
      MsgBox rng.Comments(1).Range.Text
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by David_JunFeng Thursday, March 10, 2016 6:53 AM
    • Marked as answer by David_JunFeng Thursday, March 17, 2016 1:41 AM
    Thursday, March 10, 2016 1:32 AM
  • If you apply a comment to "something inside a field code*, what Word actually does is apply the comment to the entire result of the field code (you can look at the XML). If the field code is nested inside another field code, Word applies the comment to the outermost field code. Except, maybe, if the field code is an INCLUDETEXT that includes a document that contains a comment - that kind of thing.

    So the problem is that VBA code cannot easily distinguish between a comment that has been applied to the outermost field code, and one which has been applied to something inside that.

    So it's not a bug, but your assumption set should be considered to be flawed until you can demonstrate otherwise.


    Peter Jamieson

    Thursday, March 24, 2016 7:56 PM