Word VBA:从选择中提取表格

时间:2011-12-02 12:51:58

标签: word-vba

我在word文档中有一个选项,我用

设置

With Selection.Find
.Text = "Blala"
.Forward = True
.Wrap = wdFindContinue
.MatchCase = True
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Tada"
.Forward = True
.Wrap = wdFindStop
.MatchCase = True
End With
Selection.Find.Execute

但是在那个选择中还有表格,我只想格式化文本。有没有办法从该选择中排除表格?

谢谢!

1 个答案:

答案 0 :(得分:0)

此代码循环选择内的所有段落,如果该段落中没有表格,则只执行“Blala”替换为“Tada”:

Sub ReplaceTextButSkipTables()

    Dim rng As Range
    Dim p As Paragraph

    Set rng = Selection.Range
    For Each p In rng.Paragraphs
        If p.Range.Tables.Count = 0 Then
            With p.Range.Find
                .ClearFormatting
                .MatchCase = True
                With .Replacement
                    .ClearFormatting
                    .ParagraphFormat.LeftIndent = InchesToPoints(1.27)
                End With
                .Execute FindText:="Blala", ReplaceWith:="Tada", Replace:=wdReplaceAll
            End With
        End If
    Next p

End Sub