Accessing Your Office E-mail
Sometimes you may have to access your office e-mail when you are in other places and cannot logon (remotely). If you are using Microsoft Outlook, then it is pretty easy to access your e-mail remotely. In this article, I am including a simple VB subroutine that can be installed as a macro in Outlook. It helps you with the following:
- Gets a list of all messages in one of your Outlook folders.
- Forwards all messages in your Outlook folder to your other e-mail account.
- Deletes all messages in your Outlook folder whose subject line contains a string you provided.
Here is what you need to do to use it:
- Copy the source code from the download file and use it to create a macro in Outlook.
- Adjust Outlook security to enable your macro.
- Create a new rule in Outlook that invokes this macro whenever an e-mail from your outside account arrives (such as from YourName@yahoo.com). Please note that you should not invoke this macro for all incoming messages, otherwise anyone will be able to access your e-mail.
- Leave Outlook running. The macro will not work when you shutdown Outlook.
- Send a request as described below from your outside account (YourName@yahoo.com) to your Outlook account.
Note: If you are using Outlook 2003, then there will be an annoying dialog box asking for your confirmation whenever your macro is invoked. In that case, you have to either write or download a program to get rid of the dialog box.
The Request Message
The subject line of the request message indicates request type. It has to end with number 31415926. The body of the request message provides additional parameters, such as parent folder name, child folder name, and subject string.
- Get Message List Request: The subject line has to be "
GetMsgList31415926
", the message body is ParentFolderName\ChildFodlerName
. After sending this request, you will receive a message listing all of your Outlook e-mails in the specified child folder. The list will contain message subject, sender address, and received time.
- Get Messages Request: The subject line has to be "
GetMsgs31415926
", the message body is ParentFolderName\ChildFodlerName
. After sending this request, all your Outlook e-mails in the specified child folder will be forwarded to you.
- Delete Messages Request: The subject line has to be "
DeleteMsgs31415926
", the message body is ParentFolderName\ChildFodlerName\SubjectString
. After sending this request, all of your Outlook e-mails in the specified child folder that contain the specified SubjectString
in message subject will be deleted. If you do not specify a SubjectString
in your request, then all messages in the child folder will be deleted.
- Get Folder List Request: The subject line has to be "
GetFolderList31415926
", the message body is ignored. After sending this request, you will receive a message that contains all of your Outlook folders (parent folders plus child folders). This request is intended to help you with sending the above three requests.
Note: If your Outlook folder structure has more than two layers, then you need to modify the VB code to make it work.
The Code with Comments
Sub ProcessMsg(msg As MailItem)
On Error GoTo TheEnd
Dim msgSender As String
msgSender = msg.SenderEmailAddress
Dim msgType As String
msgType = UCase(Trim(msg.subject))
Dim params() As String
params = Split(msg.Body, "\")
If Right(msgType, 8) = "31415926" Then
msg.Delete
Dim app As Application
Set app = CreateObject("Outlook.Application")
Dim ns As NameSpace
Set ns = app.GetNamespace("MAPI")
If msgType = "GETMSGS31415926" Then
If UBound(params) < 1 Then GoTo TheEnd
Dim i As MAPIFolder
For Each i In ns.Folders
If UCase(Trim(i.Name)) = UCase(Trim(params(0))) Then
Dim j As MAPIFolder
For Each j In i.Folders
If UCase(Trim(j.Name)) = UCase(Trim(params(1))) Then
Dim fwd As MailItem
Dim k As MailItem
For Each k In j.Items
Set fwd = k.Forward
fwd.To = msgSender
fwd.Send
Next
End If
Next
End If
Next
ElseIf msgType = "GETMSGLIST31415926" Then
If UBound(params) < 1 Then GoTo TheEnd
Dim listMsgs As MailItem
Set listMsgs = app.CreateItem(olMailItem)
listMsgs.To = msgSender
listMsgs.subject = "Outlook message list"
Dim msgList As String
msgList = vbCrLf
Dim r As MAPIFolder
For Each r In ns.Folders
If UCase(Trim(r.Name)) = UCase(Trim(params(0))) Then
Dim s As MAPIFolder
For Each s In r.Folders
If UCase(Trim(s.Name)) = UCase(Trim(params(1))) Then
Dim c As Integer
c = 1
Dim t As MailItem
For Each t In s.Items
msgList = msgList & vbCrLf & c & vbCrLf & " " & _
t.subject & vbCrLf & " " & t.SenderEmailAddress & _
vbCrLf & " " & t.ReceivedTime
c = c + 1
Next
End If
Next
End If
Next
listMsgs.Body = msgList
listMsgs.Send
ElseIf msgType = "DELETEMSGS31415926" Then
If UBound(params) < 1 Then GoTo TheEnd
Dim filter As String
If UBound(params) = 2 Then filter = UCase(Trim(params(2))) Else filter = ""
Dim x As MAPIFolder
For Each x In ns.Folders
If UCase(Trim(x.Name)) = UCase(Trim(params(0))) Then
Dim y As MAPIFolder
For Each y In x.Folders
If UCase(Trim(y.Name)) = UCase(Trim(params(1))) Then
Dim z As MailItem
If filter = "" Then
Set z = y.Items.GetLast
While Not z Is Nothing
z.Delete
z = y.Items.GetLast
Wend
Else
For Each z In y.Items
If InStr(UCase(Trim(z.subject)), filter) > 0 Then
z.Delete
End If
Next
End If
End If
Next
End If
Next
ElseIf msgType = "GETFOLDERLIST31415926" Then
Dim listFolders As MailItem
Set listFolders = app.CreateItem(olMailItem)
listFolders.To = msgSender
listFolders.subject = "Outlook folder list"
Dim folderList As String
folderList = vbCrLf
Dim m As MAPIFolder
For Each m In ns.Folders
folderList = folderList & vbCrLf & m.Name
Dim n As MAPIFolder
For Each n In m.Folders
folderList = folderList & vbCrLf & " " & n.Name
Next
Next
listFolders.Body = folderList
listFolders.Send
End If
End If
TheEnd:
End Sub
History