We help IT Professionals succeed at work.

VBA for Outlook

Frank Freese
Frank Freese asked
on
Good morning folks,
This is a VBA Outlook need. In searching the Internet for Outlook VBA examples I haven't found an example of what I would like to do.
I send a lot of emails to myself so when I go to the To: selection or Copy: selection I have to type in my email address. Is it possible to have code that will allow me to click on To or Copy then using the short-cut menu click on an option to add my email address with having to type it in all the time? I would be interested to see what the code looks like. I use Outlook 2016 and have not even located a publication for VBA Outlook programming.
Comment
Watch Question

Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Once you have entered your email address in the To field on one message Outlook should add that to the "nicknames" that it stores, and the next time you send a message and are in the To field and type the first letter or two of your email address it should drop down a list of matching emails where you can select yours.  That feels probably as easy as trying to map a keyboard shortcut to it, which Outlook doesn't support very well.

If you really want to just click a key combo and have your email filled in then I might look outside Outlook, at a keyboard automation tool.  I purchased KeyText a while back and use it for some similar tasks, but it isn't free.  There are others though, a few mentioned below but Google will probably find you more...



»bp

Author

Commented:
Thanks Bill. I will consider your suggestions, however, and it may not be possible with VBA for Outlook
Darrell PorterEnterprise Business Process Architect

Commented:
Why not have a desktop shortcut with a key assignment which runs the following command:
"C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE" /c IPM.note /m email@address.com

Open in new window


You can then press the key combination you assign to the shortcut and, as long as Outlook is already open, a window with a new message addressed to you at mail@address.com will appear.

Author

Commented:
Let me chew on this - interesting
Darrell PorterEnterprise Business Process Architect

Commented:
In the event you want a VBA Macro you can put on the Ribbon, use the following code
Public Sub NotesToMyself()
  Set OL = CreateObject("Outlook.Application")
  Set EmailItem = OL.CreateItem(olMailItem)
  Set olNS = OL.GetNamespace("MAPI")
  tofield = olNS.Accounts.Item(1).SmtpAddress
    With EmailItem
      .Subject = "Note To Self"
      .Body = "[Enter your message here]" & vbCrLf & _
      "" & vbCrLf & _
      .To = tofield
      .Importance = olImportanceNormal
      .Send
  End With
End Sub

Open in new window


The top 4 lines determine your email address and it is inserted at the .To = tofield so the code can be shared with others without hard-coded email addresses.

Author

Commented:
Darrel,
I copy and pated your code to a new procedure. However, in attempting to run it I get the following error:
Notes to MyselfCould use a little hep here, please.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
This link provides info on enabling macros in Outlook.

How to enable and disable macros in Outlook?


»bp

Author

Commented:
Darrel,
Have you tested this code. I get all types of problem. I do have Option Explicit enabled.
Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
Frank,

You could try these adjustments to that subroutine, it seems to do closer to what you want in a simple text here...

Public Sub NotesToMyself()
  Dim Ol, EmailItem, olns, tofield
  ' Set OL = CreateObject("Outlook.Application")
  Set Ol = Application
  Set EmailItem = Ol.CreateItem(olMailItem)
  Set olns = Ol.GetNamespace("MAPI")
  tofield = olns.Accounts.Item(1).SmtpAddress
    With EmailItem
      .Subject = "Note To Self"
      .Body = "[Enter your message here]" & vbCrLf & "" & vbCrLf
      .To = tofield
      .Importance = olImportanceNormal
      .Display
  End With
End Sub

Open in new window


»bp

Author

Commented:
Thanks to all. Bill, I can now study this code. Appreciate everyone
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome Frank, hope you get something that works well for you.


»bp
Darrell PorterEnterprise Business Process Architect

Commented:
Bill, nice work!  I literally just hacked something together from toilet paper and white glue and you polished it into something splendid.  Thank you!