我需要清理文档中所有表格单元格开头的所有空格。
我尝试使用并编辑下面链接中提到的 VBA 宏,但无法让它工作
这个
Sub TrimCellSpaces()
Dim myRE As New RegExp
Dim itable As Table
Dim C As Cell
myRE.Pattern = "\s+(?!.*\w)"
For Each itable In ThisDocument.Tables
For Each C In itable.Range.Cells
With myRE
C.Range.Text = .Replace(C.Range.Text, "")
End With
Next
Next
子目录结束
你能帮助我吗
答案1
正则表达式模式\s+(?!.*\w)
会查找尾随空格。您需要对其进行修改。由于您要删除单元格开头的空格,因此正则表达式模式必须是^\s+
。您还需要将 Global 和 Multiline 属性设置为 True。因此代码将是:
Sub TrimCellSpaces()
Dim myRE As New RegExp
Dim itable As Table
Dim C As Cell
With myRE
.Pattern = "^\s+"
.Global = True
.Multiline = True
For Each itable In ThisDocument.Tables
For Each C In itable.Range.Cells
C.Range.Text = .Replace(C.Range.Text, "")
Next
Next
End With
End Sub