Somehow I missed this one.
Exactly..the user will check the orders that apply to the case, and then the selections will be transferred to another word document for other usage
OK, I would agree with Skip that perhaps a re-think, re-design, is needed.
First of all though, is this a template, i.e. a .DOT file?
If it is, here is what I suggest.
Have all the pieces, or chunks, in the .DOT file. Make each chunk have its own bookmark. When a new document is cloned (the reason and purpose of using templates!), then have a userform display. The user selects what chunks to keep. Clicking OK removes all the other chunks.
Voila. I have done this many times in order to create individual
documents from ONE template.
In regards to the original question, and the following:
This code causes table 2 to become table 1 once the first table is deleted..I know my code sucks but I am really new to this and my document will eventually have about 30 tables one row each with a checkbox.
You are correct. Removing a table does indeed change the table index number of those that are left. Deleting Table(1) does make Table(2) now Table(1).
This is the problem with index numbers. If you have 30 tables, and ANY table could be deleted (or not), then there is simply no way to get around the confusion/mess of using index numbers.
However...there IS a way around the problem. Do not use index numbers, that is, Table(
x).
How? By bookmarking each table - which means having a bookmark name. Here is an example.
(Table1) - bookmarked, and the
bookmark named Table1
(Table2) - bookmarked, and the
bookmark named Table2
(Table3) - bookmarked, and the
bookmark named Table3
(Table4) - bookmarked, and the
bookmark named Table4
etc.
Now it becomes very easy to make a table
object and do what you want with it. The beauty of this is that you can expand any table (add or delete rows), or
move the table anywhere in the document. yes, the
Table index number will change...but the
Bookmark name will not.
To get a table object - say for Table3 (contained in the bookmark "Table3") - you use:
Code:
Dim oTable3 As Table
Set oTable3 = ActiveDocument.Bookmarks("Table3") _
.Range.Tables(1)
And there you go. The table object oTable3 IS the table contained in the bookmark Table3.
You can use the table object to action ANY property or method of tables.
So say, Table4 (already properly bookmarked) has a checkbox, and if I understand correctly, you want to delete the table IF the checkbox inside is NOT checked.
OK, can do. I am not sure
when you are going to execute the code to perform the action - and that timing could be critical - but to actually do it is fairly straightforward. The following goes through all the bookmarks - assumption: the only bookmarks are for the tables. If you have other bookmarks, then things have to be adjusted.
IF the checkbox (InlineShape(1) in the bookmark range) is FALSE, or unchecked, then poof!...the table is deleted.
Code:
Sub DeleteAsRequired()
Dim oBM As Bookmark
Dim oCheckbox As InlineShape
For Each oBM In ActiveDocument.Bookmarks
Set oCheckbox = oBM.Range.InlineShapes(1)
If oCheckbox.OLEFormat.Object.Value = False Then
oBM.Range.Tables(1).Delete
End If
Set oCheckbox = Nothing
Next
End Sub
So say you have some tables (column 1 has the checkbox, column 2 has text, or whatever):
Checked yadda yadda
Checked blah blah
Unchecked whatever
Checked more stuff
Unchecked who cares
Unchecked absolutely no one
Running the code above will end up with:
Checked yadda yadda
Checked blah blah
Checked more stuff
I have to state that this is only one way to go about what you seem to want to do. There are others. The intention - using ONE source to make different documents - is very common. The actual solution depends on a full understanding of requirements.
Oh, BTW, I have been assuming, since you used "CheckBox1", that these are
ActiveX controls, NOT formfields.
faq219-2884
Gerry
My paintings and sculpture