如果没有表,那么另外做

时间:2016-04-26 12:10:14

标签: vba ms-word word-vba word-2007 word-2003

我想找到一些text如果这个文本在表中然后将表转换为文本,否则什么都不做

但如果没有Table则会提供Error

Selection.Find.ClearFormatting
With Selection.Find
    .Text = " - ^$^$^$^$ ^$ - ^$^$^$^$^$^$"
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then

Selection.Tables(1).Select ' This is Error position

Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
    True

Else
End If

我希望do

中的loop

2 个答案:

答案 0 :(得分:1)

添加条件,如果Selection.Tables.Count> 0代码

Selection.Find.ClearFormatting
    With Selection.Find
        .Text = " - ^$^$^$^$ ^$ - ^$^$^$^$^$^$"
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
    End With
    Selection.Find.Execute
    If Selection.Find.Found = True Then

    If Selection.Tables.Count > 0 Then
        Selection.Tables(1).Select ' This is Error position

        Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:=True
    End If
End If

问候。

答案 1 :(得分:0)

@nathan_sav谢谢

以下是loop

中的代码
do
Selection.Find.ClearFormatting
With Selection.Find
   .Text = " - ^$^$^$^$ ^$ - ^$^$^$^$^$^$"
   .Forward = True
   .Wrap = wdFindStop
   .Format = True
End With
Selection.Find.Execute
If Selection.Find.Found = True Then

if selection.tables.count>0 then
Selection.Tables(1).Select 

Selection.Rows.ConvertToText Separator:=wdSeparateByTabs, NestedTables:= _
True
else
end if


Else
exit do
End If
loop