保留Word 2003 VBA宏中的表选择

时间:2015-07-26 23:34:29

标签: vba word-vba textselection

我的目标是为Word 2003编写VBA宏,其中用户选择表的一部分(尤其是列),并且宏将输入字符映射到特定的输出字符,例如,任何 a e i o u 成为 V ;某些序列如 eh uw 成为 V ;删除一个字符(感叹号);什么都没有变成" V"变成了" C"。我的问题是,在第一次替换之后,选择会被取消"取消设置",因此更改会影响原始选择以外的其他内容。

    With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .MatchWildcards = True
            .Replacement.Text = "V"
            .Text = "[aeiouáéíóú]"
            .Execute Replace:=wdReplaceAll
'replace certain sequences
            .Text = "[mn" & ChrW(618) & ChrW(650) & "]" & ChrW(769)
            .Execute Replace:=wdReplaceAll
            .Text = "[mn]" & ChrW(768)
            .Execute Replace:=wdReplaceAll
'delete !
            .Text = "[\!]"
            .Replacement.Text = ""
            .Execute Replace:=wdReplaceAll
'everything else becomes C
            .Text = "[!V]"
            .Replacement.Text = "C"
            .Execute Replace:=wdReplaceAll
    End With

如何查找/替换仅对所选单元格进行操作?我注意到在第一次替换后,Selection.End更改为与Selection.Start相同的值。我不明白Word中的列选择是如何工作的。

1 个答案:

答案 0 :(得分:0)

我创建了一些宏来促进这一点。这将开始回答如何移动列。

Sub GoToTop()
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument      ' This takes the pointer to the body of the document ' place cursor the body of the document in case you are located on the header
Selection.EndKey Unit:=wdStory 'key ctrl end
Selection.HomeKey Unit:=wdStory 'key ctrl end
End Sub


Sub GoToColumnTable() 'place cursor inside of the first column
    Selection.GoTo What:=wdGoToTable
End Sub

Sub ColumnMove() 'move from one column to the other one
    Selection.Move Unit:=wdColumn, Count:=1
End Sub
Sub ColumnSelect() 'select the entire column in which the cursor is
    Selection.SelectColumn
End Sub
Sub ColumnDelete() 'delete a column that was selected
    Selection.Columns.Delete
End Sub