[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 685
  • Last Modified:

Outlook 2003 VBA to Capture the name of the currently viewed contact on Custom Button Click

Using the built in VBA in Outlook,  how can I do the following:
1) Detect when the user is viewing a contact record
2) Obtain that contact record and
3) Write out the name of the contact to a flat file.
4) Add a button to any toolbar that will run this code and write out the name only on button click.

0
Howard Bash
Asked:
Howard Bash
  • 5
  • 5
1 Solution
 
David LeeCommented:
Hi, hbash.

Here's the code for doing this.  Follow these instructions to use it.

1.  Start Outlook.
2.  Click Tools->Macro->Visual Basic Editor.
3.  If not already expanded, expand Modules and click on Module1.
4.  Copy the code below and paste it into the right-hand pane of the VB Editor.
5.  Edit the code as needed.  I placed comment lines where things can, or need, to change.
6.  Click the diskette icon on the toolbar to save the changes.
7.  Close the VB Editor.
8.  Click Tools->Macro->Security.
9.  Change the Security Level setting to Medium.

To add the toolbar button:

1.  Click View->Toolbars->Customize
2.  Click the Toolbars tab
3.  Click New
4.  Name the toolbar
5.  Click the Commands tab
6.  Under Categories click Macros
7.  Under Commands click and hold on the macro Project1.GetContactInfo, then drag it out and drop it on the new toolbar
8.  Dock the toolbar somewhere onscreen

Sub GetContactInfo()
    Dim olkContact As ContactItem, objFSO As Object, objFile As Object
    If Application.ActiveInspector.CurrentItem.Class = olContact Then
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        'Change the file name and path of the output file on the following line
        Set objFile = objFSO.CreateTextFile("C:\ContactInfo.txt", True)
        Set olkContact = Application.ActiveInspector.CurrentItem
        objFile.WriteLine olkContact.FullName
        objFile.Close
        Set objFile = Nothing
        Set objFSO = Nothing
    End If
End Sub
0
 
Howard BashSenior Software EngineerAuthor Commented:
This is great.  I have not run it and I know it is what I need to get at.  Is there a way to detect when the user has openned a contact record and to then get that record's contact info (firstname,  lastname, ...)?  This would be good if I wanted to handle the contact record processing due to some outlook object event other than the button click.

Thanks,
Howard
0
 
Howard BashSenior Software EngineerAuthor Commented:
Also,  I created a button in vba code.  Is it possible to tie the GetContactInfo to the button programmatically?  The code follows:
Sub SetupCommandButton()
 ' MsgBox "Running Button Routine"
  Dim a As Application
  Dim cb As CommandBar
  Dim cbs As CommandBars
  Dim nCnt As Integer, nlp As Integer
  Dim cmdBarCtrl As CommandBarControl
  Dim bButtonAddedAlready As Boolean
 
  bButtonAddedAlready = False
 
  Set a = ThisOutlookSession.Application
  Set cbs = a.ActiveExplorer.CommandBars
 
  Set cb = cbs.Item(enCommandBars.Standard)
  nCnt = cb.Controls.Count
  For nlp = 1 To nCnt
    If cb.Controls(nlp).Caption = BUTTON_CAPTION Then
      bButtonAddedAlready = True
      Exit For
    End If
  Next nlp
   
    If Not bButtonAddedAlready Then
        Set cmdBarCtrl = cb.Controls.Add(msoControlButton, , , , True)
        With cmdBarCtrl
          .Caption = BUTTON_CAPTION
          .TooltipText = "Speaks the contact's name"
        End With
    End If
End Sub

Thanks,
Howard
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
David LeeCommented:
Hi, Howard.

Yes, both are possible.  To detect when a user opens a contact we need to hook the NewInspector event.  Something like this:

Private WithEvents olkInspectors As Outlook.Inspectors

Private Sub Application_Quit()
    Set olkInspectors = Nothing
End Sub

Private Sub Application_Startup()
    Set olkInspectors = Application.Inspectors
End Sub

Private Sub olkInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class = olContact Then
        GetContactInfo Inspector.CurrentItem
    End If
End Sub

Sub GetContactInfo(olkContact As ContactItem)
    Dim objFSO As Object, objFile As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Change the file name and path of the output file on the following line
    Set objFile = objFSO.CreateTextFile("C:\ContactInfo.txt", True)
    objFile.WriteLine olkContact.FullName
    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing
End Sub

Look at this question to see how I'd go about adding a button that fires code.
http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/Q_21977250.html
0
 
Howard BashSenior Software EngineerAuthor Commented:
That all worked.  You are imazing!!!  How and where can I get some Outlook smarts,  I have looked and googled and found some interesting stuff,  but no code for the various events and objects that do this kind of magic.

Thank you Sir,
Howard
back to top
0
 
David LeeCommented:
You're welcome, Howard.  The best place I've found for code is http://www.OutlookCode.com  I also highly recommend Sue Mosher's books.
0
 
