Creating an Outlook template and printing w/Word using VB

G

Guest

We have created a customized template for work which has about 20
user-defined fields. The template will be sent to co-workers via email, and
we would like the reader to be able to print the template for future
reference. I did find articles on this website related to printing forms. I
was successful in adding a "print" button to the template which is actually
an alt-screen print command. However, the data on the screen could
potentially be longer than the print screen shot - and the reader would have
to alt-print screen the top half of the form and then the bottom. I then
found an example of how to print a custom form and have the user-defined
fields sent over to Word, and Word will print out the form. I have been
playing with this, but my 1 line to define the user-generated field, doesn't
work. If I surpress that line, Word will print a blank page out - so I know
that part is correct.

The line is: ' Set the first bookmark to the Market name
oDoc.FormFields("Text1"). Result = Item.UserProperties.Find ("A.Market")

- where A.market is the user-generated field and a form has been created in
Word with a Text 1 and Text 2 Label. When I run the debugger - I get a
run=-time error: type mismatch
Any help is appreciated -
 
G

Guest

Either that field doesn't exist, or it is a Keywords field.

This would be cleaner:

Dim objProp

Set objProp = Item.UserProperties("A.Market")
If not objProp Is Nothing Then
If objProp.Type <> olKeywords Then
oDoc.FormFields("Text1"). Result = Item.UserProperties.Find
("A.Market")
End If
End If

There are issues working with keywords fields in VBScript. See:

OL2002: Working with Keywords Fields from VBScript:
http://support.microsoft.com/default.aspx?scid=kb;en-us;291117
 
G

Guest

Thanks - I'll try that. Should I just be able to insert this language into
my script? What does Dim ojbProp stand for?
 
G

Guest

Thanks -I tried that, but get an error message before the script starts which
says "expected statement - line 20".
 
G

Guest

The "Dim objProp" declares a Variant variable in VBScript.

Please post your code in entirety so we can see what's going on.
 
G

Guest

Thanks much! Here it is with your suggestions from yesterday. I get the
error message when I choose "design a form", however, I can open the form
after the error message:

Sub cmdPrint_Click()
Set oWordApp = createObject ("Word.Application")
If oWordApp Is Nothing Then
MsgBox "Couldn't start Word."
Else
Dim oWordApp
Dim oWordDoc
Dim bolPrintBackground

' Open a new document
Set oDoc = oWordApp.Documents.Add ("G:\My Documents\MyForm.dot")

' Set the first bookmark to the Market name
Dim objProp

Set objProp = Item.UserProperties("A.Market")
If not objProp Is Nothing Then
If objProp.Type <> olKeywords Then
oDoc.FormFields("Text1"). Result = Item.UserProperties.Find
("A.Market")
End If
End If

'Get the current Word setting for background printing
bolPrintBackground = oWordApp.Options.PrintBackground

'Turn background printing off
oWordApp.Options.PrintBackground = False

'Print the Word document
oDoc.PrintOut

'Restore previous setting
oWordApp.Options.PrintBackground = bolPrintBackground

'Close and don't save changes to the document
Const wdDoNotSaveChanges = 0
oDoc.Close wdDoNotSaveChanges

'Close the Word instance
oWordApp.Quit

'Clean up
Set oPS = Nothing
Set oDoc = Nothing
Set oWordApp = Nothing
End If
End Sub
 
G

Guest

There were a few syntactical and other errors with your code that I took the
liberty of correcting. The biggest problem was that you weren't setting
oDoc.FormFields("Text1").Result to objProp.Value.

Sub cmdPrint_Click()
Dim oWordApp
Dim oWordDoc
Dim bolPrintBackground
Dim objProp

Set oWordApp = createObject ("Word.Application")
If oWordApp Is Nothing Then
MsgBox "Couldn't start Word."
Exit Sub
End If

' Open a new document
Set oDoc = oWordApp.Documents.Add ("G:\My Documents\MyForm.dot")

' Set the first bookmark to the Market name
Set objProp = Item.UserProperties("A.Market")

If not objProp Is Nothing Then
If objProp.Type <> olKeywords Then
oDoc.FormFields("Text1").Result = objProp.Value
End If
End If

'Get the current Word setting for background printing
bolPrintBackground = oWordApp.Options.PrintBackground

'Turn background printing off
oWordApp.Options.PrintBackground = False

'Print the Word document
oDoc.PrintOut

'Restore previous setting
oWordApp.Options.PrintBackground = bolPrintBackground

'Close and don't save changes to the document
Const wdDoNotSaveChanges = 0
oDoc.Close wdDoNotSaveChanges

'Close the Word instance
oWordApp.Quit

'Clean up
Set oPS = Nothing
Set oDoc = Nothing
Set oWordApp = Nothing
End Sub
 
G

Guest

Eric - thanks very, very much. It worked. One final question. The entire
form is composed of dropdown fields, with the exception of one area - which
is composed of check-off fields. Here, a manager would check off any of 8
topics to which the communication applies. Would the coding be different for
this type of field (check-off?) I assume it would. Is there a way to only
print the fields which have a check in the check box and surpress the rest?
Again, thanks for your help. Being somewhat of a novice, I was about to
scrap this part of the project, but now it looks like we will be able to
accomplish our goal. Your assistance is much appreciated !!
 
G

Guest

You access checkbox controls like any other, either via a page's controls
collection:

Set myControl = Item.GetInspector.ModifedFormPages.Controls("CheckBox1")
'Get value via myControl.Value

Or if the control is bound to a user defined field, you can do it this way:

myValue = Item.UserProperties("My Field Bound To A Control").Value

If you don't want certain controls to print, try setting their .Visible
property to False at an appropriate point in your code.
 
G

Guest

Eric - We've gotten the form where the value "true" prints out if the
checkbox is checked,and the value "false" prints out if the checkbox is
unchecked. We cannot seem to figure out how to fix it so that when the
checkbox has a check - only the "caption" appears on the printed form for the
reader. Our current properties for the checkbox is: type is "yes/no"; the
format is "true/false" and the Property to Use is "value".
Example - for the checkbox labeled "benefits" - if a manager checks this box
off, we would like the reader to see the word "benefits" - not the words true
or false.....
Any help- again appreciated...
 
C

cory dorning

I am having a similar problem...however i do not know VB. Well my
experience with it is limited. I have to do the samething with a form,
but the form has 6 tabs and has only checkboxes and test fields. Does
any one know where i can find some sample code to change and make it
work with mine.
 

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