PDA

View Full Version : Is there any way to include a CR as the last character in the scope of a comment?



tcb
10-24-2016, 07:01 PM
Using the comments.add method in VBA I am inserting all theright comments in all the right places EXCEPT when the last character of thedesired scope of comment is a CR. Inthis case, the comment is inserted but the CR is not included in the scope ofthe comment.

I have tried everything I can think of but cannot make itwork. Here is everything I have tried sofar:
1. Using selection vsa range that I declared and set.
2. Inserting thecomment (and having the CR dropped from the comment scope) then
2.1. Work from thecomment object to the scope property and trying to do a
2.1.1. Scope.MoveEndunit:=wdCharacter, Count:=1 NoEffect
2.1.2. Scope.MoveEndunit:= wdParagraph, Count:=1 NoEffect
2.1.3. Scope.MoveEndUntilcset:=vbCr, Count:=1 No Effect
2.1.4. Scope.Expandunit:=wdCharacter NoEffect
2.1.5. Scope.Expandunit:= wdParagraph NoEffect
2.1.6. Scope.SetRangeStart:=rng.Start, End:=rng.End NoEffect

Any ideas?

gmaxey
10-30-2016, 11:48 AM
Something like this perhaps:

Selection.Comments.Add Selection.Range, Selection.Text

tcb
10-30-2016, 05:05 PM
Selection.Comments.Add Selection.Range, Selection.Text

will add a comment with what ever is highlighted as the text of the comment but if the last character that is highlighted is a CR the CR WILL be in the comment but NOT in the scope of the comment. Maybe an example will work better. In a document I have the sentence:

How now brown cow?<CR>

with the word cow, the question mark and the "<CR>" highlighted. I want to insert the comment "Hello".

Document.comments.add selection.range, "Hello"

will add a comment with the word Hello as the text. If you click on the comment, you will see that the text that the comment points to in the document (i.e. the scope) includes the word cow and the question mark but NOT the <CR>. I want to have the scope include the <CR>.

Any other suggestions?

gmaxey
10-30-2016, 06:09 PM
For whatever reason, that doesn't appear to be possible. Since users may or may not display paragraph marks, what is the purpose of what you want to do. Maybe there is an alternative.

tcb
11-02-2016, 05:26 PM
For whatever reason, that doesn't appear to be possible. Since users may or may not display paragraph marks, what is the purpose of what you want to do. Maybe there is an alternative.
What I am actually working on is a macro to find unneeded white space in word documents. Searching for things like spaces and/or tabs at the end of a paragraph, unneeded <CR> at the end of a table cell, and <TAB>, <NBSP>, <FF>, <CR> etc. at the end of a document.

The macro will have the option to automatically delete the unneeded white OR insert a comment showing where the white space is (i.e. the scope of the comment). Some people will allow me to edit the document contents and others are not comfortable with me doing any edits, hence the option to insert comments. Everything works fine EXCEPT when the last character of the scope is a <CR>.

Note: The macro is predicated on the fact that <CR> etc. will be shown. Macro checks the setting and will turn the option on if necessary.

gmaxey
11-04-2016, 06:33 AM
Without seeing your code and a sample of text that illustrates the issue, I can't offer any further suggestions. Looks like you have been pretty thorough in your efforts so I may still have nothing to offer if you did provide.

tcb
11-22-2016, 09:33 AM
For any one still following this thread, I found a solution. I feel like it is a bit of a kludge but it does work.

Private Sub insertComment(ByVal doc As Document, ByVal rng As Range, ByVal commentText As String)
Dim cmnt As Comment
Dim rng2 As Range

If Right(rng.Text, 1) = vbCr Then 'if range ends in <CR> then
Set rng2 = rng.Duplicate ' duplicate the range
rng.InsertAfter ("X") ' insert a character after the <CR>
rng2.MoveEnd Unit:=wdCharacter, Count:=1 ' expand range to include the X
Set cmnt = doc.Comments.Add(Range:=rng2, Text:=commentText) ' insert the comment (scope includes the X)
rng2.Collapse direction:=wdCollapseEnd ' collapse range to end
rng2.MoveStart Unit:=wdCharacter, Count:=-1 ' move start back 1 character
rng2.Delete ' delete the X (thus leaving the <CR> at end of scope
Set rng2 = Nothing ' free memory
Else 'else
Set cmnt = doc.Comments.Add(Range:=rng, Text:=commentText) ' insert the comment
End If 'endif
Set cmnt = Nothing 'free memory
End Sub


Sorry about the formatting; I can't seem to get the CODE tags to work properly.