Problem getting Outlook custom form to print to Word

D

Duane Nutley

I hope someone can help with the problem I am having as it is frustrating me
quite a bit.

I have been asked to create some Outlook form templates for a company and
that is easy. The hard bit is that they want it to print the way it looks in
the form. After browsing this forum I found the Office Article 290775 (link -
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q290775&ID=kb;en-us;Q290775 ) which was quite handy and worked for one form.

I copied/pasted the code from the one form template that worked into a new
one, changed the fields, etc and when I go to print the form I get the
following error message - Object variable not set 'strVacancyRef', which is
the first field on the form.

My VBA skills are limited as I have not worked a lot in VBA, I can fumble my
way through it, but do not understand what I have done wrong as I cant see
any difference between the code that is written, and the example provided in
the article.

I have copied the code below if that helps.

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 ("C:\Outlook Templates\Permanent
Placement Details Form.oft")

'Set the first bookmark
strVacancyRef = Item.UserProperties.Find("RefNo")
oDoc.FormFields ("Text1").Result = strVacancyRef

'Set the second bookmark
strClient = Item.UserProperties.Find("Client")
oDoc.FormFields ("Text2").Result = strClient

'Set the third bookmark
strAddress1 = Item.UserProperties.Find("Address1")
oDoc.FormFields ("Text3").Result = strAddress1

'Set the fourth bookmark
strAddress2 = Item.UserProperties.Find("Address2")
oDoc.FormFields ("Text4").Result = strAddress2

'Set the fifth bookmark
strAddress3 = Item.UserProperties.Find("Address3")
oDoc.FormFields ("Text5").Result = strAddress3

'Set the sixth bookmark
strAddress4 = Item.UserProperties.Find("Address4")
oDoc.FormFields ("Text6").Result = strAddress4

'Set the seventh bookmark
strAddress5 = Item.UserProperties.Find("Address5")
oDoc.FormFields ("Text7").Result = strAddress5

'Set the eighth bookmark
strContact = Item.UserProperties.Find("Cont")
oDoc.FormFields ("Text8").Result = strContact

'Set the ninth bookmark
strClientContactNo = Item.UserProperties.Find("ContNo")
oDoc.FormFields ("Text9").Result = strClientContactNo

'Set the tenth bookmark
strCandidateName = Item.UserProperties.Find("Candidate")
oDoc.FormFields ("Text10").Result = strCandidateName

'Set the eleventh bookmark
strPositionTitle = Item.UserProperties.Find("Position")
oDoc.FormFields ("Text11").Result = strPositionTitle

'Set the twelth bookmark
strDatePlaced = Item.UserProperties.Find("DatePl")
oDoc.FormFields ("Text12").Result = strDatePlaced

'Set the thirteenth bookmark
strCommence = Item.UserProperties.Find("CommDate")
oDoc.FormFields ("Text13").Result = strCommence

'Set the fourteenth bookmark
strBaseSalary = Item.UserProperties.Find("Salary")
oDoc.FormFields ("Text14").Result = strBaseSalary

'Set the fifteenth bookmark
strMotor = Item.UserProperties.Find("MotorVehicle")
oDoc.FormFields ("Text15").Result = strMotor

'Set the sixteenth bookmark
strOther = Item.UserProperties.Find("Other")
oDoc.FormFields ("Text16").Result = strOther

'Set the seventeenth bookmark
strTotal = Item.UserProperties.Find("Total")
oDoc.FormFields ("Text17").Result = strTotal

'Set the eighteenth bookmark
strTeamSplit = Item.UserProperties.Find("TeamSplit")
oDoc.FormFields ("Text18").Result = strTeamSplit

'Set the ninteenth bookmark
strVacancyNo = Item.UserProperties.Find("VacNo")
oDoc.FormFields ("Text19").Result = strVacancyNo

'Set the twentith bookmark
strNarration = Item.UserProperties.Find("Narration")
oDoc.FormFields ("Text20").Result = strNarration

'Set the twentyfirst bookmark
strPaymentTerms = Item.UserProperties.Find("PayTerms")
oDoc.FormFields ("Text21").Result = strPaymentTerms

'Set the twentysecond bookmark
strDueDate = Item.UserProperties.Find("InvoiceDate")
oDoc.FormFields ("Text22").Result = strDueDate

'Set the twentythird bookmark
strSuperannuation = Item.UserProperties.Find("Super")
oDoc.FormFields ("Text23").Result = strSuperannuation

'Set the twentyfourth bookmark
strFee = Item.UserProperties.Find("Fee")
oDoc.FormFields ("Text24").Result = strFee

'Set the twentyfive bookmark
strFeeTotal = Item.UserProperties.Find("FeeTotal")
oDoc.FormFields ("Text25").Result = strFeeTotal

'Set the twentysix bookmark
strGST = Item.UserProperties.Find("GST")
oDoc.FormFields ("Text26").Result = strGST

'Set the twentyseven bookmark
strGSTTotal = Item.UserProperties.Find("GSTTotal")
oDoc.FormFields ("Text27").Result = strGSTTotal

'Set the twentyeight bookmark
strInvoiceValue = Item.UserProperties.Find("InvoiceValue")
oDoc.FormFields ("Text28").Result = strInvoiceValue

'Set the twentynine bookmark
strTeam = Item.UserProperties.Find("Team")
oDoc.FormFields ("Text29").Result = strTeam

'Set the thirty bookmark
strGifts = Item.UserProperties.Find("Gifts")
oDoc.FormFields ("Text30").Result = strGifts

'Set the thirtyone bookmark
strComments = Item.UserProperties.Find("_DocSiteControl1")
oDoc.FormFields ("Text31").Result = strComments

'Set the thirtysecond bookmark
strTo = Item.UserProperties.Find("To")
oDoc.FormFields ("Text32").Result = strTo

'Set the thirtythird bookmark
chkFee = Item.UserProperties.Find("FeeAckn")
oDoc.FormFields ("Check1").Result = chkFee

'Set the thirtyfourth bookmark
chkDates = Item.UserProperties.Find("FollowUp")
oDoc.FormFields ("Check2").Result = chkDates

'Set the thirtyfifth bookmark
chkVoyager = Item.UserProperties.Find("Voyager")
oDoc.FormFields ("Check3").Result = chkVoyager

'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 do not save changes to the document
Const wdDoNotSaveChanges = 0
oDoc.Close wdDoNotSaveChanges

'Close the Word instance
oWordApp.Quit

'Clean up
Set oDoc = Nothing
Set oWordApp = Nothing
End If
End Sub
 
S

Sue Mosher [MVP-Outlook]

The symptoms suggest that there is no custom field on the Outlook form named RefNo. Check the names of the fields in the form -- on the All Fields page, under User-defined Fields in This Item.

Also note that this statement could be a problem:

Set oDoc = oWordApp.Documents.Add ("C:\Outlook Templates\Permanent
Placement Details Form.oft")

An .oft file is an Outlook template, not a Word template. A Word template would be saved as an .dot file. And you shouldn't have a space after Documents.Add. Your statements that use the CreateObject and FormFields also have extraneous spaces.
 
D

Duane Nutley

Sue,
Thank you for your reply! I cant believe I told it to look at the oft, not
the dot file.

As to the User defined fields, the fields it is looking for are the ones
that are named by right clicking on each field on the form and going to
Properties and putting in a name there. But I will look at what they are
named in the User Defined Fields section of the form.
 
D

Duane Nutley

Sue, All is working fine and it was mainly that it was looking for the oft
file not the dot template that was causing the issue.

However I have now noticed that 3 checkboxes on the Outlook form are not
filling in their equivalent Word tickboxes. I had a look at some other posts
about checkboxes and I am not sure what is causing the fault.

The field is called something like Fee Acknowledged and is a Yes/No Field.
When I rightclick on the control on the form, in the Value tab, it says it is
looking for the field Fee Acknowledge, is a Yes/No field and will display the
Icon. Is there anything else I need to change to get it to work?

Also I used the suggestion of oDoc.FormField("Check1") = user.itemproperties
code (sorry I cant remember the actual code that was used, but it was
different to what I used) and an Error about Value Mismatch came up. Now I
dont understand why that would occur as it has been told to look for a Word
form field called Check1, that is a tickbox, and on the Outlook form it is a
Yes/No field.


My last problem (and a new one I have never seen before), I can create the
form, have it print out (minus the tickboxes being selected) and send it to
someone all fine. But in Outlook Inbox, in the preview pane it says it needs
to be opened to view the contents (which I understand and am ok with). BUT
when I double click to open the email, an error message occurs, stating that
a value parameter is not valid and it will not open the email.
I have never seen this problem before and wonder if you know what might be
causing it? It has only come up so far when I tested the form by using the
Run this Form command under Form menu, while in the Design mode of the
Outlook form.

Again thank you for taking the time to read this and offer suggestion.
Duane
 
S

Sue Mosher [MVP-Outlook]

Regarding the checkbox, the correct syntax would be:

oDoc.FormField("Check1").CheckBox.Value = Item.UserProperties('your yes/no field")

The appearance in the reading pane is perfectly normal for a custom form that has code behind it. The error message could be due to code in the Item_Open event hander, which doesn't run until the user opens the item. The exact text of the error message would probably be helpful.
 
D

Duane Nutley

Sue,
The check boxes work! Of course only after making it oDoc.FormFields, not
FormField. So that one is now off the list.

The error when attempting to open the form once it has been received is as
follows:

"Can't open this item. Could not complete the operation. One or more
parameter values are not valid".

I am wondering if this is because I deleted the message body field. I could
not find the VBA code to get information from an email body (I thought it was
Item.Message), so I deleted the message body field and inserted a text field.

Something slightly related to this - Why is it that some people cannot view
an Outlook form correctly? Instead it comes up as a normal email message.
The forms I have created, one gentleman at the company, when he opens the
Outlook template from his computer, all he sees is a normal email, not the
form.

Duane
 
S

Sue Mosher [MVP-Outlook]

The object browser (F2 in VBA) is your friend. A MailItem has no Message property but it does have a Body. Adding it back to your design will also give recipients access to any attachments, which otherwise will be inaccessible.

I don't know if that will fix the error your seeing, though. It's not one that I know how to reproduce, although there are MSKB articles on similar errors.

Sorry about leaving the s off FormFIelds.
The forms I have created, one gentleman at the company, when he opens the
Outlook template from his computer, all he sees is a normal email, not the
form.

What exactly is he opening? A saved .oft file? An item created with a published form? Everyone who needs to use the form needs access to the published form definition, and items need to be created with the published form, not an .oft file. ALso, the form should be published with the "send form definition with item" box on the (Properties) page unchecked.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
D

Duane Nutley

It works!
It was the lack of the message body that was causing the error. And when
this is published there should be no problems at all.

Sue, again, thank you for taking the time to lead a newbie by the hand and
point what should be obvious. Enjoy your day/night!

Duane
 
S

Sue Mosher [MVP-Outlook]

Thanks for confirming that a missing message body control can be a cause of that nagging "Can't open this item. Could not complete the operation. One or more parameter values are not valid" message. I'm sure that will help out someone else in the future.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 

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