divert outlook mails after changing attachment using VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I wish to divert all my mails that come from User A to users B & C. Also
while diverting, i need to check if the filename from 'A' is in mht format, i
wish to change the extension that to .xls & send to B & C.
Is this possible in VBA ?
i am using Microsoft outlook 2003(11.8118.8132) SP2

Thanks,
 
All that is possible:

- use the MAPIFolder.Items.ItemAdd to trap incoming mails
- use MailItem.SenderName or MailItem.SenderEmailAddress to determine
whether this is "User A"
- use MailItem.Attachments to inspect all Attachment.FileName properties for
the extension you're looking for
- use Attachment.SaveAsFile to save it to disk, then use the Name statement
to rename it
- use MailItem.Attachments.Remove and MailItem.Attachments.Add to replace
the attachment, then MailItem.Save to commit your changes
- use MailItem.Forward to redirect to other users
- use MailItem.Recipients.Add to add the other users
- call MailItem.Send

All examples above are using the actual object names for reference purposes
- use variables to create instances of these objects with your code.
 
Thanks for this logic.. i tried implementing this protocol... but i am not
able to trap the address at run time...

I have the following code embedded in the outlook session >> Can you please
help me in correcting if anything is not initialised / incorrect?

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeOf Item Is Outlook.MailItem Then
Cancel = Not SendBigMail(Item)
End If
End Sub


Private Function SendBigMail(oMail As Outlook.MailItem) As Boolean
Dim lSize As Long
Dim bSend As Boolean
Dim FileName As String
Dim TAtt As Long
Dim l As Long
Const MIN_SIZE As Long = 50000
oMail.Save


bSend = True

If oMail.SenderEmailAddress = "(e-mail address removed)" Then
TAtt = oMail.Attachments.count
'Total number of attachments in my mail
If TAtt > 0 Then
l = 1
For l = 1 To TAtt
lSize = oMail.Size

If (Right(oMail.Attachments.Item(l).FileName, 3) = "mht") Or
(Right(oMail.Attachments.Item(l).FileName, 3) = "xls") Or
(Right(oMail.Attachments.Item(l).FileName, 3) = "pdf") Then
If lSize < MIN_SIZE Then
oMail.To = "Chitra Bhatia"
oMail.Subject = "Blank report Check " & oMail.Subject
' If its a blank attachment then send me a
notification mail
SendBigMail = bSend
Exit Function
End If
' Change the .mht file to xls before sending
If Right(oMail.Attachments.Item(l).FileName, 3) = "mht"
Then
FileName =
Replace(oMail.Attachments.Item(l).FileName, "mht", "xls", 1, 3)
oMail.Attachments.Item(l).SaveAsFile FileName
Set oMail.Attachments.Item(l) =
oMail.Attachments.Add(FileName, olByValue)
End If
End If
Next
For l = 1 To TAtt
If Right(oMail.Attachments.Item(l).FileName, 3) = "mht" Then
oMail.Attachments.Item(l).Delete
l = l - 1
End If
Next
End If
End If


SendBigMail = bSend
End Function
 
Wrong event! Item_Send captures *outgoing* e-mail, not incoming. Below is
some sample code that illustrates how to trap incoming e-mail.

Option Explicit
Private WithEvents NewMailItems As Outlook.Items

Private Sub Application_Quit()
Set NewMailItems = Nothing
End Sub

Private Sub Application_Startup()
Set NewMailItems =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub NewMailItems_ItemAdd(ByVal Item As Object)
'THIS WILL FIRE FOR EVERY NEW E-MAIL; YOU CAN USE THE
'Item OBJECT TO WORK WITH THE PROPERTIES OF THE E-MAIL MESSAGE

Dim objMail As Outlook.MailItem

If Item.Class <> olmail Then Exit Sub

Set objMail = Item
objMail.UnRead = False
objMail.FlagStatus = olNoFlag
End Sub

--
Eric Legault - Outlook MVP, MCDBA, MCTS (SharePoint programming, etc.)
Try Picture Attachments Wizard for Outlook:
http://www.collaborativeinnovations.ca
Blog: http://blogs.officezealot.com/legault/
 
Back
Top