OLK2K3: Programmatically setting checkboxes in printout to templat

B

Bill Billmire

I have a custom (message based) form where I want to print the field data. I
have it working correctly for all fields except the "checkboxes". The loop
appears to iterate through all the bookmarks in the template correctly and
fills in the bookmarks with the exception of the checkboxes, instead of the
checkboxes being "checked" they are prefixed with either True or False
depending on the state of the checkbox in the Outlook form... I want the
checkboxes on the word template to be either checked (or not checked) as
represented on the Outlook form.

Code below...

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "form.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\server\folder\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
If strField = "ElectWork" then
ObjMark("ElectWork").CheckBox.Value = True
ElseIf strField = "Furniture" then
ObjMark("Furniture").CheckBox.Value = True
ElseIf strField = "GenConstruction" then
ObjMark("GenConstruction").CheckBox.Value = True
ElseIf strField = "Other" then
ObjMark("Other").CheckBox.Value = True
ElseIf strField = "PartitionRecon" then
ObjMark("PartitionRecon").CheckBox.Value = True
ElseIf strField = "PersonnelMove" then
ObjMark("PersonnelMove").CheckBox.Value = True
ElseIf strField = "Telecomm" then
ObjMark("Telecomm").CheckBox.Value = True
End If
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
'stop
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut 0
objWord.Quit(0)
' Clean up
Set objDoc = Nothing
Set objWord = Nothing
End Sub
 
S

Sue Mosher [MVP-Outlook]

Do you actually have check box fields set up with field names that match the
bookmark names? And are the checkboxes on the Outlook form bound to Yes/No
properties?


FYI, the syntax to check a check box associated with a bookmark is:

objDoc.FormFields(objMark.Name).CheckBox.Value = True
 
B

Bill Billmire

Hi Sue!

Yes I have checkboxes with field names that match the bookmark names, and
the Outlook form fields are bound to Yes/No properties.

I used the following syntax to check the boxes, but they all get checked,
and they are all still pre-pended with the True/False variable result.
objDoc.FormFields(objMark.Name).CheckBox.Value = True

As the loop iterates and finds each bookmark, it needs to determine if the
Outlook field and reciprocal Bookmark go together then apply the
".CheckBox.Value = True" to check the correct template checkbox bookmark (if
true), all the rest (if false) get no checkmark for that bookmark. Also, not
print the "true/false" variable result before the checkmark bookmark.

In your opinion; should this logic be in an (If... Else If... End If) or a
(Select Case... End Select) construct?

'-------------------Printing Routine---------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "FWR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField1 = "true" then
objDoc.FormFields(objMark.Name).CheckBox.Value = True
Else If strfield1 = "false" then
strField1 = ""
'msgbox strField1
Else
'msgbox strField
strField1 = Item.UserProperties(strField)
'msgbox strField1
End If
objMark.Range.InsertBefore strField1
stop
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut 0
objWord.Quit(0)
' Clean up
Set objDoc = Nothing
Set objWord = Nothing
End Sub

Thanks again!

Bill Billmire -

Sue Mosher said:
Do you actually have check box fields set up with field names that match the
bookmark names? And are the checkboxes on the Outlook form bound to Yes/No
properties?


FYI, the syntax to check a check box associated with a bookmark is:

objDoc.FormFields(objMark.Name).CheckBox.Value = True
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

Sue Mosher [MVP-Outlook]

I see 3 problems:

1) Regarding this statement:

If strField1 = "true" then

I see no earlier statement to set the value of strField1. Did you just omit
that in your latest post? We'll presume that it's a statement that gets the
value of an Outlook property.

2) Regarding that same statement, a Yes/No (i.e. Boolean) Outlook property
will never return a value of "true." The value will be either True or False,
in other words a logical value rather than an string (text) value.
Therefore, this statement should be:

If strField1 = True then

3) This statement is prepending the "True" or "False" text:

objMark.Range.InsertBefore strField1

You are applying it to all fields, rather than only to those fields that do
not return a value of True or False.

I'd suggest that you step away from the keyboard, get out pencil and paper
and write out a flow chart of how you want field values to be processed. Pay
attention to when you want the text inserted and when you want a check box
checked and the fact that you don't want both to happen for any one field.
Then rearrange your code statements so that the right statements take place
within the correct portions of your If ... End If blocks.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Bill Billmire said:
Hi Sue!

Yes I have checkboxes with field names that match the bookmark names, and
the Outlook form fields are bound to Yes/No properties.

I used the following syntax to check the boxes, but they all get checked,
and they are all still pre-pended with the True/False variable result.
objDoc.FormFields(objMark.Name).CheckBox.Value = True

As the loop iterates and finds each bookmark, it needs to determine if the
Outlook field and reciprocal Bookmark go together then apply the
".CheckBox.Value = True" to check the correct template checkbox bookmark
(if
true), all the rest (if false) get no checkmark for that bookmark. Also,
not
print the "true/false" variable result before the checkmark bookmark.

In your opinion; should this logic be in an (If... Else If... End If) or a
(Select Case... End Select) construct?

'-------------------Printing Routine---------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "FWR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField1 = "true" then
objDoc.FormFields(objMark.Name).CheckBox.Value = True
Else If strfield1 = "false" then
strField1 = ""
'msgbox strField1
Else
'msgbox strField
strField1 = Item.UserProperties(strField)
'msgbox strField1
End If
objMark.Range.InsertBefore strField1
stop
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut 0
objWord.Quit(0)
' Clean up
Set objDoc = Nothing
Set objWord = Nothing
End Sub

Thanks again!

Bill Billmire -
 
B

Bill Billmire

Thanks Sue,

I already had the routine fully architected, but my limited knowledge of
VBScripting and all the functions and arguments available and correct usage
make me stumble quite a bit.

The following code now works as I expected it to, (please review and comment
if you see anything wrong or can suggest better ways to accomplish the same
thing).

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "FWR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
strField1 = Item.UserProperties(strField)

If strField1 = True then
objDoc.FormFields(objMark.Name).CheckBox.Value = True
End If

If strfield1 = False then
objDoc.FormFields(objMark.Name).CheckBox.Value = False
End If

If VarType(strField1) = 8 then
objMark.Range.InsertBefore strField1
End If
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut 0
objWord.Quit(0)
' Clean up
Set objDoc = Nothing
Set objWord = Nothing
End Sub

Thanks,
 
S

Sue Mosher [MVP-Outlook]

Good job! The one thing you might consider is refining your If ... End If to
reflect the fact that the choices are mutually exclusive. In other words,
instead of three If blocks:

If strField1 = True then
objDoc.FormFields(objMark.Name).CheckBox.Value = True
End If

If strfield1 = False then
objDoc.FormFields(objMark.Name).CheckBox.Value = False
End If

If VarType(strField1) = 8 then
objMark.Range.InsertBefore strField1
End If

You can use just one:

If strField1 = True then
objDoc.FormFields(objMark.Name).CheckBox.Value = True
ElseIf strfield1 = False then
objDoc.FormFields(objMark.Name).CheckBox.Value = False
ElseIf VarType(strField1) = 8 then
objMark.Range.InsertBefore strField1
End If

You can also get the type of field, BTW, from the Outlook property, so
another alternative might be a Select Case statement:

Set myField = Item.UserProperties(strField)
Select Case myField
Case 6 ' olYesNo
objDoc.FormFields(strField).CheckBox.Value = myField.Value
' you could add more cases for other types of Outlook properties
here
Case Else ' everything but Yes/No
objMark.Range.InsertBefore myField.Value
End Select

Note that the Set statement lets us get an object variable for the custom
UserProperty, so we can use its Type proeprty, not just its Value.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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