user2340454
user2340454

Reputation: 41

Adding comments through VBA in Word includes page number field

I have a procedure that adds a comment balloon into my Word document, but I have noticed that it also adds a page number field (evident by selecting the comment balloon and selecting update field from the context menu). This only happens when the comment is added through VBA, not when I create comments manually. Is there a way that I can inhibit the page number from being added to the comment?

Code extract below:

With Selection.Find
    .Text = "Approvals"
    .Forward = True
    .Execute

    If .Found = True Then
        Selection.Comments.Add Range:=Selection.Range, Text:="My comment text"
    End If
End With

Upvotes: 4

Views: 1170

Answers (2)

Bruce Jakeway
Bruce Jakeway

Reputation: 299

In case you set field codes in comments, this is a bit more selective in what it deletes. This is designed to run just after you add a particular comment. However, you can combine something similar with the above to work on all comments.

If comment.range.fields.count > 0 Then
    If comment.range.fields.Item(1).Type = wdFieldPage Then
        comment.range.fields.Item(1).Delete
    End If
End If

Upvotes: 0

James C
James C

Reputation: 427

Here's what I used to delete those page fields. After running the bulk of the code, just before End Sub I insert the following:

For Each f In ActiveDocument.StoryRanges(wdCommentsStory).Fields
    If f.Type = wdFieldPage Then
        f.Delete
    End If
Next

It may not be pretty, but it does the job. Unfortunately, I don't think there's a way to filter based on, say, comment author.

For Each c In ActiveDocument.Comments
   If c.Author = "Macro Name" Then 'Assuming you set it when you created the comment
        Debug.Print c.Range.Fields.Count 'This prints a 0
    End If
Next

Upvotes: 1

Related Questions