【问题标题】:Preserving table selection in Word 2003 VBA macro在 Word 2003 VBA 宏中保留表格选择
【发布时间】:2015-10-17 01:05:42
【问题描述】:

我的目标是为 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 中的列选择是如何工作的。

【问题讨论】:

    标签: vba ms-word textselection


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2017-04-03
      • 1970-01-01
      相关资源
      最近更新 更多