creating envelope macro

B

bryan

I have a protected template which is populated with information from our host
system: formfields Text1-Text4 have the name, DBA, address, CityStateZip.

I want to create a macro to which these 4 are selected to print an envelope.

I don't want the user to have to unprotect, highlight these, and then tools>
Envelopes and Labels >etc, etc.

Is there a way to programmically create a macro to print the envelope?

Thanks,
Bryan
 
G

Graham Mayor

There are a couple of ways to do this. If you are simply distributing a
form, and provided you can convince users to run macros then you can unlock
the form in code add an envelope to the form and print that (#10) envelope

Dim sAddress As String
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
sAddress = .FormFields("name").Result & vbCr & _
.FormFields("DBA").Result & vbCr & _
.FormFields("address").Result & vbCr & _
.FormFields("CityStateZip").Result
.Envelope.Insert ExtractAddress:=False, OmitReturnAddress:=
_
False, PrintBarCode:=False, PrintFIMA:=False,
Height:=CentimetersToPoints _
(10.48), Width:=CentimetersToPoints(24.13),
Address:=sAddress, AddressFromLeft:=CentimetersToPoints(9.22), _
AddressFromTop:=CentimetersToPoints(3.73),
ReturnAddressFromLeft:= _
wdAutoPosition, ReturnAddressFromTop:=wdAutoPosition,
DefaultOrientation _
:=wdCenterLandscape, DefaultFaceUp:=True,
PrintEPostage:=False
.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="0"
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With

If you are distributing the document as a template, then you can
additionally distribute an envelope template (you can start with one of the
envelope templates you can download from my web site). Here I have called
that envelope template Form Envelope.dot and it should be installed in the
user templates folder along with the form template. The envelope template
has a bookmark Address1 in the address frame of the template. (If you don't
know how to modify the envelope templates, e-mail me to the link on my web
site and I will send you a copy of the template I used for the macro)

Running the following macro will create a new envelope document add the
address, print it and close it without saving. You can print as many
envelopes as you wish without affecting the form. The form is not unlocked
in order to create the new document.

This still has the problem of getting users to run macros and an additional
problem of two templates for the user to install.

Dim sAddress As String
Dim dEnvelope As Document
With ActiveDocument
sAddress = .FormFields("name").Result & vbCr & _
.FormFields("DBA").Result & vbCr & _
.FormFields("address").Result & vbCr & _
.FormFields("CityStateZip").Result
Set dEnvelope =
Documents.Add(Options.DefaultFilePath(wdUserTemplatesPath) _
& "\Form Envelope.dot")
With dEnvelope
.Activate
With Selection
.GoTo What:=wdGoToBookmark, name:="Address1"
.TypeText sAddress
End With
.PrintOut
.Close wdDoNotSaveChanges
End With
End With

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Hi Graham,
I am using the 1st macro and have that as a toolbar macro.
Works slick!
2 things:
1) I had to take out this piece of code, not sure what it's for?:
PrintEpostage:=False
** Compile Error - Named Argument not found **

2)How can I close the envelope as I do not want it to be part of the document?

Thanks again,
Bryan
 
G

Graham Mayor

The epostage option may be language specific. There are probably other
entries there that you don't need also, but if it works leave them in as the
size of envelope is defined. The example below should work with the last
used envelope size. Which you use will be a compromise. You may also wish to
warn the users to insert an envelope into the printer as below.

Removal of the envelope is probably a good plan, so add the line

.Sections(1).Range.Delete
immediately before
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""

eg

Dim sAddress As String
MsgBox "Insert envelope in printer", vbInformation, "Print Envelope"
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
sAddress = .FormFields("name").Result & vbCr & _
.FormFields("DBA").Result & vbCr & _
.FormFields("address").Result & vbCr & _
.FormFields("CityStateZip").Result
.Envelope.Insert Address:=sAddress
.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="0"
.Sections(1).Range.Delete
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Hi Graham,
I have this working using your Envelope #10.dot from your web site.
I had to remove the 2 macros from it and insert other bookmarks and load
those from the formfield. When I used 1 bookmark as your code suggest the
information does not line up on the envelope.
I got:
Name
DBA
address
CityStateZip

