?
Solved

Outlook custom form - Print Preview in Word

Posted on 2014-02-05
12
Medium Priority
?
370 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

777 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