Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2008

Tip: Looking for answers? Try searching our database.

Inserting a checkbox using VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
melon - 21 Feb 2008 17:26 GMT
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.
Jean-Guy Marcil - 21 Feb 2008 18:06 GMT
> 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.
[quoted text clipped - 5 lines]
>
> 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
melon - 21 Feb 2008 19:47 GMT
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.
Jean-Guy Marcil - 21 Feb 2008 20:19 GMT
> 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
melon - 21 Feb 2008 20:53 GMT
On Feb 21, 3:19 pm, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:
> > Thanks, I tried to switch every selection into range then it seems to
> > work.
[quoted text clipped - 41 lines]
>
> 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.
Jean-Guy Marcil - 22 Feb 2008 14:40 GMT
> On Feb 21, 3:19 pm, Jean-Guy Marcil
> <JeanGuyMar...@discussions.microsoft.com> wrote:
[quoted text clipped - 48 lines]
> (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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.