1

Is there a regular expression solution in Microsoft Word to match cell boundaries?
Recently I had a table with several cells starting with a space and I had to remove them, unfortunately ^ to match beginning of string is available in .NET, but not in word find and replace; also ^p without using wildcards didn't find what I needed.

Similarly I often works with documents, is Word's regex able to manage cell boundaries in any way? (finding text which is spread over multiple cells, match end of cell, look only in tables...)

1

I tried the following approach to avoid leaving Word. This time it is based on Regex available in VBA.

Open Visual Basic editor (Alt+F11)
Add reference:
Tools --> References --> Microsoft VBScript Regular Expressions 5.5

Sub RemoveInitialSpacesInTablesCells()
Dim NoOfRows As Integer
Dim NoOfColums As Integer
Dim CellValue As Variant
Dim RegEx As RegExp
Dim Expr As String
Dim tbl As Table
Dim temp As String

Set RegEx = New RegExp
Expr = "^(\s+)"

For Each tbl In ActiveDocument.Tables
    NoOfRows = tbl.Rows.Count()
    NoOfColums = tbl.Columns.Count()
    For i = 1 To NoOfRows
        For j = 1 To NoOfColums
            CellValue = tbl.Cell(i, j)
            RegEx.Global = True
            RegEx.IgnoreCase = False
            RegEx.MultiLine = False
            RegEx.Pattern = Expr
            temp = RegEx.Replace(CellValue, "")
            If Len(temp) > 2 Then
                temp = Left(temp, Len(temp) - 2)
            End If
            tbl.Cell(i, j).Range.Text = temp
        Next j
    Next i
Next tbl
End Sub

Exemplary outputs from this function
INPUT - represents cell value before the rule is applied
OUTPUT - represent cell value after the rule is applied

INPUT = ""
OUTPUT = ""

INPUT = "             "
OUTPUT = ""

INPUT = "             a"
OUTPUT = "a"

INPUT = "              a
             b"
OUTPUT = "a
             b"

INPUT = "



"
OUTPUT = ""

INPUT = "


c"
OUTPUT = "c"

INPUT = "



a

                          b"
OUTPUT = "a

                          b"

| improve this answer | |
  • Thanks, looks nice! I've upvoted it, but still wait a bit before accepting it as the answer if somebody could find a way without VBA. Referencing to table rows and columns doesn't work in word VBA if some cells are merged, it's generally safer to use for each c in tbl.cells instead. – Máté Juhász Jul 23 '15 at 15:13
1

I tried to recreate the described scenario and following seems to be working for me.

Steps to recreate:
1. Open Word 2007
2. Ribbon.Insert.Table-->2x2 Table
3. Table1.Cell(0,0) = 'a', Table1.C(0,1) = ' b', Table1.Cell(1,0) = ' c', Table1.Cell(1,1) = 'd'
4. Ribbon.Insert.Table-->2x2 Table
5. Table2.Cell(0,0) = ' e', Table2.C(0,1) = 'f', Table2.Cell(1,0) = ' g', Table2.Cell(1,1) = ' h'
6. Office Button.Save As.Other Forats-->Word XML Document (*.xml)
7. Open the file in Notepad++ (my version is 6.6.9)

Observations:
1. Each table sits in <w:tbl> tag regardless of how many tables we've created in Word document
2. If the value does not start with a space it has the following structure <w:t>myValue</w:t>
3. If the value starts with a space then it has the following structure <w:t xml:space="preserve"> myValue</w:t>

Conclusions:
1. It seems that what needs to be removed is ' xml:space="preserve"' AND spaces before myValue
2. Regex replace is required

Solution:
1. In N++ type Ctrl+H (or Menu-->Search-->Replace-->Replace tab-->Search Mode = "Regular expression")
2. In 'Replace With:' leave blank value and in 'Find what:' use this expression '(?<=xml:space="preserve">)(\s+)' (// excluding quotes) and click 'Replace All'
3. In 'Replace With:' leave blank value and in 'Find what:' use this expression ' xml:space="preserve"' and click 'Replace All'
4. Save document
5. Open document in Word
6. Save as doc(x)

| improve this answer | |
  • Thanks! Nice approach indeed. My only problem is that I generally have long documents (100+ pages) with pictures, captions, references and I'm afraid something would be corrupted during the conversion to xml and back. – Máté Juhász Jul 23 '15 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.