PDA

View Full Version : Multiple script on Outlook



JL9
01-20-2021, 07:15 AM
Hello, I would like to know what should I do for the following situation:

-I have 3-5 scripts written in ThisOutlookSession(most of them are used as scripts to automictically send out emails with attachments to different recipients)

-Currently I can use only one script a time (I save it on ThisOutlookSession and it is working perfect)
- however, I have trouble running more than one script there
-In the near future I would like to run several script automictically, how am I supposed to do this? (I found out the you can have only one project/ThisOutlookSession) correct me if i am wrong

thank you so much for your kind assistance!

PS: I have to mention that I am newbie in VBA so I am not able to modify the code (I tried to combine two scripts on "ThisOutlookSession" but it was not working) I searched online and tried to paste my script into "Modules" as well but it was not working also

gmayor
01-20-2021, 09:49 PM
Unless you post the code that you are trying to use, how can we suggest how to address the problem?

JL9
01-21-2021, 05:50 AM
Private Sub Application_Reminder(ByVal Item As Object)
'Updated by Extendoffice 20200522
Dim xMailItem As MailItem
Dim xItemDoc As Word.Document
Dim xNewDoc As Word.Document
Dim xFldPath As String
On Error Resume Next
If Item.Class <> OlObjectClass.olAppointment Then Exit Sub
If Item.Categories <> "Send Schedule Recurring Email" Then Exit Sub
Set xMailItem = Outlook.Application.CreateItem(olMailItem)
Set xItemDoc = Item.GetInspector.WordEditor
xFldPath = CStr(Environ("USERPROFILE"))
xFldPath = xFldPath & "\MyReminder"
If Dir(xFldPath, vbDirectory) = "" Then
MkDir xFldPath
End If
xFldPath = xFldPath & "\AppointmentBody.xml"
xItemDoc.SaveAs2 xFldPath, wdFormatXMLDocument ' wdFormatXML
Set xNewDoc = xMailItem.GetInspector.WordEditor
VBA.DoEvents
xNewDoc.Application.Selection.HomeKey
xNewDoc.Activate
xNewDoc.Application.Selection.InsertFile FileName:=xFldPath, Attachment:=False
With xMailItem
.To = Item.Location
.Recipients.ResolveAll
.Subject = Item.Subject
.Attachments.Add "XXX\XXX\XXX\XXX.doc"
.Send
End With
Set xMailItem = Nothing
VBA.Kill xFldPath
End Sub

gmayor
01-21-2021, 09:33 PM
How do the other macros differ from this one?

JL9
01-22-2021, 06:04 AM
they are exactly the same, but when I put them under different modules. it doesnt work out.

Is it due to "Private Sub Application_Reminder(ByVal Item As Object)"?

gmayor
01-22-2021, 06:21 AM
You can only have one Application_Reminder in ThisOutlookSession. If you want it to cater for different conditions then you need a conditional statement to identify which conditions apply and then insert the code that relates to such a condition, but as you seem very coy about what the differences are I cannot help you with that.

JL9
01-22-2021, 08:03 AM
hello there,

thanks for all the useful tips! could I change the name of "Application_Reminder" so we can run multiple scripts on different modules? or actually we can combine all together into the "ThisOutlookSession" by using conditional statement?

the differences between all those scripts are that I have to send out on a different date, different recipients and different contents with more importantly the attachments. I need to insert different attachments for each individual scripts.

I think of one feasible solution my friend, can we create put all those different scripts into different modules instead of ThisOutlookSession? and it still manages to auto-send out based on the assigned date?

gmayor
01-22-2021, 10:05 PM
Application_Reminder is a built-in function. There can only be one and it goes in the ThisOutlookSession module.
You can use it to call other macros in other folders based on the conditions you are looking for in the message to determine what happens. e.g. You could test for the subject as below or for some other variable criteria and run a different macro based on the result.


Private Sub Application_Reminder(ByVal Item As Object)'Updated by Extendoffice 20200522
Dim xMailItem As MailItem
Dim xItemDoc As Word.document
Dim xNewDoc As Word.document
Dim xFldPath As String
On Error Resume Next
If Item.Class <> OlObjectClass.olAppointment Then Exit Sub
If Item.Categories <> "Send Schedule Recurring Email" Then Exit Sub


Select Case Item.Subject
Case "Subject 1"
Call Reminder1(Item)
Exit Sub
Case "Subject 2"
Call Reminder2(Item)
Exit Sub
Case "Subject 3"
Call Reminder3(Item)
Exit Sub
End Select
'Not one of the above so run the erst of the code below


Set xMailItem = Outlook.Application.CreateItem(olMailItem)
Set xItemDoc = Item.GetInspector.WordEditor
xFldPath = CStr(Environ("USERPROFILE"))
xFldPath = xFldPath & "\MyReminder"
If Dir(xFldPath, vbDirectory) = "" Then
MkDir xFldPath
End If
xFldPath = xFldPath & "\AppointmentBody.xml"
xItemDoc.SaveAs2 xFldPath, wdFormatXMLDocument ' wdFormatXML
Set xNewDoc = xMailItem.GetInspector.WordEditor
VBA.DoEvents
xNewDoc.Application.Selection.HomeKey
xNewDoc.Activate
xNewDoc.Application.Selection.InsertFile FileName:=xFldPath, Attachment:=False
With xMailItem
.To = Item.Location
.Recipients.ResolveAll
.Subject = Item.Subject
.Attachments.Add "XXX\XXX\XXX\XXX.doc"
.Send
End With
Set xMailItem = Nothing
VBA.Kill xFldPath
End Sub


Sub Reminder1(ByVal Item As Object)
MsgBox "Reminder1"
End Sub


Sub Reminder2(ByVal Item As Object)
MsgBox "Reminder2"
End Sub


Sub Reminder3(ByVal Item As Object)
MsgBox "Reminder3"
End Sub

JL9
01-31-2021, 11:17 PM
hello there,

thank you so much for the advice! I am not familiar with VBA so may I ask you for further assistance if you have time to do so.

for example, i wanna set up some conditions based on "Category name" or "appointment date/time", what will the VBA script look like? you can give me some sorta ideas if you are busy and i will try to code the rest

thanks for helping as always!

gmayor
02-01-2021, 05:46 AM
What are you trying to achieve with the conditions?

JL9
02-01-2021, 06:34 AM
i am trying to send out email automatically on a monthly-basis.

from my previous code, I set up an appointment with unique category name on Outlook as well as its specific time and recipients. Then once the email reminder comes out, it will trigger outlook and send out the email to relevant parties.

it works perfect until i found out that Application_Reminder is a built-in function and there can only be one in the ThisOutlookSession module

JL9
02-13-2021, 08:44 AM
any update? thanks

gmayor
02-14-2021, 03:47 AM
The code I posted provided alternatives according to the item.subject. You can change that to the categories associated with the appointments:

Select Case True
Case InStr(1, Item.Categories, "Category Name 1") > 0
Call Reminder1(Item)
Exit Sub
Case InStr(1, Item.Categories, "Category Name 2") > 0
Call Reminder2(Item)
Exit Sub
Case InStr(1, Item.Categories, "Category Name 3") > 0
Call Reminder3(Item)
Exit Sub
End Select