Share via

Adding Comments to a Word document using VBA

Anonymous
2016-10-26T04:56:35+00:00

I have a macro that I have been working on/improving for a very long time.  The macro works most of the time, but comments interfere with the operation of the macro.

Having been unable to find a way to make the macro work despite comments, what I would like to do is remove the comments from the document and store them somewhere (e.g., in a data structure).  I can then run my macro on the de-commented text, after which I would like to re-insert the comments into their original locations.

Copying the comments into a data structure and deleting them is no problem.  The difficulty is in restoring the comments.  As far as I can tell, the only way to add a comment is using the Comments.Add method to a text range.  That method has only two properties: the range to which the comment applies, and the text of the comment.  It does not (as far as I can tell) permit me to set any other properties: for example, author, date, replies, etc.

Does anyone know a programmatic way to add comments to a document that would let me set all the properties of the comment (or, more preferably, just copy the comment from the temporary data structure into the document somehow)?

Thanks in advance,

Ariel S. Rogson

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

7 answers

Sort by: Most helpful
  1. Anonymous
    2016-10-27T04:00:48+00:00

    Hi, Doug.

    I'm reluctant to share my code for personal reasons, as the code might eventually be a marketable product, if I can get around the problems I have.  (I'm unlikely to actually try and market the code, but I want to retain the possibility.)  In addition, the code in question is probably several hundred, if not a thousand, lines in length, but my question relates to only a small portion of the code.

    This is actually not my first attempt to deal with the problem I have been having, but other posts to the forum have not yielded answers that worked.  I keep trying to solve my problem in different ways, only to find that VBA in Microsoft Word has problems with every approach I have tried.

    To summarize the problem, I am attempting to perform a search and replace on text in a document, where the text meets a pattern that can be expressed as a regular expression (or multiple individual search strings with wildcards using Find and Replace), but the substitute text varies at each instance and does not depend on the text in the original string (that is, the substitute text is not simply a rearrangement of text).  If there is a comment buried in the document, the comment can throw off the search and replacement (however performed), preventing the macro from operating correctly.  ^a as a wildcard search parameter in a VBA Find and Replace sort of works, but not consistently; the VBscript RegExp object removes comments (or, I think, any formatting for that matter) when using the Replace method); and the VBScript RegExp object does not correctly identify where in the text the located strings are when using the Execute method.

    I am open to other ideas how to proceed.

    Thanks,

    Ariel

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-10-27T03:44:02+00:00

    Hi, Ar.  I thought questions about macros were appropriate for this forum: I apologize if I misunderstood the uses of this forum.  I will try the MSDN forums with my question.

    Ariel

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-10-27T02:22:57+00:00

    Ar:

    How is this question outside of the scope of this forum. They are asking for help with a macro.  Granted MSDN or maybe even TECHNET would be better for this technical question.

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2016-10-27T01:48:35+00:00

    If you show us the code of the original macro, someone might be able to come up with a way around the issue caused by the presence of the comments.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2016-10-26T23:26:54+00:00

    Hi Ariel,

    This question is outside the scope of this site and to be sure you get the best answer it should be asked on MSDN.

    http://social.msdn.microsoft.com/Forum

    Regards.

    Was this answer helpful?

    0 comments No comments