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