Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA for Word Table

Status
Not open for further replies.

ITChick526

Technical User
Sep 26, 2008
15
I have a word document with 9 tables
1 row each with checkboxes

When a checkbox value is false, I have written some to code to delete the corresponding row. The problem is once a row is deleted, it throws of the table number of the remaining tables. I can't find a way to work around this?? Any suggestions. I am a novice at vba.
 
I did see that..that will require more research on my part because I am a total novice with vba

thanks again
 



pretty sinple to loop thru the table collection.
Code:
dim tbl as table
for each tbl in thisdocument.tables
   
next


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Here's something I came up with to delete the tables but another problem pops up when you go to re-protect the document; all the form fields are reset. But since I wrote it, I thought I'd share, maybe it'll spark some other ideas.

As for solving your original problem, I'd say Skip's solution of copying to a new document would be the best way to go.

And Skip, since you're here, I've got to ask ... why the blank lines before every post?
Code:
Public Sub DeleteUncheckedTables()
    Dim ff As FormField
    Dim tb As Table
    Dim ProtectionType As Integer
    
    ProtectionType = ActiveDocument.ProtectionType
    If (ProtectionType <> wdNoProtection) Then ActiveDocument.Unprotect
    
    For Each tb In ActiveDocument.Tables
        tb.Select
        
        For Each ff In Selection.FormFields
            If (ff.Type = wdFieldFormCheckBox) Then
                If (ff.CheckBox.Value = False) Then
                    tb.Delete
                End If
            End If
        Next
        
    Next
    
    ActiveDocument.Protect (ProtectionType)
    
End Sub
 
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
 
Thank you Fumei...

I will definitely try all the things you suggested...I may contact you again for more questions. I hope that is ok!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top