none
Auto-count number of characters in a table cell RRS feed

  • Question

  • Hi,

    My company uses some templates we need to fill inn, and these templates contain a number of cells with a fixed character limit - that is, the cell itself has no limit but machine processing further down the line does. This creates all kinds of headaches!

    So...is it possible to make a function that will count the characters in a cell and display something like the following in a cell next to it?

    Max characters: x

    Characters used: y

    Characters left: z

    For any other kind of programming, I would assume the reference would be something like (this.parent.cells[this.cell-1] or something to reference the other cell).

    Anyone got any good tips? We're using Word 2010 (Norwegian)

    Cheers!

    Thursday, June 21, 2012 8:27 AM

Answers

  • Word doesn't provide that kind of interactivity, unless you use a userform. As previously advised, a formfield can be used to limit what can be input. Unlike a userform, this requires no programming.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, June 21, 2012 11:02 AM
  • AFAIK you can do it using the old-style ActiveX forms controls, as long as

     a. your layout/template is fixed (i.e. you do not need to add table rows)

     b. the user knows that they have to type into the control and not "between the control and the edge of the table cell".

    e.g. if you have a 3 column table, and you need the users to type into cells in column 1, you put an ActiveX text box in each cell in column 1,

    then write the code for the _Change event for each textbox in the ThisDocument object for the document, e.g. as a crude example,

    Private Sub TextBox1_Change()
    ActiveDocument.Tables(1).Cell(1, 2).Range.Text = TextBox1.TextLength
    ActiveDocument.Tables(1).Cell(1, 3).Range.Text = 50 - TextBox1.TextLength
    End Sub

    Then ensure the document is not in Design mode. 


    Peter Jamieson

    Saturday, June 23, 2012 8:38 AM

All replies

  • hi lars,

    If you set the form up with formfields, those provide the means to limit the number of characters that can be entered. Depending on your requirements, you can limit others to dates in particular formats, numbers, the checking of checkboxes or selections from a dropdown. This may serve your needs better than truncating data later on in ways that may compromise the utility of whatever's captured.

    That said, with vba you can use code like:

    Dim Rng As Range, i As Long, j As Long, StrText As String
    With ActiveDocument.Tables(1)
      For i = 1 To .Rows.Count
        For j = 1 To .Columns.Count
          Set Rng = .Cell(i, j).Range
          Rng.End = Rng.End - 1
          StrText = Left(StrText, 4)
          MsgBox StrText
        Next
      Next
    End With

    The above code works through each row in the table, limiting the output for each cell to a maximum of 4 characters.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, June 21, 2012 8:54 AM
  • Thanks, but...would it be possible to just display the count for the cell next to it? This is a creative process and it would be much more useful to just warn the users when they've created too much text.
    Thursday, June 21, 2012 10:43 AM
  • Word doesn't provide that kind of interactivity, unless you use a userform. As previously advised, a formfield can be used to limit what can be input. Unlike a userform, this requires no programming.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, June 21, 2012 11:02 AM
  • AFAIK you can do it using the old-style ActiveX forms controls, as long as

     a. your layout/template is fixed (i.e. you do not need to add table rows)

     b. the user knows that they have to type into the control and not "between the control and the edge of the table cell".

    e.g. if you have a 3 column table, and you need the users to type into cells in column 1, you put an ActiveX text box in each cell in column 1,

    then write the code for the _Change event for each textbox in the ThisDocument object for the document, e.g. as a crude example,

    Private Sub TextBox1_Change()
    ActiveDocument.Tables(1).Cell(1, 2).Range.Text = TextBox1.TextLength
    ActiveDocument.Tables(1).Cell(1, 3).Range.Text = 50 - TextBox1.TextLength
    End Sub

    Then ensure the document is not in Design mode. 


    Peter Jamieson

    Saturday, June 23, 2012 8:38 AM