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

BASIC: export to PDF and send PDF as attachment in email

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


Joined: 24 Nov 2006
Posts: 53

PostPosted: Thu Jan 14, 2010 12:40 pm    Post subject: BASIC: export to PDF and send PDF as attachment in email Reply with quote

Hello,

This forum has helped me a lot in the past weeks to write macro's. So here I'm giving (a little) back.

Code:
Sub ExportAsPdfAndSendEmail
' ------------------------------
' This macro converts the active document to PDF and then adds the PDF
' to a new emailmessage. The recipientaddress and subject are taken
' from a textfield inside the document
'
' This macro assumes the default email application is set
' in Tools -> Options -> OpenOffice.org -> External Programs.
'
' This macro uses SimpleCommandMail, which might not work in windows
' Try SimpleSystemMail instead
' ------------------------------
   DIM oDoc, MailClient, MailAgent, MailMessage AS Object
   DIM sDocURL, sPDFURL, sTo, sSubject AS String

   REM Get location of the doc
   oDoc = ThisComponent
   If (oDoc.hasLocation()) Then
      sDocURL = oDoc.getURL()
   End if

   REM Replace .odt with .pdf
   sPDFURL = Left$(sDocURL,Len(sDocURL)-4) + ".pdf"

   REM Save as PDF
   DIM args(0) AS NEW com.sun.star.beans.PropertyValue
   args(0).Name = "FilterName"
   args(0).Value = "writer_pdf_Export"
   oDoc.storeToURL(sPDFURL,args())

   REM Get the values of the textfields inside the document to form the subject line
   DIM enuTF, aTextField AS Object
   DIM sDosName, sDosNum, sDosUref, sTo AS String
   enuTF = oDoc.TextFields.createEnumeration
      Do While enuTF.hasMoreElements
      aTextField = enuTF.nextElement
         if aTextField.supportsService("com.sun.star.text.TextField.Input") then
            if (aTextField.getPropertyValue("Hint") = "DOS_NAAM") Then
               sDosName = aTextField.getPropertyValue("Content")
            End If
            if (aTextField.getPropertyValue("Hint") = "DOS_NUM") Then
               sDosNum = aTextField.getPropertyValue("Content")
            End If
            if (aTextField.getPropertyValue("Hint") = "UREF") Then
               sDosUref = aTextField.getPropertyValue("Content")
            End If
            if (aTextField.getPropertyValue("Hint") = "EMAIL_ADDR") Then
               sTo = aTextField.getPropertyValue("Content")
            End If
         end if
      Loop
   sSubject = sDosName + " - " + sDosUref + " - " + sDosNum

   REM Send the PDF as an attachment
   MailAgent = CreateUnoService("com.sun.star.system.SimpleCommandMail")
   MailClient = MailAgent.querySimpleMailClient()
      MailMessage=MailClient.createSimpleMailMessage()
      MailMessage.setRecipient(sTo)
      MailMessage.setSubject(sSubject)
      MailMessage.setAttachement(array(sPDFURL))
   MailClient.sendSimpleMailMessage(MailMessage, 0)

   REM Save and close the document after opening the email application
   oDoc.store()
   oDoc.close(True)
End Sub


After making (assembling from different sources, really) the macro, I have used it several times already - I wonder why I didn't think of this earlier!

Zl.
Back to top
View user's profile Send private message
SergeM
Super User
Super User


Joined: 09 Sep 2003
Posts: 3211
Location: Troyes France

PostPosted: Fri Jan 15, 2010 9:23 am    Post subject: Reply with quote

Thank you for your code.
I only add cross references :
Sending emails from macros : http://www.oooforum.org/forum/viewtopic.phtml?t=4333
DannyB program : http://www.oooforum.org/forum/viewtopic.phtml?t=2732
setAtaachement questionhttp://www.oooforum.org/forum/viewtopic.phtml?t=7387
_________________
Linux & Windows OOo3.0
UNO & C++ : WIKI
http://wiki.services.openoffice.org/wiki/Using_Cpp_with_the_OOo_SDK
In French
http://wiki.services.openoffice.org/wiki/Documentation/FR/Cpp_Guide
Back to top
View user's profile Send private message Visit poster's website
zenlord
Power User
Power User


Joined: 24 Nov 2006
Posts: 53

PostPosted: Mon Jan 18, 2010 10:52 am    Post subject: Reply with quote

I made the part where the textfields are looked through better by removing the If's in favour of a case select:

Code:
   REM Get the values of the textfields inside the document to form the subject line
   DIM enuTF, aTextField AS Object
   DIM sDosName, sDosNum, sDosUref AS String
   enuTF = oDoc.TextFields.createEnumeration
      Do While enuTF.hasMoreElements
      aTextField = enuTF.nextElement
         if aTextField.supportsService("com.sun.star.text.TextField.Input") then
            Select Case aTextField.getPropertyValue("Hint")
               Case "DOS_NAAM":
                  sDosName = aTextField.getPropertyValue("Content")
               Case "DOS_NUM":
                  sDosNum = aTextField.getPropertyValue("Content")
               Case "REF_O":
                  sDosNum = aTextField.getPropertyValue("Content")
               Case "UREF":
                  sDosUref = aTextField.getPropertyValue("Content")
               Case "EMAIL_ADDR":
                  sTo = aTextField.getPropertyValue("Content")
            End Select
         end if
      Loop
   sSubject = sDosName + " - " + sDosUref + " - " + sDosNum
Back to top
View user's profile Send private message
bugmenot
OOo Enthusiast
OOo Enthusiast


Joined: 24 Apr 2006
Posts: 144

PostPosted: Wed May 12, 2010 10:55 pm    Post subject: yeh Reply with quote

Hi, i actually using this code java to execute Basic macros in section "My macros" and work fine. But i don't know how execute macros of the document loaded, i mean, macros in section "DocumentName".
Back to top
View user's profile Send private message Visit poster's website
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