XTreme Logo

If you appreciate these tips, please consider a small donation:

OLE Automation Tip

Automation of MSN Messenger from VB Code
Tested with VB6
Courtesy of Nicole Jordan

This program is used when there are multiple MSN/Hotmail accounts on the same computer.  You will need a form with a frame, and as many option buttons (in a control array) as e-mail accounts. Note that this was originally for a much older version of MSN Messenger, and the object model subsequently changed. This may not be useful at all anymore, except as inspiration.

1 - Start a new, standard VB project.

2 - Add a frame to the form.

3 - Add the required number of option buttons in an array on the frame, with indexes beginning at 0.  This example uses a total of two, with indexes of 0 and 1.  You might want to caption each option button to indicate the account it logs onto.

4 - Add a reference to the Messenger Type Library (msmsgs.exe).

5 - Place the following code in the General Declarations section of the form code:

Option Explicit
Dim Msgrobj As New MsgrObject
Dim MsgrSrvobj As IMsgrService

Private Sub Form_Unload(Cancel As Integer)
  'set the object to nothing
  Set Msgrobj = Nothing
  Set MsgrSrvobj = Nothing
End Sub

Private Sub Optname_Click(Index As Integer)
  Dim i As Integer
  On Error Resume Next
  'tests to see if msn messenger is logged in
  If Msgrobj.LocalState <> MSTATE_OFFLINE Then
     'If it is logged in then it logs off
     'Used Do Events to give Msn Messenger time to _
     log off before logging in again

     For i = 0 To 20000
  End If
  'This logs in to the email specified below
  'The number of case statements, and option buttons, _
  is determined by the number of e-mails accounts.

  Select Case Index
   Case 0
    'these use the messenger object logon method _
    with the username, password, imsgservice (which _
    is a object in the messenger library) as arguments

    'Change e-mail address and password below to _
        the correct values for the password and address _
    to be used in each case.

    Msgrobj.Logon "e-mail address", "password", MsgrSrvobj 

   Case 1
    Msgrobj.Logon "e-mail address", "password", MsgrSrvobj 

  End Select 

  Unload Me

End Sub

5 – Run the program and test.  Click on each option as desired to see if Messenger logs onto the appropriate account.  Further explorations of what can be done through automation of Messenger are left as an exercise for the reader.

[ Back To The Top ]

Contact: web@xtremecomp.com
All contents copyright XTreme Computing unless noted otherwise.