Question

Sending e-mails using a document macro in Aurena


Userlevel 7
Badge +30

Hi macro lovers!

In a recent customer case I got the idea to try and use a document macro to do it instead of the default behavior. 

This post explains how to set it up.

Firstly, you need the Aurena Agent to be installed for it to work.

Here is the source code that makes it all happen:

Public Sub CreateNewOutlookMail

   On Error Resume Next

   Dim ol, newMail, olMailItem, file

   olMailItem = 0
   Set ol = CreateObject("Outlook.Application")

   Set newMail = ol.CreateItem(olMailItem)

   newMail.Subject = "Regarding " & ScriptValues.Item("DOCUMENT_TITLE").Value

   newMail.Body = "This is the message body..."

   file = Replace(Replace(ClientScriptValues.Item("CHECK_OUT_PATH").Value, "/", "\"), "\\", "\") & "temp\" & ClientScriptValues.Item("LOCAL_FILE_1").Value

   newMail.Attachments.Add (file)
  
   newMail.Display
   
   Set ol = Nothing

   If Err.Number <> 0 Then
      MsgBox "Got an error when trying to send the e-mail using Outlook. Error number: " & CStr(Err.Number) & " " & Err.Description & Err.Source
      Err.Clear
   End If
   
End Sub

Save the above in the Script field in a new macro block in Document Macro Block:

Then, for each file type you want to use this, add a macro header like this:

Above, I have made this macro available for text files (the file type is called NOTEPAD in this environment).

Now when you execute the Send Document File By E-Mail command in, say, the Document Revision page, the macro will open a new e-mail in Outlook and fill in some default values:

Why should you use this if I can send e-mails using the Aurena Agent without a macro? Well, you might want to provide another subject, or some default recipients, or a different body. Or perhaps attach yet another file (something you want to add to all e-mails, for example.)

If you want you can also restrict the macro to certain document classes. By default, all macros are available to all document classes (since the macro is primarily connected to the file type and the file operation/process). This can be done under Document Class Management.

You can read more about what document macros are and how to set them up here:

https://docs.ifs.com/ifsclouddocs/22r1/CreateAndMaintainDocument/AboutDocumentMacro.htm?StandAlone=true

Enjoy!
 


18 replies

Userlevel 4
Badge +7

Hi, 

the idea is very good, but I think that it is necessary to go further in the sending of the documents to a customer or a supplier. for example: to send all the documents in attachments according to a rule of printing . it will be enough to define the class of documents which will be sent. 

Userlevel 7
Badge +30

Hi,

I think you are talking about something quite different. This is about how to send a specific document by e-mail, not about sending documents attached to some object, plus some report. The latter cannot be done by a macro since a macro cannot run in that context.

It’s a common request/question though.

 

Userlevel 7
Badge +30

Here is an updated macro block that works regardless if the local check out path ends with a backslash or not:

Public Sub CreateNewOutlookMail

On Error Resume Next

Dim ol, newMail, olMailItem, file, path

olMailItem = 0
Set ol = CreateObject("Outlook.Application")

Set newMail = ol.CreateItem(olMailItem)

newMail.Subject = "Regarding " & ScriptValues.Item("DOCUMENT_TITLE").Value

newMail.Body = "This is the message body. You should replace it in the macro with something nicer." & vbCrLf

' Fix up slashes and backslashes

path = Replace(Replace(ClientScriptValues.Item("CHECK_OUT_PATH").Value, "/", "\"), "\\", "\")

' Add a backslash at the end of the path if it is not there

If Not Mid (path, Len (path)) = "\" Then
path = path & "\"
End If

file = path & "temp\" & ClientScriptValues.Item("LOCAL_FILE_1").Value

newMail.Attachments.Add (file)

newMail.Display

Set ol = Nothing

If Err.Number <> 0 Then
MsgBox "Got an error when trying to send the e-mail using Outlook. Error number: " & CStr(Err.Number) & " " & Err.Description & Err.Source
Err.Clear
End If

End Sub

 

Badge +3

Hi Mathias,

I followed above steps according to my requirement. Below I attached my VB code and Macro Setup. 

VB code

 

Document Macro Setup

 

After I press the command button “Send Mail”, It try to open up new mail but below error message pop up in the screen

 

 

Do you have any idea why this is pop up?

 

/Isuru. 

Userlevel 7
Badge +30

What is FILE_NAME in your code? I used LOCAL_FILE_1

Userlevel 7
Badge +30

Hi all. Here is an updated version of the send mail macro. Tested on IFS Cloud 23R1:

Public Sub CreateNewOutlookMail

On Error Resume Next

Dim ol, newMail, olMailItem, file, path

olMailItem = 0
Set ol = CreateObject("Outlook.Application")

Set newMail = ol.CreateItem(olMailItem)

newMail.Subject = "Regarding " & ScriptValues.Item("DOCUMENT_TITLE").Value

newMail.Body = "This is the message body. You should replace it in the macro with something nicer." & vbCrLf

file = Replace(ClientScriptValues.Item("LOCAL_FILE_1").Value, "/", "\")

' MsgBox "This file will be attached: " & file ' Remove single quote at the beginning of this line to have the file name be displayed before the e-mail is created

newMail.Attachments.Add (file)

newMail.Display

Set ol = Nothing

If Err.Number <> 0 Then
MsgBox "Got an error when trying to send the e-mail using Outlook. Error number: " & CStr(Err.Number) & " " & Err.Description & Err.Source
Err.Clear
End If

End Sub

For some reason, the original macro I posted does not work for me. Or, I know why it doesn't work, but I don't understand why I wrote it like that. The problem there is that I concatenate the local check out path with the file name of the document, but the document already has a path. The new version just use the file name of the file copied to disk.
 

Userlevel 7
Badge +30

Note: There seem to be a bug where, even though the send mail macro runs, the page also tries to send the document by mail using the standard functionality. I.e. you might get two e-mails opened. Now, if you have problems with the send mail function in the first place you will not see this. But if you have written your own send mail macro because ... of reasons ... this bug might affect you.
 

Badge +4

Hi Mathias,

I followed above steps according to my requirement. Below I attached my VB code and Macro Setup. 

VB code

 

Document Macro Setup

 

After I press the command button “Send Mail”, It try to open up new mail but below error message pop up in the screen

 

 

Do you have any idea why this is pop up?

 

/Isuru. 

Hi @isujlk @Mathias Dahl 

have you finally found the solution to this error?

I followed the same procedure as you and encountered this error.

 

Thank you in advance for your help

Userlevel 7
Badge +30

@ArcElhadC Which IFS version do you use?

 

Userlevel 7
Badge +30

Adding to the above, I want to mention that we now have a new version of the Aurena Agent where the problem has been fixed where some users could not send e-mails. It was due to a mismatch between the "bit version" of Outlook and the Aurena Agent. So, the need for a macro to send e-mails, which for some was a workaround, is not as great.

So, @ArcElhadC, do you still want help with the macro?

The latest Aurena Agent MSI (installation program) can always be downloaded from this location:

https://ifsdownloads.blob.core.windows.net/addon/AurenaAgent/SetupAurenaAgent.msi

It's compatible with Aurena on Apps 10 as well as IFS Cloud Web Client.
 

Badge +4

Hello @Mathias Dahl  tank you for the answer.

I’m using 23R1 version and  latest Aurena Agent MSI.

I was able to fix the error: it couldn't find the path to the file it was supposed to attach.

 

Userlevel 7
Badge +30

Thanks for the update! Would you be allowed to share your updated macro code?

Also, do you really need the macro? (see my previous comment)
 

Badge +4

Public Sub CreateNewOutlookMail

   On Error Resume Next

   Dim ol, newMail, olMailItem, file, path, objMail

   olMailItem = 0
   Set ol = CreateObject("Outlook.Application")

   Set newMail = ol.CreateItem(olMailItem)

    file = Replace(ClientScriptValues.Item("LOCAL_FILE_1").Value, "/", "\")

 MsgBox "This file will be attached: " & file ' Remove single quote at the beginning of this line to have the file name be displayed before the e-mail is created
 
 
   With newMail 
        .Subject = "Regarding " & ScriptValues.Item("DOCUMENT_TITLE").Value
        .Body = "This is the message body. You should replace it in the macro with something nicer." & vbCrLf
        .Attachments.Add "C:\Users\\Aurena Agent\Temp\" + file
        .Display ' Instead of .Display, you can use .Send to send the email or .Save to save a copy in the drafts folder
    End With
    

   Set ol = Nothing

   If Err.Number <> 0 Then
      MsgBox "Got an error when trying to send the e-mail using Outlook. Error number: " & CStr(Err.Number) & " " & Err.Description & Err.Source
      Err.Clear
   End If

End Sub

Badge +4

Thanks for the update! Would you be allowed to share your updated macro code?

Also, do you really need the macro? (see my previous comment)
 

HELLO @Mathias Dahl here is the update macro code.

we needed a feature that would allow the user to open outllok when printing or e-mailing a report. This would enable them to add another attachment, for example.

 Macro code allows us to open outlook, but it doesn't 100% meet our needs. Since the report has to be in the document revision page.

Userlevel 7
Badge +30

C:\Users\\Aurena Agent\Temp\

Is that the location of your local check out path, for all users? There is an extra backslash in there that you might want to remove.

Also, I did not get this:

Since the report has to be in the document revision page.

What do you mean by that? Is it a document with a checked in report file (PDF) that the user tries to send?

 

Badge +4

yes there is an extra backslash in there to remove.

And i mean to test the macro I went to the document revision page to find the report  and launched the mail sending function. But what we really want is to be able to open Outlook on the relevant page when the user is about to print and view the document.

In brief i want to open outlook and add more attachments from  here 

 

Userlevel 7
Badge +30

In brief i want to open outlook and add more attachments from  here 

That is not possible today, sorry

What can be done though, is opening Outlook when a user uses the print or view commands in the document revision screen (or even Attachments / Documents on any screen where that's enabled). Perhaps this is what you are trying... As a user I would find it very annoying though, if I don't want to send an e-mail with the document at some point, when I view it. One way to handle that is to figure out, with some logic in the macro code, possibly looking at some attributes/values from the document, before an Outlook e-mail opens, so that it only opens sometimes, and sometimes the document just prints normally.

I think people have requested ways to include other documents to be added when we execute or preview a report from that dialog. But to be on the safe side, you could ask for it to be added by filing an idea in the Ideas section here on IFS Community.

Badge +4

 

Yes Filing this idea in the Ideas section on IFS Community is a great suggestion, and I will look into that as well. Thanks again for your insights.

Reply