Post email data to the Web

April 8, 2009JPNo CommentsRate This Article


    In Outlook VBA macro to post email body to the web, Jeremy Slade posts an Outlook macro to publish emails to a web server.

    The code uses the XMLHTTP Object to send POST data to a specified URL. I'm not familiar with the actual site being used, but the XMLHTTP object leads me to believe the interface is super fast.

    If an email slips through the script rule, however, you'll be stuck writing code that imports the message manually. So I've taken the liberty of doing so myself.

    This code will call Jeremy's PublishMsgToWeb macro on a selected or opened message, so you can process any email manually. You can either run it after selecting an email in a folder (ActiveExplorer.Selection.Item(1)) or after opening the message for display (ActiveInspector.CurrentItem).

Sub CallPublish()

Dim Msg As Outlook.MailItem

' set reference to open/selected mail item
On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set Msg = ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set Msg = ActiveInspector.CurrentItem
    Case Else
End Select
On Error GoTo 0

If Msg Is Nothing Then GoTo ExitProc

Call PublishMsgToWeb(Msg)

ExitProc:
End Sub

    Come to think of it, setting a reference to Msg like this is pretty common, so here's an encapsulated function that does it:

Function GetMessage() As Outlook.MailItem
' returns MailItem object reference to open/selected mail item

' if any error occurs, just exit
On Error GoTo ExitProc
Select Case TypeName(Application.ActiveWindow)
    Case "Explorer"
        Set GetMessage = ActiveExplorer.Selection.Item(1)
    Case "Inspector"
        Set GetMessage = ActiveInspector.CurrentItem
    Case Else
End Select

ExitProc:
End Function

Usage:

Sub CallPublish()
Dim msg As Outlook.MailItem

Set msg = GetMessage

If Msg Is Nothing Then GoTo ExitProc

Call PublishMsgToWeb(Msg)

ExitProc:
End Sub

About JP
I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space if you want to learn more about VBA. Keep Reading »

↑ Scroll to top
Previous Post:

Next Post:

Speak Your Mind

Tell us what you're thinking...

Certain comments (including first-time comments) are subject to moderation and will not appear immediately. Please view the Comment Policy for more information. To post VBA code in your comment, use tags like this: [cc lang='vb']Code goes here[/cc].




Site last updated July 26, 2010 @ 8:14 pm