none
How to add a comment for each caption label present in a document? RRS feed

  • Question

  • Hello everyone,

    I am trying to write a script to add comments for each caption label for figure and table.

    How can I select a caption and use Comments object for adding a comment?

    I tried using AutoCaptions list for going through all captions present in a document but Comments.Add function is not available with Caption object type.

    When I try adding a comment by selection manually I am able to add. Here is my piece of code.

    For Each objCaption In ActiveDocument.AutoCaptions
          objCaption.Comments.Add Range:=Para.Range, text:="[" & sIdLabel & sCurrentNumber & "]" & vbCrLf  ' ID‘}
          ' increment number for next TAG
          sCurrentNumber = Format(Val(sCurrentNumber) + Val(sStepNumber), String(Len(sCurrentNumber), "0"))

    Monday, April 20, 2015 10:42 AM

Answers

  • Try this code instead:

    Sub AddTextCommentInParaWithSEQField()
    'Macro created 26 April 2015 by Stefan Blom, Word MVP
    Dim f As Field
    Dim r As Range
    For Each f In ActiveDocument.Fields
    If f.Type = wdFieldSequence Then
    Set r = f.Code.Paragraphs(1).Range
    ActiveDocument.Comments.Add Range:=r, Text:="Add your text here"
    End If
    Next f
    Application.ScreenRefresh
    End Sub
    


    Stefan Blom, Microsoft Word MVP

    Sunday, April 26, 2015 3:08 PM

All replies

  • If you are saying that you want to add your comments at the end of a paragraph containing captions (= SEQ fields), try the following code (on a copy of the document just in case):

    Sub AddTextCommentInParaWithSEQField()
    Dim f As Field
    Dim r As Range For Each f In ActiveDocument.Fields If f.Type = wdFieldSequence Then Set r = f.Code.Paragraphs(1).Range r.End = r.End - 1 r.Font.Bold = True r.InsertAfter " [Add text here]" End If Next f End Sub


    Stefan Blom, Microsoft Word MVP

    Wednesday, April 22, 2015 6:18 PM
  • Hello Stefan,

    Thanks for your response.

    I'm trying to add comments for each table and figure present in my document.

    I have earlier tried a script to add comments for each outline level and it works.

    When I tried adding a feature to add comments for each caption in the document, it didn't work.

    Apparently when I try to go through the document by each para, captions are selected as para. So my current script is not able to parse though all the captions.

    Then I went with the other option where I'm trying to use AutoCaption object list to go through each caption. In this Caption object do not have Comment.Add function.

    Friday, April 24, 2015 2:45 PM
  • Try this code instead:

    Sub AddTextCommentInParaWithSEQField()
    'Macro created 26 April 2015 by Stefan Blom, Word MVP
    Dim f As Field
    Dim r As Range
    For Each f In ActiveDocument.Fields
    If f.Type = wdFieldSequence Then
    Set r = f.Code.Paragraphs(1).Range
    ActiveDocument.Comments.Add Range:=r, Text:="Add your text here"
    End If
    Next f
    Application.ScreenRefresh
    End Sub
    


    Stefan Blom, Microsoft Word MVP

    Sunday, April 26, 2015 3:08 PM
  • Thanks a lot. This works just as I expected.
    Wednesday, April 29, 2015 9:53 AM
  • You are welcome. Thanks for the feedback.

    Stefan Blom, Microsoft Word MVP

    Wednesday, April 29, 2015 9:59 AM