• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Outlook custom form - Print Preview in Word

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
etdowdle
Asked:
etdowdle
  • 5
  • 5
1 Solution
 
David LeeCommented:
Hello again, etdowdle.

Have you created the Word template that is a mirror image of your custom Outlook form?
0
 
etdowdleAuthor Commented:
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
 
David LeeCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
etdowdleAuthor Commented:
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
 
David LeeCommented:
I think this Microsoft page describes everything you need to do.
0
 
etdowdleAuthor Commented:
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
 
David LeeCommented:
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
 
etdowdleAuthor Commented:
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
 
David LeeCommented:
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
 
etdowdleAuthor Commented:
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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now