Delete table and text box in current section

A

andreas

Dear Experts:

I would like to delete ...
the only text box and the only table in the section where the cursor
resides using VBA.
If either no text box or no table is found in the current section, a
msgbox has to say so and the macro is to exit.


Help is much appreciated. Thank you very much in advance for your
professional help.

Regards, Andreas
 
F

Fumei2 via OfficeKB.com

Sub DeleteFF_Table()
Dim oTable As Table
Dim oFF As FormField
Dim j As Long

j = Selection.Sections(1).Index

For Each oFF In ActiveDocument.Sections(j).Range.FormFields
oFF.Delete
Next
For Each oTable In ActiveDocument.Sections(j).Range.Tables
oTable.Delete
Next
End Sub

This deletes all formfields and all tables from the Section the cursor is in.

Oh, right you wanted a counter. That makes it a bit longer.

Sub DeleteFF_Table()
Dim oTable As Table
Dim oFF As FormField
Dim j As Long
j = Selection.Sections(1).Index

If ActiveDocument.Sections(j).Range.FormFields.Count > 0 Then
For Each oFF In ActiveDocument.Sections(j).Range.FormFields
oFf.Delete
Next
Else
Msgbox "No formfields in this Section."
End If
If ActiveDocument.Sections(j).Range.Tables.Count > 0 Then
For Each oTable In ActiveDocument.Sections(j).Range.Tables
oTable.Delete
Next
Else
Msgbox "No tables in this Section."
End If
End Sub

Of course you can also combine the two messages if you like, or give just one.
..something like:

"No tables found in this Section. Three formfields deleted."

Or whatever.

Gerry
 
A

andreas

Sub DeleteFF_Table()
Dim oTable As Table
Dim oFF As FormField
Dim j As Long

j = Selection.Sections(1).Index

For Each oFF In ActiveDocument.Sections(j).Range.FormFields
   oFF.Delete
Next
For Each oTable In ActiveDocument.Sections(j).Range.Tables
   oTable.Delete
Next
End Sub

This deletes all formfields and all tables from the Section the cursor isin.

Oh, right you wanted a counter.  That makes it a bit longer.

Sub DeleteFF_Table()
Dim oTable As Table
Dim oFF As FormField
Dim j As Long
j = Selection.Sections(1).Index

If ActiveDocument.Sections(j).Range.FormFields.Count > 0 Then
    For Each oFF In ActiveDocument.Sections(j).Range.FormFields
       oFf.Delete
   Next
Else
   Msgbox "No formfields in this Section."
End If
If ActiveDocument.Sections(j).Range.Tables.Count > 0 Then
   For Each oTable In ActiveDocument.Sections(j).Range.Tables
      oTable.Delete
   Next
Else
   Msgbox "No tables in this Section."
End If
End Sub

Of course you can also combine the two messages if you like, or give justone.
.something like:

"No tables found in this Section.  Three formfields deleted."

Or whatever.

Gerry

Hi Gerry,

sorry for the late reply. I failed to track this question. It is
working as desired. Exactly what I wanted. Thank you very much in
advance. Regards, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top