Printing in Word from Outlook custom forms


D

dht

I am trying to print in Word using the below code (which I have taken from
Sue Moshers book on Outlook programming), I'm pretty new to this so I'm
pretty much guessing.

The problem is that the code doesn' print...

What do I need to look at to get this to work.
I only use 1 fill field obviously when I get this working I can get the rest
working.

I'm using Outlook 2000

Dim m_blnWeOpenedWord
Dim m_blnWordPrintBackground
Const wdDoNotSaveChanges = 0
Const olDiscard = 1

Sub CmdPrint_Click()
Dim ObjDoc
Set ObjDoc = GetWordDoc("\\Wtbnas1\Shared Data\Intranet\Commercial
Services\Word Templates/Request for Reports.dot")
Call FillFields(ObjDoc)
objDoc.Applications.Options.PrintBackground = True
objDoc.PrintOut
objDoc.Close wdDoNotSaveChanges
Call RestoreWord
Set objDoc = Nothing
End Sub

Sub FilledFields(objDoc)
Dim colFields
Set colFields = objDoc.FormFields
colFields("Date").Result = Item.Sent
Set colFields = Nothing
End Sub

Function GetWordDoc(strTemplatePath)
Dim objWord
On Error Resume Next
m_blnWeOpenedWord = False
Set objword = GetObject(, "word.application")
If objword Is Nothing Then
Set objWord = createObject("word.application")
m_blnWeOpenedWord = True
End If
m_blnWordPrintBackground = objWord.Options.PrintBackground
If StrTemplatePath = " " Then
StrTemplatePath = "Normal.dot"
End If
Set GetWordDoc = objWord.Documents.Add(StrTemplatePath)
Set objWord = Nothing
End Function

Sub RestoreWord()
Dim objWord
On Error Resume Next
Set objWord.Options.PrintBackground = m_blnWordPrintBackground
If m_blnWeOpenedWord Then
objWord.Quit
Else
objWord.Visible = True
End If
Set objWord = Nothing
End Sub

Thanks
David
 
Ad

Advertisements

S

Sue Mosher [MVP-Outlook]

Are you putting this code in an Outlook form? Do you have a command button
named cmdPrint? Is the form published? Does any code run?
 
D

dht

Ok the code is in the Outlook scripting box not as a macro.
Button is in place and named cmdPrint
The form is published in my Personal Forms Folder
The code seemed to have been read as when I first opened the form it read
the code and located a typo I made.

The only thing is can I specify the location of the template over a network
as in the example below?

Thanks
David
 
S

Sue Mosher [MVP-Outlook]

A UNC name should work, but yours has one slash going the wrong way, so that
might cause a problem.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Ad

Advertisements

Joined
May 5, 2009
Messages
3
Reaction score
0
Sue, i too am trying to print a custom form. i already have a macro created to launch the form and i've added a cmdprint command button to launch the print command but the script that i have doesn't work. i hope you can help.

Sub cmdPrint_Click()

Set oWordApp = CreateObject("Word.Application")

If oWordApp Is Nothing Then

MsgBox "Couldn't start Word."

Else

Dim oWordApp

Dim oWordDoc

Dim oBMs

Dim bolPrintBackground

' Open a new document

Set oDoc = oWordApp.Documents.Add

' Set a page setup object variable

Set oPS = oDoc.PageSetup

' Reduce the margins to .5" (36 points)

oPS.TopMargin = 36

oPS.BottomMargin = 36

oPS.LeftMargin = 36

oPS.RightMargin = 36

' Paste in the screen shot

oWordApp.Selection.Paste

' Center the screen shot

Const wdAlignParagraphCenter = 1

oDoc.Paragraphs(1).Alignment=wdAlignParagraphCenter

' 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 don't save changes to the document

Const wdDoNotSaveChanges = 0

oDoc.Close wdDoNotSaveChanges

' Close the Word instance

oWordApp.Quit

' Clean up

Set oPS = Nothing

Set oDoc = Nothing

Set oWordApp = Nothing

End If

End Sub

 
Ad

Advertisements


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