简体   繁体   中英

Comment.Add in Word VBA (2019) is inconsistent in adding text to the comment

Context: I am using Word 2019 (Office 365 subscription, if that matters). I have added a series of buttons to my Review ribbon, each set to create a specific comment on some text. (Writing teacher. I make a lot of repeated comments.)

The structure for each comment macro is as follows:

Sub AddSomeComment()
   Dim someComment As String
   someComment = "Long explanation of some revision recommendation."

   ActiveDocument.Comments.Add Range:=Selection.Range, Text:=someComment
End Sub

Problem: The comment balloon is always added, but it is frequently left blank. On some documents, every comment will work (1/10 maybe). On others, the first comment will work but all others will be left blank. A majority of documents will not place the text into the comment. All documents are saved as docx prior to commenting.

I have tried debugging and rewriting the code several times. I've hard coded strings in to each comment and gotten the same results. I have tried using a MsgBox() to verify that someComment has a string in it (it does).

I don't know why it might be behaving like that. Perhaps there's a "race" condition (the text is sent to the comment before it's been created).

Try it like this: create the comment and after it's been created assign the text. (Note: I'm on a mobile device so can't test for syntax errors)

Sub AddSomeComment()
   Dim cmt as Comment
   Dim someComment As String

   someComment = "Long explanation of some revision recommendation."    
   Set cmt = ActiveDocument.Comments.Add Range:=Selection.Range
   cmt.Range.Text = someComment
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM