Solved

Outlook custom form - Print Preview in Word

Posted on 2014-02-05
12
299 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
  • 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
Set OWA language and time zone in Exchange for individuals, all users or per database.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now