2

Goal: To have a button in the Outlook toolbar which, when pressed, sends the current message in it's entirety (perhaps as a .msg file) via post to a URL. Some kind of browser window must be opened in the process so the user can enter login details for the application at the URL (unless I can get around this somehow, thoughts?).

I have created a VBA Macro and put a button for it in Outlook 2007 (though I'll need this to work in Outlook 2003 too, at least).

I've read about the VBA FollowHyperlink method, but I can't get the little VBA Macro IDE to recognise it (Application.FollowHyperlink and several other attempts gave errors).

(I'm a .Net programmer new to VBA, so if there is a simpler way in VB.NET or C# by all means point me in the right direction).

Ideas?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
MGOwen
  • 6,562
  • 13
  • 58
  • 67

1 Answers1

4

Here is an outline of how I think it should work.

  • For the communication, use the a "WinHttp.WinHttpRequest.5" COM object (investigating FollowHyperlink() will be pointless). This object can do any form of HTTP request you can think of. Reference it in the Outlook VBA project to get Intellisense and early binding.
  • For username and password, create a tiny custom form with two text boxes. You can even use that form to cache username and password (call "Hide" when the form should go out of view - all form level variables will be retained so you can use them again).
  • For preparing the payload, use this method (KB240935) to get the currently selected item, check it for type (If TypeOf currentItem Is MailItem Then ...), and call SaveAs() to save it as .msg in a temporary folder. Attach it to your prepared POST request, and you are good to go.
  • For the user interface, put a button in a custom CommandBar (you can even create one programmatically at Application start). Link the button to a Public Sub in an ordinary VBA module. In this Sub, decide if you need to Show() your custom username/password dialog, or if cached credentials exist, and do the necessary HTTP request handling.

Good luck! It should not be too complicated.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    You can use the same idea a Tomalak answers using VSTO and C# and object such as Webclient etc you may have to be careful of the 2007, 2003 outlook references if you want it to working in both. – 76mel May 12 '09 at 08:59
  • Though VBA might be the more portable solution, even if (or exactly because) it is less modern. I would also say it is easier to implement. – Tomalak May 12 '09 at 09:23
  • Thanks Tomalak, I'll give it a shot. By the way, why would investigating FollowHyperlink be pointless? – MGOwen May 13 '09 at 23:16
  • Update: I am trying to implement this, I plan to accept the answer and post more details here for posterity if successful. – MGOwen May 20 '09 at 01:11
  • I'm curious what you come up with. :-) – Tomalak May 20 '09 at 05:10
  • Was a solution found? – QHarr Jun 19 '18 at 06:25
  • @QHarr The post is from 2009, so... probably not, or the OP didn't forgot to share. But the general approach hasn't changed, I'd try going down the same route today. – Tomalak Jun 19 '18 at 06:33
  • @Tomalak Thanks for responding. Do you happen to know of any APIs where one can practice sending a JSON string POST request with VBA and examing the responseText? – QHarr Jun 19 '18 at 06:34
  • Use the VBA IDE in any Office program. There is an interactive debugger that lets you try all kinds of things interactively. There are many samples of VBA sending HTTP requests all over the Internet, and I'm sure there is a JSON-encode function available as well (a quick search reveals https://github.com/VBA-tools/VBA-JSON). – Tomalak Jun 19 '18 at 06:45