I do get a message after running the PrintEnvelope macro:
The margins of section 1 are set outside the printable area of the page.
Continue?
Is there a way to bypass this message or autoselect "Yes" ?

Thanks for all your help,
Bryan
 
B

bryan

Quick question.
How can I have the envelope print to a specific printer without changing the
default printer?

Thanks,
Bryan
 
G

Graham Mayor

Just add the extra lines of code (before and after printout) to switch the
printer. See http://www.gmayor.com/fax_from_word.htm for specific examples.
Are you going to know what printer your users have available?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You shouldn't get the error message if you have altered the template
correctly and there is only one bookmark that needs to be inserted into that
envelope template and that is Bookmark1 which should be the only thing in
the address frame. The alternative method is probably simpler.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Hi Graham,
I did not notice this before from our network printer but,
I have printer 34 as my default.
I have a macro to print the envelope to printer 28 which works but, it first
print the form to printer 28 which I do not want. The form will go to printer
34.
Here is my print envelope macro which should only print the envelope to
printer 28:
Sub PrtEnv()
'
' PrtEnv Macro
' Macro created 10/15/2008 by bjsorens
'
Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter
ActivePrinter = "\\XS01\PRT28 on NE05:"
Application.PrintOut FileName:=""



Dim sAddress As String
MsgBox "Insert envelope in printer", vbInformation, "Print Envelope"
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
sAddress = .FormFields("InsuredName").Result & vbCr & _
.FormFields("Name2").Result & vbCr & _
.FormFields("Name3").Result & vbCr & _
.FormFields("Name4").Result
.Envelope.Insert ExtractAddress:=False, OmitReturnAddress:= _
False, PrintBarCode:=False, PrintFIMA:=False,
Height:=CentimetersToPoints _
(10.48), Width:=CentimetersToPoints(24.13),
Address:=sAddress, AddressFromLeft:=CentimetersToPoints(9.22), _
AddressFromTop:=CentimetersToPoints(3.73),
ReturnAddressFromLeft:= _
wdAutoPosition, ReturnAddressFromTop:=wdAutoPosition,
DefaultOrientation _
:=wdCenterLandscape, DefaultFaceUp:=True

.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="0"
.Sections(1).Range.Delete
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With
ActivePrinter = sCurrentPrinter
End Sub
 
G

Graham Mayor

The form is being printed by the line

Application.PrintOut FileName:=""

after you have declared the active printer.

The envelope is printed by the line

.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="0"


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Good morning!
I have this envelope funtionality in production and it works great except
the user indicates that about every 3rd or 4th envelope prints nothing.
This is done from a template which populates from our host system.
Once the user prints the form and envelope, there is another toolbar macro
which saves to our imaging system and then closes Word.
Here is code used to print envelope and form:
Sub PrtEnv()
'
' PrtEnv Macro
' Macro created 10/15/2008 by bjsorens
'
Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter
ActivePrinter = "HP LaserJet 2100 Series PCL 6 on LPT1:"
Application.PrintOut FileName:=""



Dim sAddress As String
MsgBox "Insert envelope in printer", vbInformation, "Print Envelope"
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
sAddress = .FormFields("Contact").Result & vbCr & _
.FormFields("Name1").Result & vbCr & _
.FormFields("Name2").Result & vbCr & _
.FormFields("Name3").Result
.Envelope.Insert ExtractAddress:=False, OmitReturnAddress:= _
False, PrintBarCode:=False, PrintFIMA:=False,
Height:=CentimetersToPoints _
(10.48), Width:=CentimetersToPoints(24.13),
Address:=sAddress, AddressFromLeft:=CentimetersToPoints(9.22), _
AddressFromTop:=CentimetersToPoints(3.73),
ReturnAddressFromLeft:= _
wdAutoPosition, ReturnAddressFromTop:=wdAutoPosition,
DefaultOrientation _
:=wdCenterLandscape, DefaultFaceUp:=True

