Search code examples
vbams-wordword-2007word-2003

If there is no Table then do else


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


Solution

  • 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.