| View previous topic :: View next topic |
| Author |
Message |
zenlord Power User

Joined: 24 Nov 2006 Posts: 53
|
Posted: Thu Jan 14, 2010 12:40 pm Post subject: BASIC: export to PDF and send PDF as attachment in email |
|
|
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 |
|
 |
SergeM Super User

Joined: 09 Sep 2003 Posts: 3211 Location: Troyes France
|
|
| Back to top |
|
 |
zenlord Power User

Joined: 24 Nov 2006 Posts: 53
|
Posted: Mon Jan 18, 2010 10:52 am Post subject: |
|
|
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 |
|
 |
bugmenot OOo Enthusiast

Joined: 24 Apr 2006 Posts: 144
|
Posted: Wed May 12, 2010 10:55 pm Post subject: yeh |
|
|
| 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 |
|
 |
|
|
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
|