How to call modules in ThisOutlookSession

Pham Le Anh Tuan (FJP.GE.DFT) 1 Reputation point
2022-03-04T04:44:22.93+00:00

Hello everyone,

I want to write a script that combines ThisOutlookSession and module.

For example currently I have the below script in ThisOutlookSession


Sub AutoForwardAllSentItems(Item As Outlook.MailItem)

Dim strMsg As String

Dim myFwd As Outlook.MailItem

Set myFwd = Item.Forward

myFwd.Recipients.Add ""******@f.com""

myFwd.Recipients.Add ""******@f.com""

xStr1 = ""<p>A</p>""

xStr2 = ""<p>B</p>""

myFwd.HTMLBody = xStr1 & xStr2 & Item.HTMLBody

myFwd.Send

Set myFwd = Nothing

End Sub"


Could you help me how to write in ThisOutlookSession so that

If condition A, run module 1

If condition B, run module 2


Module 1

Sub AutoForwardAllSentItems(Item As Outlook.MailItem)

Dim strMsg As String

Dim myFwd As Outlook.MailItem

Set myFwd = Item.Forward

myFwd.Recipients.Add ""******@f.com""

xStr1 = ""<p>A1</p>""

xStr2 = ""<p>A2</p>""

myFwd.HTMLBody = xStr1 & xStr2 & Item.HTMLBody

myFwd.Send

Set myFwd = Nothing

End Sub


Module 2

Sub AutoForwardAllSentItems(Item As Outlook.MailItem)

Dim strMsg As String

Dim myFwd As Outlook.MailItem

Set myFwd = Item.Forward

myFwd.Recipients.Add ""******@f.com""

xStr1 = ""<p>B1</p>""

xStr2 = ""<p>B2</p>""

myFwd.HTMLBody = xStr1 & xStr2 & Item.HTMLBody

myFwd.Send

Set myFwd = Nothing

End Sub


Microsoft 365 and Office | Development | Other
Outlook | Windows | Classic Outlook for Windows | For business
Developer technologies | Visual Basic for Applications
{count} votes

1 answer

Sort by: Most helpful
  1. Pham Le Anh Tuan (FJP.GE.DFT) 1 Reputation point
    2022-03-08T03:00:57.853+00:00

    Thank you. I will try it.

    I tried to rewrite my code as below
    Case 1: If the mail is sent TO ******@f.com -> AutoForward To ******@x.com
    Case 2: If the mail is sent TO ******@f.com ******@f.com ******@f.com -> AutoForward To ******@x.com ******@x.com

    Could you please help me how to code in Case 2: If the mail is sent TO ******@f.com ******@f.com ******@f.com -> AutoForward To ******@x.com ******@x.com

    Sub AutoForwardAllSentItems(Item As Outlook.MailItem)

    Dim myFwd As Outlook.MailItem
    Set myFwd = Item.Forward

    Dim xStr1 As String
    Dim xStr2 As String
    Dim Recipient As String

    Dim recips As Outlook.Recipients
    Dim recip As Outlook.Recipient
    Dim pa As Outlook.PropertyAccessor
    Const PR_SMTP_ADDRESS As String = _
    "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
    Set recips = mail.Recipients
    For Each recip In recips

    'Case 1: If the mail is sent TO ******@x.com'
    If recip = "******@x.com" Then
        Recipient = "******@f.com"
        xStr1 = "<p>A1</p>" 
        xStr2 = "<p>A2</p>"
    
    'Case 2: If the mail is sent TO ******@x.com ******@x.com ******@x.com'
    ElseIf recip = "******@x.com" "******@x.com" "******@x.com" Then
        Recipient = "@f.com"
        Recipient = "******@f.com"
        Recipient = "******@f.com"
        xStr1 = "<p>B1</p>" 
        xStr2 = "<p>B2</p>"
    Else
        MsgBox "None of the conditions was true, abort."
        Exit Sub
    End If
    

    Next

    myFwd.Recipients.Add Recipient
    myFwd.HTMLBody = xStr1 & xStr2 & Item.HTMLBody

    myFwd.Send
    Set myFwd = Nothing

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.