Consulting

Results 1 to 13 of 13

Thread: Multiple script on Outlook

  1. #1
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location

    Red face Multiple script on Outlook

    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

  2. #2
    Unless you post the code that you are trying to use, how can we suggest how to address the problem?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location
    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

  4. #4
    How do the other macros differ from this one?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location
    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)"?

  6. #6
    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.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location
    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?

  8. #8
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location
    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!

  10. #10
    What are you trying to achieve with the conditions?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location
    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

  12. #12
    VBAX Regular
    Joined
    Jan 2021
    Posts
    7
    Location
    any update? thanks

  13. #13
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •