Inserting a checkbox using VBA

M

melon

I have a macro in Outlook that will create a word document. I want to
insert a checkbox control.

objWord.ActiveDocument.Bookmarks("Required").Select
objWord.Selection.TypeText CStr(names)

basically it select a bookmark, then type in some names. Instead of
plain text I want it to be a checkbox with the caption = names.

I tried this:

objWord.Selection.InlineShapes.AddOLEControl
ClassType:="Forms.CheckBox.1"

However the checkbox just won't show up.
 
J

Jean-Guy Marcil

melon said:
I have a macro in Outlook that will create a word document. I want to
insert a checkbox control.

objWord.ActiveDocument.Bookmarks("Required").Select
objWord.Selection.TypeText CStr(names)

Try not to use the Selection object, it is unstable, slow and unreliable,
especially when automating the creation of documents, which are often
invisible (The Selection object does not work well, or sometimres not at all,
with invisible documents). This code should be:

objWord.ActiveDocument.Bookmarks("Required").Range.Text = CStr(names)

Also, if you use the same object for more than one operation , use "With"
blocks. It speeds up the code and makes the whole project lighter, easier to
read and faster to manage and debug.

The above could have been, but we know it cannot be since you should not use
the selection object! ;-)

With objWord
.ActiveDocument.Bookmarks("Required").Select
.Selection.TypeText CStr(names)
End With

Finally, it is better to assign objects to for everything you manipulate in
your code, especially when automating documents.

You have a Word Application object (objWord), but you do not have a Document
object. You should have something like this in your code:

Dim objWord As Word.Application
Dim docNew As Word.Document

Set objWord = New Word.Application
With objWord
.Visible = True
Set docNew = .Documents.Add
With docNew
.Bookmarks("Required").Range.Text = "Some text"
'etc.
End With
End With

Of course, the way you create the Word object depends on what you are doing
overall, and you may not need to make the application visible...
basically it select a bookmark, then type in some names. Instead of
plain text I want it to be a checkbox with the caption = names.

I tried this:

objWord.Selection.InlineShapes.AddOLEControl
ClassType:="Forms.CheckBox.1"

However the checkbox just won't show up.

Probably becasue you are using the selction object.

Here is an example that uses the range object that you can easily adapt to
your situation:

Dim inShpCheck As InlineShape
Dim rgeShape As Range

'For this example, I am inserting the checkbox at the beginning _
of the second paragraph in the document
Set rgeShape = ActiveDocument.Range.Paragraphs(2).Range

With rgeShape
.Collapse wdCollapseStart
Set inShpCheck = .InlineShapes.AddOLEControl("Forms.CheckBox.1", rgeShape)
End With

With inShpCheck.OLEFormat.Object
.Caption = "Names"
End With
 
M

melon

Thanks, I tried to switch every selection into range then it seems to
work.

But after inserting one checkbox, how do I insert another with a new
line? I try to use Range.InsertAfter vbNewLine or
Range.InsertParagraph in between each checkbox but that doesn't work.
 
J

Jean-Guy Marcil

melon said:
Thanks, I tried to switch every selection into range then it seems to
work.

But after inserting one checkbox, how do I insert another with a new
line? I try to use Range.InsertAfter vbNewLine or
Range.InsertParagraph in between each checkbox but that doesn't work.

Hre is an example of what you can do. I have moved the Checkbox stuff in a
function so that it can be called as oftewn as you need, in this case, 3
times.

Sub ManageCheck()

Dim i As Long
Dim rgeTarget As Range

Set rgeTarget = ActiveDocument.Range.Paragraphs(2).Range
rgeTarget.InsertParagraphAfter

For i = 1 To 3
rgeInsertCheck rgeTarget, i
Next



End Sub

Function rgeInsertCheck(ByRef rgeInsert As Range, ByVal lngCount As Long) As
Range

Dim inShpCheck As InlineShape

With rgeInsert
.Collapse wdCollapseEnd
Set inShpCheck = .InlineShapes.AddOLEControl("Forms.CheckBox.1",
rgeInsert)
.MoveEnd wdCharacter, 1
.Collapse wdCollapseEnd
.InsertParagraphAfter
.InsertParagraphAfter
End With

With inShpCheck.OLEFormat.Object
.Caption = "Names" & lngCount
End With


End Function
 
M

melon

Hre is an example of what you can do. I have moved the Checkbox stuff in a
function so that it can be called as oftewn as you need, in this case, 3
times.

Sub ManageCheck()

Dim i As Long
Dim rgeTarget As Range

Set rgeTarget = ActiveDocument.Range.Paragraphs(2).Range
rgeTarget.InsertParagraphAfter

For i = 1 To 3
rgeInsertCheck rgeTarget, i
Next

End Sub

Function rgeInsertCheck(ByRef rgeInsert As Range, ByVal lngCount As Long) As
Range

Dim inShpCheck As InlineShape

With rgeInsert
.Collapse wdCollapseEnd
Set inShpCheck = .InlineShapes.AddOLEControl("Forms.CheckBox.1",
rgeInsert)
.MoveEnd wdCharacter, 1
.Collapse wdCollapseEnd
.InsertParagraphAfter
.InsertParagraphAfter
End With

With inShpCheck.OLEFormat.Object
.Caption = "Names" & lngCount
End With

End Function

I don't know what's going on but the new checkbox control will still
get inserted in the same line. The InsertParagraph will execute first
(even if it is placed after InlineShapes.AddOLEContro), then the
control box is drawn in a horizontal arrangement.
 
J

Jean-Guy Marcil

melon said:
I don't know what's going on but the new checkbox control will still
get inserted in the same line. The InsertParagraph will execute first
(even if it is placed after InlineShapes.AddOLEContro), then the
control box is drawn in a horizontal arrangement.

I have tested the code I posted, and here, on Word 2003 SP3, it works as
advertised (3 checkboxes are inserted below the second paragraph in the
document, each one preceded and followed by a paragraph mark - ¶).
If you get different results, you either modified the code, in which case we
cannot help without seeing your modified code, or you are applying my code to
a document that has a peculiar lay out I was unware of when I created the
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top