Howard BashSenior Software EngineerAuthor Commented:
I have been to that URL, but have not found this type of code,  perhaps I need to dig deeper.  However, I will look int o Sue Mosher's books (any one in particular?).

I originally wanted to make the code an addin,  but the security issues with outlook2003 have made me nuts.  Code that I have written works on my dev box,  but for the life of me,  I cannot get it to run on another pc.  It installs,  but the load fails and the check box for the addin manager is disabled.  During those attempts to get past security,  I had found that site.  I have an ongoing incident with Microsoft about this and see that a lot of folks are having a horrible time with outlook due to this new security patch.
0
 
David LeeCommented:
Sue has books out for Outlook 2003 ("Microsoft Outlook Programming" ISBN: 1-55558-286-9) and 2007 ("Microsoft Outlook 2007 Programming" ISBN: 978-1-55558-346-0).  I also like "Programming Microsoft Outlook and Microsoft Exchange 2003" by Thomas Rizzo (ISBN: 0-7356-1464-4).

I also find Ken Slovak's site, http://www.slovaktech.com, very useful, especially the sample add-in templates.  
0
 
Howard BashSenior Software EngineerAuthor Commented:
So,  it only works if I am stepping through the code in the IDE...

Here is the Module1 Code:
  Option Explicit
 
  Enum enCommandBars
    TASK_PANE = 1
    CLIPBOARD = 3
    MENU_BAR = 4
    Standard = 5
    ADVANCED = 6
    WEB = 7
  End Enum
 
  Const BUTTON_CAPTION = "&Speak Contacts"
 
Sub SetupCommandButton()
  MsgBox "Running Button Routine"
  Dim a As Application
  Dim cb As CommandBar
  Dim cbs As CommandBars
  Dim nCnt As Integer, nlp As Integer
  Dim cmdBarCtrl As CommandBarControl
  Dim bButtonAddedAlready As Boolean
 
  bButtonAddedAlready = False
 
  Set a = ThisOutlookSession.Application
  Set cbs = a.ActiveExplorer.CommandBars
 
  Set cb = cbs.Item(enCommandBars.Standard)
  nCnt = cb.Controls.Count
  For nlp = 1 To nCnt
    If cb.Controls(nlp).Caption = BUTTON_CAPTION Then
      bButtonAddedAlready = True
      Exit For
    End If
  Next nlp
   
    If Not bButtonAddedAlready Then
        Set cmdBarCtrl = cb.Controls.Add(msoControlButton, , , , True)
       
        With cmdBarCtrl
          .Caption = BUTTON_CAPTION
          .TooltipText = "Speaks the contact's name"
         .FaceId = 59    'Find the button face id's here: http://www.outlookexchange.com/articles/toddwalker/BuiltInOLKIcons.asp
         .Style = msoButtonIconAndCaption
         .Visible = True
        End With
       
    End If
   
End Sub

Sub GetContactInfo(olkContact As ContactItem)
    SpeakMsg olkContact.FullName
End Sub

Sub SpeakMsg(sMsg As String)
 Dim v As SpVoice
 
 Set v = New SpVoice
 v.Speak sMsg, SVSFlagsAsync
 Set v = Nothing
 
End Sub

Here is the OutlookSession Code:

Private WithEvents olkInspectors As Outlook.Inspectors

Private Sub Application_OptionsPagesAdd(ByVal Pages As PropertyPages)
  Debug.Print "Application_OptionsPagesAdd TypeName(PropertyPages)=" & TypeName(PropertyPages)
End Sub

Private Sub Application_Quit()
  Set olkInspectors = Nothing
End Sub

Private Sub Application_Reminder(ByVal Item As Object)
  Debug.Print "Application_Reminder TypeName(Item)=" & TypeName(Item)
End Sub

Private Sub Application_Startup()

  Set olkInspectors = Application.Inspectors

  Module1.SetupCommandButton
 
End Sub

Private Sub olkInspectors_NewInspector(ByVal Inspector As Inspector)
    If Inspector.CurrentItem.Class = olContact Then
        GetContactInfo Inspector.CurrentItem
    End If
End Sub


If I set a breakpoint in the GetContactInfo and step through... it works, otherwise it appears to not be there.

To try this,  you need to add a project reference to sapi.dll (Microsoft Speech Engine Api)

If you have any thoughts here...THANKS!

0
 
David LeeCommented:
Howard,

I made the following two changes and it worked fine for me.

1.  Add this line of code

     .OnAction = "GetContactInfo"

    in the SetupCommandButton subroutine.  This tells Outlook what code to run when the button is clicked.

2.  Change your GetContactInfo subroutine to this:

    Dim olkSelected As ContactItem
    If Application.ActiveExplorer.Selection(1).Class = olContact Then
        Set olkSelected = Application.ActiveExplorer.Selection(1)
        SpeakMsg olkSelected.FullName
    End If
    Set olkSelected = Nothing
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now