If there is no Table then do else

87 Views Asked by At

I want to find some text if this text is in table then convert the table into text else do nothing

But when there is no Table it gives an 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

and I want it to do it in loop

2

There are 2 best solutions below

0
Clon On BEST ANSWER

Add a condition, if Selection.Tables.Count > 0 to your code

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

Regards.

0
VBAbyMBA On

@nathan_sav thanks

Following is working code in 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