Consulting

Results 1 to 3 of 3

Thread: Word Table Marker Problem

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Word Table Marker Problem

    Untitled.jpg



    Above is a screen shot from a table in Word. I'm trying to remove any extra paragraph markers. Problem is when I search on vbcr it detects one in the first cell and two in the second cell. Seems like the marker to right of "Hello" would have its own ASCII number but it just reflects as a vbcr (13). Any idea on why?

  2. #2
    Set a range to the cell that excludes the last (cell end) character then delete any paragraph breaks from the end of the range e.g.

    Sub Macro1()
    Dim oTable As Table
    Dim oCell As Cell
    Dim oRng As Range
        Set oTable = Selection.Tables(1)
        For Each oCell In oTable.Range.Cells
            Set oRng = oCell.Range
            oRng.End = oRng.End - 1
            Do While oRng.Characters.Last = Chr(13)
                oRng.Characters.Last.Delete
            Loop
        Next oCell
    lbl_Exit:
        Set oRng = Nothing
        Set oCell = Nothing
        Set oTable = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
    Dim lngIndex As Long
      Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
      MsgBox Len(oRng.Text) 'Len returns the "length" of (or number of characters in) the text string and in this case returns 7.
                                      'Hello has only five characters. So the string length of the last end of cell mark MUST be 2!!
      'What are those last 2 characters?
      For lngIndex = 6 To 7
        MsgBox Asc(Mid(oRng.Text, lngIndex, 1))  'Asc character 13 (what we think of as a paragraph mark) and Asc character 7. 
                                                                'The two combined constitute the end of cell mark.
      Next lngIndex
      'Move the range backwards by 1 
      oRng.End = oRng.End - 1
      MsgBox Len(oRng.Text) 'Returns 5 (Hello has five characters). So the "Range" width of the end of cell marker is only 1!!
    lbl_Exit:
      Exit Sub
    End Sub
    That doesn't exactly answer your question, but hopefully illustrates that the end of cell/end of row markers are certainly a bit odd and mysterious. There is no "Thing" we can use in VBA to find the end of cell or end of row markers. However if we understand something about them, then we can achieve most goals like yours (illustrated by Graham above) or because when know that the end of cell\end of row marks have a string length = 2:

    If Len(ActiveDocument.Tables(1).Rows(3).Range.Text = 6 Then Msgbox "The third table row is empty"

    Assuming your illustrated table has just the two columns showing.
    Last edited by gmaxey; 07-02-2020 at 03:17 PM.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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