PDA

View Full Version : Create Tables With Multiple Checkboxes



dawkin
07-09-2013, 12:57 AM
Hi!
I'm new to this Forum and to VBA as well. I am trying to code a Word template in VBA. Please excuse my low level code, as i'm not a frequent programmer (especially not in VBA).

I am trying to make a form, containing of several checkboxes. I am trying to add a table once you check one of the checkboxes, and if you check another one another table should be added. the code i am working on looks like this:

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=4, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
If ActiveDocument.Tables.Count >= 1 Then
With ActiveDocument.Tables(2).Cell(Row:=1, Column:=1).Range
.Delete
.InsertAfter Text:="Text1"
.Font.Name = "Times New Roman"
.Font.Size = "12"
.Font.Underline = wdUnderlineSingle
End With
With ActiveDocument.Tables(2).Cell(Row:=1, Column:=2).Range
.Delete
.InsertAfter Text:="Text2"
.Font.Name = "Times New Roman"
.Font.Size = "12"
.Font.Underline = wdUnderlineSingle
End With
With ActiveDocument.Tables(2).Cell(Row:=1, Column:=3).Range
.Delete
.InsertAfter Text:="Text3"
.Font.Name = "Times New Roman"
.Font.Size = "12"
.Font.Underline = wdUnderlineSingle
Tables(2).Cell(1, 3).Select
Selection.Footnotes.Add Range:=Selection.Range, _
Text:="Footnote1"
End With
With ActiveDocument.Tables(2).Cell(Row:=1, Column:=4).Range
.Delete
.InsertAfter Text:="Text4"
.Font.Name = "Times New Roman"
.Font.Size = "12"
.Font.Underline = wdUnderlineSingle
End With
End If
End Sub

Private Sub CheckBox2_Click()
Same code as above except i change the identifier numbers
End Sub

And then I repeat that code for each checkbox.
my problem comes with the logic behind this code, if I check checkbox2 before checkbox1 Table(2) will be the one added, but the code is trying to change Table(3) which doesn't exist. I have tried using a counter but haven't been able to make that work?

The structure of the code doesn't need to be anything fancy. If anyone have a better solution or could help me solve my problem I would appreciate it!

Thanks in advance.