Sending current document as email pdf from Basic

I want to create a document then click a button to send it as a .pdf

So far, I have looked at the online object reference for com::sun::star::mail

This module has a service ‘MailMessage’ which has a method ‘create’:

create ([in] string sTo, [in] string sFrom, [in] string sSubject, [in] com::sun::star::datatransfer::XTransferable xBody)

So, I create in instance of ‘MailMessage’ and find it has no method ‘create’.

objMailer = createUnoService("com.sun.star.mail.MailMessage")
objMailMessage = objMailer.create(strTargetMailAddress, strSourceMailAddress, strSubject, strBody)

The second line fails with the message "BASIC runtime error. Property or method not found:create’. Sure enough, if I examine objMailer with MRI, there is no such method.

Obviously I am misunderstanding what the object model is trying to tell me, but I seem to be going around in circles. Any help would be appreciated.

I have made a bit of progress, but it is still not doing what I want.

I used the macro recorder to capture the sequence of sending the current document as a pdf, the tried embellishing the code. What I have so far is this:

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
dim args(3) As New com.sun.star.beans.PropertyValue
rem ----------------------------------------------------------------------

args(0).Name = "From:"
args(0).Value = "someone@gmail.com"
args(1).Name = "To:"
args(1).Value = "someone_else@gmail.com"
args(2).Name = "Subject:"
args(2).Value = "Here is your document"
args(3).Name = "Message:"
args(3).Value = "Thank you for your enquiry."

rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SendMailDocAsPDF", "", 0, args())

end sub

Two problems remain:

  1. The PDF dialog is presented: I want to silently accept its defaults.
  2. I have no idea what the correct argument names are for providing the source email, destination email, subject and message body to the mail client (Evolution). My guesses did not work.

I would really appreciate any ides on how to progress this macro. ":sunglasses:

Kind regards,
Doug

---------------- Later -------------------

I have also tried this angle:

    Sub subSendEmail
	Dim objThisMessage as Object
	
	objThisMessage = createUnoService("com.sun.star.mail.MailMessage")
	
	objThisMessage.addRecipient("someone@gmail.com")
	objThisMessage.SenderName = "A Name"
	objThisMessage.SenderAddress = "abpa@nowhere.com"
	objThisMessage.ReplyToAddress = objThisMessage.SenderAddress
	objThisMessage.Subject = "This is a test email."
	objThisMessage.Body = "Thank you for playing."
    End Sub

The problems with this are:

  • Sender name and address are read-only attributes. On the target system, Evolution manages several different email accounts and the one to be used with this code is not the default, so it is vital I can set the sender details from BASIC.

  • Even if I could create the email the way I want it, how do I send it? com::sun::star::mail offers the SendMailMessageFailedException, but no ‘send’ method. The com::sun::star::system::XSimpleMailClient Interface offers sendSimpleMailMessage(), but I don’t see anything similar in com.sun.star.mail.MailMessage

Still bashing my head against the wall …

------------------- Later still ---------------------

This also fails, with createUnoService silently creating a null object:

    Sub subSimpleMailAttempt
	Dim objClient As Object
	Dim objMessage As Object
	
	objClient = createUnoService("com.sun.star.system.SimpleSystemMail")
	objMessage = objClient.createSimpleMailMessage()
	objMessage.setRecipient("someone@somewhere.com")
    End Sub

I have found the services and interfaces I need and will ask a new question about them.

------------------- Later ----------------------

I found SimpleSystemMail does not seem to be implemented. The following code works, except the sender does not get set to the originator I specify. Still, it is major progress:

Sub subSimpleMailAttempt
Dim objClient As Object
Dim objMessage As Object

objClient = createUnoService("com.sun.star.system.SimpleCommandMail")
objMessage = objClient.createSimpleMailMessage()
objMessage.setRecipient("someone@gmail.com")
objMessage.setOriginator("an_account@gmail.com>")
objMessage.setSubject("This is your raffle ticket")
objMessage.Body = "Arnold"
objClient.sendSimpleMailMessage (objMessage, 2) 
End Sub

I have little to no experience with mail in LO and less in macros so I was a bit intrigued. Saw your question from the start but had few ideas. Tried a few things but a lot of intervention. Tried your last “Sub” (you have an > in the originator address) and all worked OK. Found a couple of thing on internet over last couple of days:

DocumentToPDF

and

EmailWithAttachment

And pieced them together with your code:

Sub subSimpleMailWithPDF
    Dim objClient As Object
    Dim objMessage As Object
    Dim mAttachment(0)	
    dim document as object
    dim dispatcher as object
    document = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    ' change the path below as per your needs'
    path ="file:///home/YOUR-DIRECTORY/Documents/document1.pdf"
    Open path For Append As #1
    Close #1
    dim args1(0) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "URL"
    args1(0).Value = path
    dispatcher.executeDispatch(document, ".uno:ExportDirectToPDF", "", 0, args1())
    objClient = createUnoService("com.sun.star.system.SimpleCommandMail")
    objMessage = objClient.createSimpleMailMessage()
    objMessage.setRecipient("SOME-MAIL@yahoo.com")
    objMessage.setOriginator("YOUR-MAIL@outlook.com")
    objMessage.setSubject("First test using simple command mail.")
    objMessage.Body = "Some sample text."
    mAttachment(0) = ConvertToURL(path)
    objMessage.setAttachement(mAttachment())
    objClient.sendSimpleMailMessage (objMessage, 0) 
End Sub

The testing I did completed successfully each time. The process did stop to have you press the Send button for the email. Not sure if you can get around this. The PDF created in the first part of the sub is overwritten each time.