#1
|
|||
|
|||
Delete Tables using conditions from Drop Down Field
Hi,
I was trying to work on a Word Document. I have created various dropdown fields in the form. I need a functionality to delete specific tables when a certain dropdown is selected. For example: I have a dropdown field which is named "Parent1" and the dropdown options are Yes and No. I want to delete tables 10 to 14 if the option "No" is selected. I have entered the following code but it is only accepting one condition. Sub Parent() ActiveDocument.Unprotect Select Case ActiveDocument.FormFields("Parent1").Result Case "No": ActiveDocument.Tables(10).Delete End Select ActiveDocument.Protect wdAllowOnlyFormFields, True End Sub But this code can delete only 1 table. I am new to VBA, need help. |
#2
|
||||
|
||||
Try:
Code:
Sub Parent() Application.ScreenUpdating = False Dim i As Long ActiveDocument.Unprotect Select Case ActiveDocument.FormFields("Parent1").Result Case "No" For i = 14 To 10 Step -1 ActiveDocument.Tables(i).Delete Next End Select ActiveDocument.Protect wdAllowOnlyFormFields, True Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank You Paul
This is really helpful. Regards, Tejas |
#4
|
|||
|
|||
Hi Paul,
I was working on this document forun with around 14 different tables, and with different options selected, the tables are to be deleted. For example: if in table 1 there is an option to select yes and no, and if no is selected 4 tables will be deleted from 10 - 13, (this i was able to do with the above code). Now in table 2 we again have this option and if here no is selected the table number 2 and 14 both will be deleted, but if already 10 - 13 tables are deleted the table number 14 will become table 10, So is there a way we can name these 14 tables and then can delete them using reference. Also will there be any issue if the form dropdown is in table 2 and table 2 is to be deleted if "No" is selected from the dropdown. |
#5
|
||||
|
||||
A different way of approaching this issue would be to bookmark the ranges spanning the tables that correspond to each formfield. Then you could use code like:
Code:
Sub Parent() Application.ScreenUpdating = False Dim i As Long With ActiveDocument .Unprotect Select Case .FormFields("Parent1").Result Case "No" With .Bookmarks("BkMk1").Range While .Tables.Count > 0 .Tables(1).Delete Wend End With End Select .Protect wdAllowOnlyFormFields, True End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Thank You Paul,
I have applied the bookmark technique on almost all the dropdowns, its just one dropdown thats bothering me, in this bookmark range, i need to delete table 1 and 3 if criteria 1 matches and delete only table 3 if the other criteria matches. Please help. Code:
Sub Financial() Application.ScreenUpdating = False Dim i As Long With ActiveDocument .Unprotect Select Case .FormFields("Financials1").Result Case "Not Available" With .Bookmarks("Finance1").Range For i = 3 To 1 Step -2 .Tables(i).Delete Next End With Case "Limited" With .Bookmarks("Finance1").Range While .Tables.Count = 3 .Tables(3).Delete Wend End With End Select ActiveDocument.Protect wdAllowOnlyFormFields, True Application.ScreenUpdating = True End Sub |
#7
|
||||
|
||||
So why don't you use two bookmarks, one for the first table, the other for the third table, then use the code I posted previously to process however many tables are in both bookmark ranges for "Not Available" and only the second bookmarked range for "Limited"?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Thanks Paul,
The Code works now. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to delete all empty rows from all tables | braddgood | Word VBA | 15 | 10-02-2015 01:54 PM |
Excel to Word and delete certain tables | jon12355 | Word VBA | 10 | 10-15-2014 03:01 PM |
IF field + Drop-Down list | nightkn8 | Word | 2 | 07-02-2014 02:42 AM |
Form Field Drop Down List | James Ayers | Word | 3 | 07-14-2013 07:53 PM |
using checkboxes to delete tables | atfresh | Word Tables | 1 | 06-19-2011 09:13 PM |