Search code examples
vbams-worditerationcelltrim

How to trim table cell values in MS Word with VBA


I have a table in a Word file. This vba program below will iterate through all the table cells

Dim strCellText As String
Dim uResp As String
Dim Row As Integer
Dim Col As Integer

Dim itable As Table


For Each itable In ThisDocument.Tables

    uResp = ""

    For Row = 1 To itable.Rows.Count

        For Col = 1 To itable.Columns.Count

            strCellText = itable.Cell(Row, Col).Range.Text
            uResp = uResp & Trim(strCellText)                

        Next

    Next

    MsgBox uResp
Next

In the line uResp = uResp & Trim(strCellText) VBA appends a newline and a dot to each cell.So MsgBox uResp will display a 'column' message box.How can I remove the newline and dot while displaying the message box.


Solution

  • Word uses two characters as the cell delimiter, so use

    uResp = uResp & Trim(left(strCellText,len(StrCellText)-2))                
    

    In this case, you will have no separator between the cell values, so you might want to concatenate a space.