编辑文本时保留邮件合并字段

时间:2015-06-05 22:19:01

标签: vba word-vba

我正在编写一个宏来清理Word中的表。它遍历表,如果给定的单元格包含邮件合并字段,它应该删除单元格中的其余文本并保持邮件合并字段不变。我相信我当前的解决方案不起作用,因为它将占位符标记写为文本而不是保留作为占位符的对象 - 文本看起来正确,但MM不做任何事情。此外,这是在OSX上,所以我无法访问某些库,因此没有正则表达式。我应该如何找到并保留合并字段对象?

Public Sub TableSweep()

Dim oRow As row
Dim oCell As Cell
Dim cellText As String
Dim startInd As Integer
Dim endInd As Integer

'ensure cursor is within table
Selection.Collapse Direction:=wdCollapseStart
If Not Selection.Information(wdWithInTable) Then
    MsgBox "Can only run this within a table."
    Exit Sub
End If

For Each oRow In Selection.Tables(1).Rows

    For Each oCell In oRow.Cells

        cellText = oCell.Range
        startInd = InStr(cellText, "«")
        endInd = InStr(cellText, "»")
        If startInd > 0 And endInd > 0 And endInd > startInd Then
            cellText = (Mid(cellText, startInd, endInd - startInd + 1))
            oCell.Range = cellText
        End If

    Next oCell

Next oRow

End Sub

0 个答案:

没有答案