Showing posts with label VBA. Show all posts
Showing posts with label VBA. Show all posts

Using Signed Macros with MS Office

Signing excel apps via a digital certificate can help guarantee that no one has tampered with the code and that what you are executing is what was originally written by the original signer. Further, it helps lock down your security policy. By only running signed code and only importing certificates from people you trust, you can prevent a situation where an an excel document can hijack your whole computer (because it can)

If you rely on VBA solutions within your organization, make sure you sign and then import certificates for all projects.

Sign with Certificate

When creating a VBA project that uses Macros, you can easily sign the project with a self signed certificate using the following steps:

  1. If you don’t already have one, you’ll need to create a self-signed certificate on your computer. If you do have one, you can skip to step 3. If not, go to the Start Menu > All Programs > Microsoft Office > Microsoft Office Tools > and open Digital Certificate for VBA Projects
    • 1
  2. Enter a name for your certificate and Click OK
    • 2
  3. Open up your VBA project in Excel, Word, Outlook (etc) and hit Alt + F11 to open up the VBA Project and go to Tools > Digital Signature
    • 3
  4. On the Digital Signature Menu, Click “Choose”
    • 4
  5. You should see the Certificate we just created. Select that and hit OK
    • 5

Import Certificate From Someone Else

When the file is opened on another machine, it won’t recognize the original certificate which only exists on the original author’s machine. Using the following steps we can import the certificate on the client’s machine.

Disclaimer: You should only perform these steps when you are absolutely certain that the file has originated from someone you trust to execute any commands on your machine without your knowledge.

  1. Open the Excel File with Macros and click “Options”
    • 1
  2. Click “Show Signature Details”
    • 2
  3. Click “View Certificate”
    • 3
  4. Click “Install Certificate”
    • 4
  5. Install Wizard Will Open
    • 5
  6. Select “Place all certificates in the following store” and Click “Browse”
    • 6
  7. Select “Trusted Root Certification Authorities”
    • 7
  8. Click OK / Next several times
    • 8
  9. Accept the Security Warning to Install the Certificate
    • 9
  10. On the original Dialog, Select “Trust all documents from this publisher” and Click OK
    • 10

Manage Certificates

If you want to go in and make changes to your certificates directly, you can view and edit all of the certs on your current system with the following steps.

  1. Hit Win + R to open up the Run Dialog
  2. Type “certmgr.msc” and Click OK
    • 1
  3. You should see the certificate we added in the Personal/Certificates folder
    • 2

Alternatively: You can also load your Computer’s Management Console by going to Start > Run mmc and then going to File > Add Snap In and then selecting Certificates.

And that’s it! Adding certificates to your projects is a great way to keep security in mind while providing some sort of automation within Office and other products.

Making Sure Outlook Calendar Reminders Actually Pop-up in Windows 7

Introduction

In Windows 7, MS Outlook seems to be horribly bad at popping up calendar reminders when outlook doesn’t have focus. This can cause a series of problems from minor annoyances to missed meetings. As discussed in this StackExchange question, there are some relatively easy work arounds to this issue that I’ll elaborate on here. If you’ve never used Outlook’s VBA editor to extend it’s capabilities before, this article will take you through how to set up everything you’ll need to do, start to finish. If you’re already familiar with using VBA in Outlook, then you can just skip to Step 5 and paste in the code and you’ll be all set.

Step By Step

  • 1) Hit the Windows Key and type “certificate”
  • 2) Select Digital Certificate for VBA Projects and type in a name for your certificate and hit OK
  • 3) Open Outlook 2010 and Hit Alt + F11 to start the VBA editor
  • 4) In the Project Pane on the left, Expand Project1 > Microsoft Outlook Objects > and double click on ThisOutlookSession
  • 5) Paste in the following code exactly:
'ensures all reminder notices receive focus
Private Sub Application_Reminder(ByVal Item As Object)
    If TypeOf Item Is AppointmentItem Then
        Application.ActiveWindow.Activate
    End If
End Sub

Edit: Added New Better Code Below:

'Declare Functions From User32 Library  
Private Declare PtrSafe Function FindWindowA Lib "user32" _  
        (ByVal lpClassName As String, _  
         ByVal lpWindowName As String) As Long  
Private Declare PtrSafe Function SetWindowPos Lib "user32" _  
        (ByVal hwnd As Long, _  
         ByVal hWndInsertAfter As Long, _  
         ByVal X As Long, _  
         ByVal Y As Long, _  
         ByVal cx As Long, _  
         ByVal cy As Long, _  
         ByVal wFlags As Long) As Long  

'Declare Constants  
Private Const SWP_NOSIZE = &H1  
Private Const SWP_NOMOVE = &H2  
Private Const FLAGS As Long = SWP_NOMOVE Or SWP_NOSIZE  
Private Const HWND_TOPMOST = -1  

'Only show the message the first time  
Private messageAlreadyShown As Boolean  

'Finds Reminder Window and Brings to TopMost position  
Private Sub Application_Reminder(ByVal Item As Object)  
    On Error Resume Next  

    'show message box for first reminder  
    If Not messageAlreadyShown Then  
        MsgBox "First Reminder", vbSystemModal, ""  
        messageAlreadyShown = True  
    End If  

    'find reminder window  
    ReminderWindow = FindWindowA(vbNullString, "1 Reminder")  
    'bring reminder window to front  
    SetWindowPos ReminderWindow, HWND_TOPMOST, 0, 0, 0, 0, FLAGS  
End Sub 
  • 6) Sign the Macro by going to Tools > Digital Signature and clicking Choose
  • 7) Select the certificate you created earlier and hit OK
  • 8) Select OK again, hit Ctrl + S to save and exit the VBA window
  • 9) To Enable Macros, Go to File > Options and select Trust Center from the left window
  • 10) Run the Trust center by clicking the Trust Center Settings button on the right.
  • 11) From the Trust Center, select Macro Settings, and select “Notifications for digitally signed macros, all other macros disabled” and hit OK
  • 12) Exit Outlook - It will ask you if you want to save the project, click Yes
  • 13) Start Outlook - It will give you a security notice. Select “Trust all documents from this publisher” (You can first confirm that you are the publisher by selecting “Show Signature Details”)
  • 14) That’s it! You’re all set. You never have to touch any of that code again or miss another meeting (unintentionally)

UPDATE!

I’ve update the code to use ActiveWindow instead of ActiveExplorer, which returns nothing “if no explorer is active.” Thanks to CW for the impetus to update my code.