question

PhamTuan-0438 avatar image
0 Votes"
PhamTuan-0438 asked PhamTuan-0438 published

How to call modules in ThisOutlookSession

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 ""A@f.com""

myFwd.Recipients.Add ""B@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 ""A@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 ""B@f.com""


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

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

myFwd.HTMLBody = xStr1 & xStr2 & Item.HTMLBody


myFwd.Send

Set myFwd = Nothing

End Sub


office-outlook-itprooffice-vba-devoffice-scripts-excel-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you try something like this: If condition A Then Module1.AutoForwardAllSentItems myFwd?


1 Vote 1 ·

1 Answer

PhamTuan-0438 avatar image
0 Votes"
PhamTuan-0438 answered PhamTuan-0438 published

Thank you. I will try it.

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

Could you please help me how to code in Case 2: If the mail is sent TO A@f.com B@f.com C@f.com -> AutoForward To A@x.com B@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 A@ x.com'
If recip = "A@ x.com" Then
Recipient = "A@ f.com"
xStr1 = "<p>A1</p>"
xStr2 = "<p>A2</p>"

'Case 2: If the mail is sent TO A@ x.com B@ x.com C@ x.com'
ElseIf recip = "A@ x.com" "B@ x.com" "C@ x.com" Then
Recipient = "@ f.com"
Recipient = "B@ f.com"
Recipient = "C@ 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







5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.