Consulting

Results 1 to 7 of 7

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

  1. #1
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location

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

    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:=1NoEffect
    2.1.2.Scope.MoveEndunit:= wdParagraph, Count:=1NoEffect
    2.1.3.Scope.MoveEndUntilcset:=vbCr, Count:=1No Effect
    2.1.4.Scope.Expandunit:=wdCharacterNoEffect
    2.1.5.Scope.Expandunit:= wdParagraphNoEffect
    2.1.6.Scope.SetRangeStart:=rng.Start, End:=rng.EndNoEffect

    Any ideas?

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Something like this perhaps:

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

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location
    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?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location
    Quote Originally Posted by gmaxey View Post
    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.

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Newbie
    Joined
    Mar 2014
    Posts
    4
    Location
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •