Printing Custom Forms

  • Thread starter Thread starter Micah Chaney
  • Start date Start date
M

Micah Chaney

How do you print a Custom Contact Form? When I click Print it prints field
list and values, some (not all) custom fields and some (not all) standard
outlook fields. I'm using Outlook 2003. Thanks.
 
OK, I'm trying to get this Print thing to work. This is what I have, and I'm
getting all kinds of errors. I'm sure it'll be easy for you to figure out
what I'm doing wrong. I'm completely stumped.

Sub Print_Click
Set objWord = CreateObject("Word.Application")
strTemplate = "OrderFormPrint"
strTemplate = "C:\Documents and Settings\micah\Application
Data\Microsoft\Templates\"&strTemplate
Set objDocs = objWord.Documents
objDocs.Add strTemplate
Set mybklist = objWord.ActiveDocument.Bookmarks

For counter = 1 to mybklist.count
strField = objWord.ActiveDocument.Bookmarks(counter)
objWord.ActiveDocument.Bookmarks(strField).Select
strField1 = Item.UserProperties.find(strField).Value

if strField1=True Then
strField1="Yes"
elseif strField1=False Then
strField1="No"
end If

objWord.Selection.TypeText Cstr(strField1)
Next
objWord.PrintOut Background = True
objWord.Quit(0)
End Sub

I know this is a Word Question -- but how do I know if I even set my
bookmarks up correctly, shouldn't there be some sort of indicator? I'm on
Word 2003 also. Thanks.
 
"All kinds of errors" isn't very helpful.

These statements have problems:

1)
strTemplate = "C:\Documents and Settings\micah\Application
Data\Microsoft\Templates\"&strTemplate

You need spaces on either side of the ampersand concatenation operator.

2)
if strField1=True Then
strField1="Yes"

I think the 2nd strField1 should be strField. Give your variables more
distinctive names so you don't get confused about whether strField1 is a
bookmark or an Outlook custom property.

As for setting up bookmarks "correctly," I don't know what kind of indicator
you're looking for. Either you have a bookmark for all the fields you want
to print or you don't. You can look that up in Word's dialog that shows you
all the bookmarks.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
I'm sorry, about that last post...just a little frustrated. I have the
following code...I basically did a copy paste from your outlookcode.com
samples and modified the Command Button name and name of the template file.
I even put the template in the directory that existed.

The error I'm getting after I click Print is:

Object Required: 'Item.UserProperties.find(...)'

Usually doesn't that mean I need to set an item? But I looked and I think
the item is set. I'm really clueless as to what these methods and properties
mean. So it's hard for me to attempt to read the code and determine the
problems myself. What are strField and strField1? And I don't understand
the If/Then statement. Perhaps, some help with that, might help me out a
little bit. Otherwise, do you see a reason why I'm getting the
above-mentioned error?

Dim strTemplate
Dim objWord
Dim objDocs
Dim PageRange
Dim strField
Dim strField1

Sub Print_Click
Set objWord = CreateObject("Word.Application")

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

' Location of Word template; could be on a shared LAN
strTemplate = "c:\templates\" & strTemplate
Set objDocs = objWord.Documents
objDocs.Add strTemplate
set mybklist = objWord.ActiveDocument.Bookmarks
For counter = 1 to mybklist.count
strField = objWord.ActiveDocument.Bookmarks(counter)
objWord.ActiveDocument.Bookmarks(strField).Select
strField1 = Item.UserProperties.find(strField).value

If strField = True then
strField1 = "Yes"
ElseIf strField = False then
strField1 = "No "
End If

objWord.Selection.TypeText Cstr(strField1)
Next

objWord.PrintOut Background = True
objWord.Quit(0)

End Sub
 
Umm...I think I've figured it out. I still don't understand the Code...but
it looks like if you have any Bookmarks in your Word Document that don't
match up with the Fields in your Custom Form, you'll get the error I
described. So I took out the 3 Bookmarks I knew were wrong, and it printed.
Thanks for all your help...If you can still kinda explain how it's working,
I'd like to understand a bit more. Thanks.
 
The error I'm getting after I click Print is:
Object Required: 'Item.UserProperties.find(...)'
Usually doesn't that mean I need to set an item?

Two objects are involved in that statement -- Item and the UserProperty
object returned by UserProperties.Find. If this is code running in an
Outlook form, then Item is always valid, because it's the intrinsic object
representing the current item.

That leaves UserProperty. It's actually preferable not to use the Find
method and just use:

Item.UserProperties(strField)

If there is no custom property named with the name contained in the variable
strField, however, you will get an error when you try to use that property.
To test whether the property exists in the item, you can set a UserProperty
object and check it with an If ... Then ... End If block:

On Error Resume Next
Set objProp = Item.UserProperties(strField)
If Not objProp Is Nothing Then
strField1 = objprop.value
Else
MsgBox "No field named " & strField & _
" exists in this item."
End If
I'm really clueless as to what these methods and properties mean.

When in doubt, check the object browser: Press ALt+F11 to open the VBA
environment in Outlook, then press F2. Switch from <All Libraries> to
Outlook to browse all Outlook objects and their properties, methods, and
events. Select any object or member, then press F1 to see its Help topic.
So it's hard for me to attempt to read the code and determine the problems
myself.

If that's the case, then you need to slow down, break the code out into
pieces, and take the time to understand its logic -- what it is actually
trying to accomplish -- rather than just copying and pasting and modifying
what may not have been a very good sample in the first place.
What are strField and strField1?

These are variables in your code but it's very confusing as to which is
related to the bookmark and which to the Outlook property. I'd give them
more descriptive names.
And I don't understand the If/Then statement.

What don't you understand about it? The logic of how an If ... Then ... End
If block works? The expression in the If ... Then statement?
 
Back
Top