OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Selected address to envelope & print envelope

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Wed Jun 22, 2005 11:29 am    Post subject: Selected address to envelope & print envelope Reply with quote

This macro will copy & paste the addressee’s address that you have selected in a letter to an envelope and print the envelope. My limited testing indicates that it will work with a simple typed address, an address made up of data source fields or one made up of user fields.

It assumes you have:
1) Created an envelope template file that works correctly with your printer.
2) That this template contains an empty text frame intended to hold the recipient's address and that the name of the frame is “Addressee” (this is the default name of the appropriate frame in an envelope created using Insert > Envelope when you choose the New Doc option), and
3) That you will edit the macro to provide the correct path to your envelope template.

EDITED 6-23-05 to allow user to enter the address directly in the envelope if no address is selected, i.e., the envelope is just opened and the macro quits.
Code:
Sub SelectedAddressToEnvelope
On Error goto ErrorHandler
oDoc = thisComponent
oVC = oDoc.CurrentController.getViewCursor
If oVC.String = "" then
 NoSelection = true
 msg = "Nothing has been selected. Do you want to type the address "
 msg = msg & "directly in the envelope or quit?"
 iAns = MsgBox (msg,1,"NO SELECTED ADDRESS.")
 If iAns = 2 then End
EndIf
envURL = ConvertToURL("c:\docs\Envelope.stw") 'Path to envelope template.
If NOT FileExists(envURL) then
 MsgBox "Unable to locate your envelope template at the URL '" & envURL & "'. Quitting!"
 End
EndIf
oFrame = oDoc.CurrentController.Frame
oENV = StarDeskTop.loadComponentFromUrl(envURL,"_blank",0,Array())
oEnvVC = oEnv.CurrentController.getViewCursor
oTFrames = oEnv.getTextFrames()
oAddrFrame = oTFrames.getByName("Addressee")
oEnvTC = oAddrFrame.createTextCursor
oEnvVC.gotoRange(oEnvTC,false)
If NoSelection then END 'Let user complete and print envelope.
oEnvFrame = oEnv.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(oFrame, ".uno:Copy", "", 0, Array())
dispatcher.executeDispatch(oEnvFrame, ".uno:Paste", "", 0, Array())
iAns = MsgBox ("Ready to print?",1,"PRINT ENVELOPE")
If iAns = 2 then End
oPrinter = oEnv.getPrinter()
oEnv.Print(oPrinter) : END 'Normal program end.
ErrorHandler:
If err# = 1 then
 msg = "Unable to find text frame named 'Addressee'. Quitting!"
 ElseIf err# = 423 then msg = "Some document must be open. Quitting!"
 Else msg = "An unknown error has occurred.  Error #" & err# & " Quitting!"
EndIf
MsgBox (msg,,"A MACRO ERROR OCCURED!")
End Sub
Back to top
View user's profile Send private message
Oropi
Newbie
Newbie


Joined: 14 Aug 2005
Posts: 3

PostPosted: Sun Aug 14, 2005 8:16 am    Post subject: Nicely done Reply with quote

Great macro that comes in very handy.
Just a question though, is there a way to change the macro to PASTE the address into the ADDRESSEE field in PASTE UNFORMATTED form? For some reason I loose formatting of the template as the PASTE function of the source overrides the template.

Cheers,
Oropi
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Mon Aug 15, 2005 9:59 am    Post subject: Reply with quote

Try substituting these lines:
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "SelectedFormat"
args2(0).Value = 1
dispatcher.executeDispatch(oEnvFrame, ".uno:ClipboardFormatItems", "", 0, args2())

for this line:
dispatcher.executeDispatch(oEnvFrame, ".uno:Paste", "", 0, Array())
Back to top
View user's profile Send private message
Oropi
Newbie
Newbie


Joined: 14 Aug 2005
Posts: 3

PostPosted: Mon Aug 15, 2005 10:21 am    Post subject: Reply with quote

GREAT stuff.
Works like a charm.

Many thanks,
Oropi
Back to top
View user's profile Send private message
gokee2
Newbie
Newbie


Joined: 30 Nov 2007
Posts: 4

PostPosted: Fri May 15, 2009 4:57 pm    Post subject: Re: Nicely done Reply with quote

Oropi wrote:
Great macro that comes in very handy.
Just a question though, is there a way to change the macro to PASTE the address into the ADDRESSEE field in PASTE UNFORMATTED form?

I would also like to paste unformatted.


Another thing, can it be made to work in calc as well as writer?

Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group