Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Sep 2002
    Location
    Detroit, Michigan, USA
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Tables annoying characters (WD2000)

    Upon working with data contained in Word tables, I keep running into characters that are created with the table and apparently have a certain meaning. The ASC codes on those items are always 13 and 7 (although the Tools/Option/Show all box checking shows only one character on screen).

    This behavior can be rather annoying if you are trying to string-operate with the contents of the cell since those characters are always there. I tried to use the approach str=left(str, len(str)-1) to filter off the last one or two digits but the results I get not always are what I expect . Sometimes, depending on how I loaded the data, I even get paragraph marks (even if I loaded the data with a userform...without any human participation).

    Is there a way to consistently get rid of those control characters or a better and more efficient way to deal with raw data in tables?

    Thanks in advance.

  2. #2
    Star Lounger
    Join Date
    Sep 2001
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tables annoying characters (WD2000)

    Hi milonguero

    To receive the text out of a cell I'm normally using a range object:
    Set rng = ActiveDocument.Tables(1).Cell(1, 1).Range
    rng.MoveEnd wdCharacter, -1
    MsgBox rng.Text
    As you can see, I move the end of the range by one character to get rid of the cell paragraph mark.
    This does always work for me, but I would expect, that your approach should work as well, if you can ensure, that "str" is always exactly the text of the cell and nothing else.

    hedgehog

  3. #3
    Star Lounger
    Join Date
    Sep 2002
    Location
    Detroit, Michigan, USA
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tables annoying characters (WD2000)

    I normally use ....<table.cell(row,column).range.text>... ( I don't know why but I think for certain purposes, range alone won't do the trick). Again...yours is an acceptable approach. Maybe I should create a function that will check for the proper ASCI codes and weed out anything that doesn't fall within a certain range....

  4. #4
    Star Lounger
    Join Date
    Sep 2001
    Posts
    83
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tables annoying characters (WD2000)

    Can you be more specific about your requirements? Maybe, I might suggest another approach.

    Regards,
    hedgehog

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Tables annoying characters (WD2000)

    A table cell range contains the terminal control characters just as a paragraph range ends with control characters. You shouldn't have to go to the trouble of checking all of the cell contents, just trim off the end.

  6. #6
    Star Lounger
    Join Date
    Sep 2002
    Location
    Detroit, Michigan, USA
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Tables annoying characters (WD2000)

    True...that's the point. Since data normally comes in diff lengths, I need to evaluate each and every single piece of info in a cell before I can get the "raw" string as explained in previous posts. I was trying to find out whether or not a better approach was available... .

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Tables annoying characters (WD2000)

    If you only want to knock out two or three types of control characters, look up the Replace function. If that's too limiting, you can use the Regular Expressions feature of VBScript to craft your own, more powerful, in-string replace feature.

    <pre>Public Function WildReplace(strExpression As String, strFind As String, _
    strReplace As String, Optional bolReplaceAll As Boolean = True, _
    Optional bolCaseSensitive As Boolean = False) As String
    'requires a reference to Microsoft VBScript Regular Expressions
    'requires VBScript 5 = IE 5.x
    'perform minimal parameter checking
    If (strExpression = vbNullString) Or (strFind = vbNullString) Then
    WildReplace = strExpression
    Exit Function
    End If
    'Dim objRegExp As RegExp
    Dim objRegExp As Object
    'instantiate RegExp object
    'Set objRegExp = New RegExp
    Set objRegExp = CreateObject("vbscript.regexp")
    objRegExp.IgnoreCase = Not bolCaseSensitive
    objRegExp.Global = bolReplaceAll
    objRegExp.Pattern = strFind
    WildReplace = objRegExp.Replace(strExpression, strReplace)
    End Function</pre>

    You'd call this with code similar to:

    <pre>strTemp = WildReplace(strTemp, "rn>*", vbCrLf)</pre>

    This means "return a string in which you've replaced a carriage return/new line pair followed by one or more > symbols with a carriage return/new line pair." It can get way more complicated than that. The VBScript's Regular Expressions syntax is a bit different than that in Perl, which is better documented on the web. You can find some guidance here: Visual Basic Scripting Edition: Regular Expression (RegExp) Object; pay special attention to the Pattern property.

    Note that the RegExp object does not retain rich text attributes. It sounds as though you don't want those anyway.

Posting Permissions

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