Friday, February 18, 2011

Outlook Macro and Digital Signature

Digital Signature
 
Setting the security level to medium or low affects macros coming from all
sources (like virus-infected emails). After some more research, I've found a
way to do this without reducing the security level.

1. Create a digital certificate for yourself
Windows XP: Start, All Programs, Microsoft Office Tools, Digital Certificate
for VBA Projects.
Windows 2000: Start, Programs, Microsoft Office Tools, Digital Certificate
for VBA Projects.
(If the SelfCert.exe file is not on your computer, you might need to install
it.)
Follow the instructions to create a certificate.

2. Sign the Macro
In Outlook: Tools, Macro, Visual Basic Editor.
In the Project Explorer, select the project you want to sign.
Click Tools, Digital Signature. Click Choose, select the certificate, click
OK twice. Save.

3. When you open or run the macro, you’ll get a security warning. Check
“Always trust macros from this source”.

Your digital signature/certificate can be used for macros in all Office
applications.
 
 
Outlook Macro
 
Goto     Tools -> Macro -> Visual Basic Editor (Alt + F11)
Select   "Microsoft Office Outlook Objects" -> "ThisOutlookSession"
Type in below code :
 
Public WithEvents myOlItems As Outlook.Items
 
 
Public Sub Application_Startup()
 
   ' Reference the items in the Inbox. Because myOlItems is declared
   ' "WithEvents" the ItemAdd event will fire below.
   Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
 
End Sub
 
 
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
 
   ' If it's currently not between 9:00 A.M. and 5:00 P.M.
   If Time() < #9:00:00 AM# Or Time() > #5:00:00 PM# Then
 
      ' Check to make sure it is an Outlook mail message, otherwise
      ' subsequent code will probably fail depending on what type
      ' of item it is.
      If TypeName(Item) = "MailItem" Then
 
         ' Forward the item just received
         Set myForward = Item.Forward
 
         ' Address the message
         myForward.Recipients.Add "kadian.vaibhav@aol.in"
 
         ' Send it
         myForward.Send
 
      End If
 
   End If
 
   ' If it's currently not between 9:00 A.M. and 5:00 P.M.
   If Time() >= #9:00:00 AM# Or Time() <= #5:00:00 PM# Then
 
      ' Check to make sure it is an Outlook mail message, otherwise
      ' subsequent code will probably fail depending on what type
      ' of item it is.
      If TypeName(Item) = "MailItem" Then
 
         ' Forward the item just received
         Set myForward = Item.Forward
 
         ' Address the message
         myForward.Recipients.Add "vaibhav.kadian@aol.in"
 
         ' Send it
         myForward.Send
 
      End If
 
   End If
 
End Sub
 
 

1 comment:

Andrea said...

Great post. Thanks for sharing all the steps to reduce the security level and to create a certificate. I will try all these steps now to create a digital signature which I can use in all my applications.
digital signature Adobe Acrobat