+ Reply to Thread
Results 1 to 8 of 8

Cell Comment Truncating String?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Cell Comment Truncating String?

    I form a string and put that string into a selected cell as a comment. Mostly it works fine but as the string gets over a certain length (255 characters?), the string gets "truncated" in that only the characters over that length get put into the comment.

    Here's the code that I use:

    Public Sub getBellTimes()
        Dim cmnt As Comment
        Dim sStr As String
        sStr = "Bell Schedule" + Chr(10) + "    AM" + Chr(10)
    
    ...code to further develope the sStr value...
    
        MsgBox sStr
        MsgBox Len(sStr)
        ActiveSheet.Range("hold").AddComment (sStr)
        Set cmnt = ActiveSheet.Range("hold").Comment
        With cmnt.Shape.TextFrame
            .Characters.Text = ""
            .AutoSize = True
            .Characters.Text = sStr
        End With
    End Sub
    The sStr variable is correctly formed (as seen with the msgBox) prior to the time that I try to put it into the comment.

    Any ideas what I'm doing wrong?

    TIA.

    Bob
    Last edited by bstubbs; 06-11-2009 at 02:56 PM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Cell Comment Truncating String?

    You need to break the string up into sections and pass those to the comment using the Characters.Insert method. See this MSKB article for example. (states it's for Excel 97, but it's still not fixed to the best of my knowledge)
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Cell Comment Truncating String?

    Ok, that makes sense to me. The problem that I next run into, I think, is that, since I'm tied to using cell comment (different than putting the data into the cell with characters.insert routines, right?), I'm limited to comments of less than 255 characters. I can bust the string into manageable subStrings but addComment only works with a blank cell comment as I understand it. Any ideas how I can concatentate the substrings into the comment given the addComment string limitation?

    TIA.

    Bob

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Cell Comment Truncating String?

    Hello Bob,

    I just did some testing and found the maximum character width of a comment is 255 characters. This holds true regardless of the font size. So you would need break the string at multiples of 254 characters and add a vbLf character at the end.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    Re: Cell Comment Truncating String?

    Thanks. What routine did you use to get the less-than-255 strings into the comment field and concantenate them there? Can you provide the proto-typed code?

    Again, thanks for your help.

    Bob

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Cell Comment Truncating String?

    Try this:
    Function AddComment(cell As Range, sText As String, _
                        Optional ByVal bAppend As Boolean, _
                        Optional bAutoSize As Boolean, _
                        Optional vMargin As Variant) As Boolean
        ' Adds or appends text to a comment
        Dim i       As Long
        Dim iBeg    As Long
    
        If cell.Comment Is Nothing Then
            bAppend = False
            cell.AddComment
        ElseIf Not bAppend Then
            cell.Comment.Delete
            cell.AddComment
        End If
    
        With cell.Comment.Shape.TextFrame
            If bAppend Then iBeg = .Characters.Count
    
            For i = 1 To Len(sText) Step 255
                .Characters(iBeg + i).Text = Mid(sText, i, 255)
            Next i
            
            .AutoSize = bAutoSize
            
            If IsArray(vMargin) Then
                .AutoMargins = False
                If UBound(vMargin) = 3 Then
                    .MarginLeft = vMargin(0) * 72
                    .MarginRight = vMargin(1) * 72
                    .MarginTop = vMargin(2) * 72
                    .MarginBottom = vMargin(3) * 72
                End If
            End If
        End With
    End Function
    Last edited by shg; 06-11-2009 at 04:14 PM. Reason: Added some stuff
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1