Inserting Checkboxes into a table as an InlineShape using VBA coed

  • Thread starter Michael Petrilla
  • Start date
M

Michael Petrilla

I wish to insert two checkboxes, side by side on the same line, in the third
column of a three column table. The Caption for one checkbox should be
"YES" and the other one should be "NO". I can insert one checkbox, but when
the code runs for the second checkbox, the first box gets overwritten. How
do I place the second checkbox next to the first checkbox on the same line?
I also want a small space between the two, say three spaces. Here is my
code. Thank you.


Sub insertInlineShapesCheckbox()
Dim myCheckbox1, myCheckbox2

'shifts insertion point to first tab
Selection.TypeText Text:=vbTab

'insert first checkbox
Set myCheckbox1 = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.
CheckBox.1")
With myCheckbox1.OLEFormat.Object
.TextAlign = 3
.Alignment = 0
.Width = 43
.Caption = "YES"
.FontName = "Arial"
.FontSize = 11
End With

'Selection.TypeText Text:=" "
'Selection.TypeText Text:=vbTab

'insert second checkbox
Set myCheckbox2 = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.
CheckBox.1")
With myCheckbox2.OLEFormat.Object
.TextAlign = 3
.Alignment = 0
.Width = 43
.Caption = "NO"
.FontName = "Arial"
.FontSize = 11
End With

End Sub

url:http://www.ureader.com/gp/1022-1.aspx
 
D

Doug Robbins - Word MVP

Why don't you split the cells in two and insert one check box in each cell?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

Michael Petrilla said:
I wish to insert two checkboxes, side by side on the same line, in the third
column of a three column table. The Caption for one checkbox should be
"YES" and the other one should be "NO". I can insert one checkbox, but when
the code runs for the second checkbox, the first box gets overwritten. How
do I place the second checkbox next to the first checkbox on the same line?
I also want a small space between the two, say three spaces. Here is my
code. Thank you.

Don't use the selction object unless absolutely necessary, which it is not
in this case...


Sub insertInlineShapesCheckbox()

Dim myCheckbox
Dim rgeInsert As Range

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place the cursor in a table...", vbExclamation, "Not in table"
Else
Set rgeInsert = Selection.Rows(1).Cells(3).Range
With rgeInsert
.Collapse wdCollapseStart
.InsertAfter vbTab
.Collapse wdCollapseEnd
End With
End If

'Insert first checkbox
Set myCheckbox = rgeInsert.InlineShapes _
.AddOLEControl(ClassType:="Forms.CheckBox.1")
With myCheckbox.OLEFormat.Object
.TextAlign = 3
.Alignment = 0
.Width = 43
.Caption = "YES"
.FontName = "Arial"
.FontSize = 11
End With

Set rgeInsert = Selection.Rows(1).Cells(3).Range
With rgeInsert
.MoveEnd wdCharacter, -1
.Collapse wdCollapseEnd
.InsertAfter " "
.Collapse wdCollapseEnd
End With

'insert second checkbox
Set myCheckbox = rgeInsert.InlineShapes _
.AddOLEControl(ClassType:="Forms.CheckBox.1")
With myCheckbox.OLEFormat.Object
.TextAlign = 3
.Alignment = 0
.Width = 43
.Caption = "NO"
.FontName = "Arial"
.FontSize = 11
End With

End Sub
 
Top