.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="0"
.Sections(1).Range.Delete
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With

ActivePrinter = sCurrentPrinter

End Sub

Thanks for your insight,
Bryan
 
G

Graham Mayor

I always suppress a shudder when HP printer drivers are involved. They make
great hardware but their drivers leave a lot to be desired. However printing
three envelopes correctly out of four suggests something else may be amiss -
maybe pilot error ;)

I have tested it here and it works for me (albeit with a different printer).
How are you calling the macro? Could it be activated before the user fills
in the required fields that make up the address?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Hi Graham,
The form once opened is pre-populating the formfields with data from our
host system.
I created a toolbar macro for the PrtEnv

My first inclination was user error as well but,
I was with the user once when she was showing me this and the envelope doc
opened briefly and was filled with data but, when envelope inserted in
printer - nothing printed.

Your thinking it might be he printer drivers?
If that could be the issue, what does one do?

Thanks,
Bryan
 
G

Graham Mayor

A possibilty is that the print spooler is not responsing quickly enough to
the command before it is dismissed. Try adding a line to turn off the Word
background printing option at the start of the macro and turn it back on
again when all is finished.
Options.PrintBackground = False
Ensure there is no 's' on Background as that is something else entirely. :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

I will do this on Monday.
When I get feedback from the user I will let you know.

Thanks again for your assistance,
Bryan
 
B

bryan

Graham,
This did the trick.
Just got e-mail from user and works perfectly, no missing info.

I appreciate all your insight and help!

Bryan
 
B

bryan

Hi Graham,
User has been using this for awhile and while it works most of the time, she
ocassionally (40% of the time) is getting this message:
Margins of section 1 are set outsude of printable area. Continue?
When she selects 'Yes', nothing prints on the envelope.
When I test to a different printer, an HP also, it works fine.
Ideas ?
I am using option 1 of running a macro.
Here is the code of macro:
Dim sCurrentPrinter As String
sCurrentPrinter = ActivePrinter

ActivePrinter = "HP LaserJet 2100 Series PCL 6 on LPT1:"
Application.PrintOut FileName:=""

Options.PrintBackground = False

Dim sAddress As String
MsgBox "Insert envelope in printer", vbInformation, "Print Envelope"
With ActiveDocument
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
sAddress = .FormFields("Contact").Result & vbCr & _
.FormFields("Name1").Result & vbCr & _
.FormFields("Name2").Result & vbCr & _
.FormFields("Name3").Result
.Envelope.Insert ExtractAddress:=False, OmitReturnAddress:= _
False, PrintBarCode:=False, PrintFIMA:=False,
Height:=CentimetersToPoints _
(10.48), Width:=CentimetersToPoints(24.13),
Address:=sAddress, AddressFromLeft:=CentimetersToPoints(9.22), _
AddressFromTop:=CentimetersToPoints(3.73),
ReturnAddressFromLeft:= _
wdAutoPosition, ReturnAddressFromTop:=wdAutoPosition,
DefaultOrientation _
:=wdCenterLandscape, DefaultFaceUp:=True

.PrintOut Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="0"
.Sections(1).Range.Delete
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With
Options.PrintBackground = True
ActivePrinter = sCurrentPrinter

End Sub

Any idea?

Thanks,
Bryan
 
G

Graham Mayor

As I implied long ago in this thread, HP makes great hardware but lousy
drivers and the problems get even worse if you use Word 2007.
As HP laser printers all respond to similar commands and you have another
printer driver that works, may I suggest you setup another copy of the
printer driver that works and direct its output to the HP LaserJet 2100
printer on LPT1 and substitute that driver in the macro.
FWIW it works as intended here (though my printer feeds envelopes from the
side and not from the centre and so the address is in the wrong place)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

What do you mean by subing that driver in the macro?
I do not work with hardware so what do I need to for this?
 

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