Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2014, 04:55 AM
tejaspareek tejaspareek is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 64bit
Novice
Delete Tables using conditions from Drop Down Field
 
Join Date: Oct 2014
Posts: 13
tejaspareek is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 10-20-2014, 05:12 AM
macropod's Avatar
macropod macropod is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
PS: When posting code, please use the code tags (added via the # symbol on the posting menu).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-20-2014, 05:16 AM
tejaspareek tejaspareek is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 64bit
Novice
Delete Tables using conditions from Drop Down Field
 
Join Date: Oct 2014
Posts: 13
tejaspareek is on a distinguished road
Default

Thank You Paul

This is really helpful.

Regards,
Tejas
Reply With Quote
  #4  
Old 10-30-2014, 05:18 AM
tejaspareek tejaspareek is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 64bit
Novice
Delete Tables using conditions from Drop Down Field
 
Join Date: Oct 2014
Posts: 13
tejaspareek is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 10-30-2014, 06:03 AM
macropod's Avatar
macropod macropod is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
where 'BkMk1' is the bookmark name. That way, it doesn't matter whether tables in any other ranges have been deleted - the code looks only at its own range.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 10-30-2014, 07:24 AM
tejaspareek tejaspareek is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 64bit
Novice
Delete Tables using conditions from Drop Down Field
 
Join Date: Oct 2014
Posts: 13
tejaspareek is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 10-30-2014, 02:04 PM
macropod's Avatar
macropod macropod is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 11-02-2014, 10:24 PM
tejaspareek tejaspareek is offline Delete Tables using conditions from Drop Down Field Windows 7 64bit Delete Tables using conditions from Drop Down Field Office 2010 64bit
Novice
Delete Tables using conditions from Drop Down Field
 
Join Date: Oct 2014
Posts: 13
tejaspareek is on a distinguished road
Default

Thanks Paul,

The Code works now.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete Tables using conditions from Drop Down Field 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
Delete Tables using conditions from Drop Down Field IF field + Drop-Down list nightkn8 Word 2 07-02-2014 02:42 AM
Delete Tables using conditions from Drop Down Field Form Field Drop Down List James Ayers Word 3 07-14-2013 07:53 PM
Delete Tables using conditions from Drop Down Field using checkboxes to delete tables atfresh Word Tables 1 06-19-2011 09:13 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:22 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft