Solved

Outlook custom form - Print Preview in Word

Posted on 2014-02-05
12
325 Views
Last Modified: 2014-11-11
I've added both a print and print preview button to my custom outlook form using code found on a similar post in this forum. The idea is that my form will open up and print from word thereby maintaining the format of the form.

The code appears to work as it activates the word doc. I am just missing the initial set-up steps. What do I need to add to the word document to pull in the desired fields?

Sub cmdPrint_Click()
      printOrPreview("print")
End sub

sub cmdPrintPreview_Click()
      printOrPreview("preview")
end sub

sub printOrPreview(action)
    ' Print or preview the Word document
     Dim oWordApp ' As Word.Application
     Dim strMyField ' As String
     Dim oDoc ' As Word.Document
     Dim bolPrintBackground

  Set oWordApp = CreateObject("Word.Application")
  If oWordApp Is Nothing Then
     MsgBox "Couldn't start Word."
  Else

    oWordApp.Visible = True
     ' Open a new document
     Set oDoc = oWordApp.Documents.Add("C:\Users\BTD2920\Desktop\StaffLeave.dot", True, , True)
                                    'Template, NewTemplate, DocumentType, Visible
    'Map the data in the Outlook form the the field in the word template
    ' Set the first bookmark to the contact's full name

     oDoc.FormFields("EmployeeName").Result = CStr(Item.EmployeeName)

     strMyField = Item.UserProperties.Find("Department")
     oDoc.FormFields("Department").Result = strMyField

     strMyField = Item.UserProperties.Find("Employee ID")
     oDoc.FormFields("StaffNumber").Result = strMyField

     oDoc.FormFields("Title").Result = CStr(Item.JobTitle)

     strMyField = Item.UserProperties.Find("Effective Date")
     oDoc.FormFields("StartDate1").Result = strMyField

    strMyField = Item.UserProperties.Find("Supervisor Name")
    oDoc.FormFields("EndDate1").Result = strMyField

     strMyField = Item.UserProperties.Find("Supervisor ID")
     oDoc.FormFields("StartDate2").Result = strMyField

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

     ' Turn background printing off
     oWordApp.Options.PrintBackground = False
    if action = "print" then
        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 oDoc = Nothing
         Set oWordApp = Nothing
    else
         oDoc.PrintPreview
end if
end if
end sub   

Open in new window

0
Comment
Question by:etdowdle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
12 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 39838476
Hello again, etdowdle.

Have you created the Word template that is a mirror image of your custom Outlook form?
0
 

Author Comment

by:etdowdle
ID: 39838735
Hi Savant,

I had not. I read something about using bookmarks and/or DocVariables on the Word doc to capture the fields. I understand how to navigate and add them in Word but do not know how to associate them to my custom form.

Does the layout of the Word doc need to be identical to the custom form?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 39839059
Does the layout of the Word doc need to be identical to the custom form?

That depends on whether you want it to or not.  When I see "print preview", I think of output the looks exactly like the item I'm print previewing.  If that's a custom Outlook form, then I'd think you'd want the output to look like the form.  It's up to you though.

I understand how to navigate and add them in Word but do not know how to associate them to my custom form.

You can't "associate" them with your form in the sense the word implies.  What you'll do is populate them with values from your form.  Each value in your form is stored as a userproperty.  You need to know the name of each of those userpropery items.  You can then populate the fields in the Word document like this (using a line from code you posted above)

oDoc.FormFields("EmployeeName").Result = olkMsg.UserProperties.Item("EmployeeName").value

Open in new window

Of course you'll need to adjust the names of the form field in Word and the name of the userproperty from the Outlook item you're processing against.  olkMsg can be any object that represents a message which is using your custom form.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:etdowdle
ID: 39840534
Hi Savant,
I'm still trying to figure this one out.

I think I'm all set on the outlook side once I add in all my fields in vbscript.

What is a "field" in Word? I've tried adding a DocVariable but I do not see a field when I click OK (see image).

Does the code need to be added in Word? Or just in Outlook?

Thanks againField window in Word
0
 
LVL 76

Expert Comment

by:David Lee
ID: 39840655
I think this Microsoft page describes everything you need to do.
0
 

Author Comment

by:etdowdle
ID: 39842632
I read though the link you posted and then spent a lot of time researching online - my fields will not appear in Word. Maybe the code is wrong?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 39842727
If you'll upload a copy of the Word document you're using along with the code as you have it, then I'll try it out.
0
 

Author Comment

by:etdowdle
ID: 39842751
Thanks - I've only added a few fields to the word doc to test it out.




Sub cmdPrint_Click()
      printOrPreview("print")
End sub

sub cmdPrintPreview_Click()
      printOrPreview("preview")
end sub

sub printOrPreview(action)
    ' Print or preview the Word document
     Dim oWordApp ' As Word.Application
     Dim strMyField ' As String
     Dim oDoc ' As Word.Document
     Dim bolPrintBackground

  Set oWordApp = CreateObject("Word.Application")
  If oWordApp Is Nothing Then
     MsgBox "Couldn't start Word."
  Else

    oWordApp.Visible = True
     ' Open a new document
     Set oDoc = oWordApp.Documents.Add("C:\Desktop\Action.docx", True, , True)
                                    'Template, NewTemplate, DocumentType, Visible
    'Map the data in the Outlook form the the field in the word template
    ' Set the first bookmark to the contact's full name

    
oDoc.Bookmarks("EmplName1").Result = olkMsg.UserProperties.Item("EmplName1").value
oDoc.FormFields("EmplID").Result = olkMsg.UserProperties.Item("EmplID1").value
oDoc.FormFields("Action1").Result = olkMsg.UserProperties.Item("Action1").value

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

     ' Turn background printing off
     oWordApp.Options.PrintBackground = False
    if action = "print" then
        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 oDoc = Nothing
         Set oWordApp = Nothing
    else
         oDoc.PrintPreview
end if
end if
end sub

Open in new window

Action.docx
0
 
LVL 76

Expert Comment

by:David Lee
ID: 39842779
Without even testing the code I see a problem.  olkMsg isn't set to anything.  It's nonexistent.  As I said in my post with the code snippet, "olkMsg can be any object that represents a message which is using your custom form."  In other words, you have to set olkMsg to point to some message.  If you wanted the message that's currently open, then something like

Set olkMsg = Application.ActiveInspector.CurrentItem

Open in new window


That line should occur before any of the lines where you're trying to get the value of a property from the message.
0
 

Accepted Solution

by:
etdowdle earned 0 total points
ID: 39848630
Hi BlueDevilFan,

Wanted to let you know I think I've got this one. For others researching this...

This Microsoft support site really spells out the steps: Link

I added a line to Method 3 to enable me to print preview
oWordApp.Visible = True

Open in new window


What I was doing wrong:
From word - I was saving the file as a doc rather than a dot (template).
I was also struggling with the fields - but there isn't any magic there. Just add Text Form Field from the developer tab and apply Bookmark name (right click properties) to code (Ex. Text1, Text2, etc).
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Find out what you should include to make the best professional email signature for your organization.
Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question