Microsoft KB Archive/177851

From BetaArchive Wiki

Article ID: 177851

Article Last Modified on 7/8/2005



APPLIES TO

  • Microsoft Collaboration Data Objects 1.21
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Collaboration Data Objects 1.1



This article was previously published under Q177851

SUMMARY

This article provides sample code that demonstrates how to create a Visual Basic based Messaging application using Collaboration Data Objects (1.1, 1.2, 1.21) that runs as a service under Windows NT version 4.0.

Note The information in this article does not apply to building a Microsoft Outlook application to run from a service. Running Outlook in a service is not recommended and is not supported because some functionality in Outlook that can cause dialogs to appear cannot be programmatically controlled or trapped, thereby halting execution.

MORE INFORMATION

This application is dependent on the Collaboration Data Objects (1.1, 1.2, 1.21) library being referenced by the Visual Basic project.

Since this application is being built with the intent to run as a service, it is built in a code module (not a form). In addition, you should select the Unattended Execution check box on the General tab of the Project Properties dialog box. Selecting this check box indicates that the project is intended to run without user interaction. Unattended projects have no interface elements. Any run time functions like messages that normally result in user interaction are written to an event log.

Note If you are developing with CDO v1.21 there are some special considerations. For more information, please see:

239785 PRB - CDO (1.21) will not run in an NT service with Outlook 97 installed


This article is divided into three primary sections:

  • Method 1: How to write a Visual Basic CDO (1.1, 1.2, 1.21) Application that can be run from the Windows NT Schedule Service.
  • Method 2: How to write a Visual Basic CDO (1.1, 1.2, 1.21) Application that can be run from the Windows NT Schedule Service indirectly using the SU utility from the Windows NT Resource Kit.
  • Method 3: How to write and register as a Service a Visual Basic Application that uses CDO (1.1, 1.2, 1.21).

Method 1

Paste the following code into a new BAS Module. Make sure that the Collaboration Data Objects (1.1, 1.2, 1.21) library is currently referenced by the project. Build the executable and follow the directions in the code comments below:

   Sub Main()
    ' How to write a Visual Basic Active Messaging App that can be run
    ' from the Windows NT Schedule Service.
    ' ----------------------------------------------------------------
    ' To demonstrate this functionality, the Schedule Service MUST be
    ' run in the context of a USER ACCOUNT (not the system account.)
    ' To set the Schedule Service Context:
    '   - From the Control Panel select the Services icon
    '   - Scroll down and single click the Schedule Service
    '   - Single click the Startup button
    '   - Select "This Account", and enter YOURDOMAIN\YOURACCOUNT
    '   - It is also suggested that Startup be Manual
    '
    ' With this done, choose the Start button (you should see a dialog box
    ' indicating success). Close the Control Panel.
    '
    ' Now you need to schedule the application to execute. To do this:
    '   - From the Start Menu choose Run
    '   - At the prompt, enter "AT HH:MM PATH\MYEXE.EXE" where
    '   * HH:MM is the time the application is to execute. This is a 24
    '   hour clock, and time is based on the system clock.
    '   * PATH\MYEXE.EXE is path and filename of your EXE.
    '
    SendScheduledMsg
   End Sub

   Sub SendScheduledMsg()
    'Setup variables for use
    Dim objSession As MAPI.Session
    Dim objMsg As Message
    Dim objRecip As Recipient

    'Create Session and Logon
    Set objSession = CreateObject("mapi.session")
    objSession.Logon _
          nomail:=True, _
          profileinfo:="YOURSERVER" & vbLf & "YOURMAILBOX"

    'Create new Message and set properties.
    Set objMsg = _
     objSession.Outbox.Messages.Add( _
       Subject:="This is a Message Sent From a Scheduled Service", _
       Text:="This is a test")

    'Add a Recipient.
     Set objRecip = objMsg.Recipients.Add(Name:="YourRecipsEmailName")
     objRecip.Resolve

    'Send the Message and Logoff.
    objMsg.Send
    objSession.Logoff

    'Cleanup
    Set objRecip = Nothing
    Set objMsg = Nothing
    Set objSession = Nothing
   End Sub
                

Method 2

Paste the following code into a new BAS Module. Make sure that the Collaboration Data Objects (1.1, 1.2, 1.21) library is currently referenced by the project. Build the executable. Follow the directions in the code comments below:

   Sub Main()
    'How to write a Visual Basic CDO (1.1, 1.2, 1.21) Application that
    'can be run from the Windows NT Schedule Service indirectly using
    'the SU utility from the Windows NT Resource Kit.
    '----------------------------------------------------------------
    'To demonstrate this functionality, the Schedule Service can be
    'run in the context of the SYSTEM or USER ACCOUNT. However, while
    'using this option the context will most likely be system.
    'To set the Schedule Service Context:
    '   - From the Control Panel select the Services icon
    '   - Scroll down and single click the Schedule Service
    '   - Single click the Startup button
    '   - Under Log On As, select "System Account."
    '   - It is also suggested that Startup be Manual, and that
    '     service interaction with the Desktop be disallowed.
    '
    'With this done choose the Start button (you should see a dialog box
    'indicating success). Close Control Panel.
    '
    'Now you need to schedule the SU utility to execute. To do this:
    '   - From the Start Menu choose Run
    '   - At the prompt, enter "AT HH:MM PATH\MYBAT.BAT" where
    '     * HH:MM is the time the application is to execute. This is a 24
    '     hour clock, and time is based on the system clock.
    '     * PATH\MYBAT.BAT is the path and filename of a batch file that
    '     sets values of system variables needed by SU, then launches
    '     SU itself. The batch file needs to contain the following
    '     information:
    '
    '        set SU_COMMANDLINE=MyVBexe.exe
    '        set SU_DOMAIN=MyDomain
    '        set SU_PASSWORD=MyDomainPassword
    '        su.exe MyDomainAcctName
    '
    'Please note that this is only one way to start SU. The Su.txt file
    'that accompanies Su.exe documents several alternate methods of
    'launching the SU utility. Please see Su.txt for additional
    'information.
    '
    SendScheduledMsg
   End Sub

   Sub SendScheduledMsg()
    'Setup variables for use.
    Dim objSession As MAPI.Session
    Dim objMsg As Message
    Dim objRecip As Recipient

   'Create Session and Logon
    Set objSession = CreateObject("mapi.session")

   'The following Logon designates a specific ProfileName. The
   'Microsoft Knowledge Base article that describes how to search
   'the HKEY_CURENT_USER hive (which was loaded by SU) of the system
   'registry for the default profile in case a specific profile name
   'is either unknown of intentionally not designated is listed in the
   'REFERENCES section.
   '

   '
   objSession.Logon _
     profileName:="ValidProfileNameForUserAccountNamedInBatchFile", _
     nomail:=True

   'Create new Message and set properties
   Set objMsg = _
   objSession.Outbox.Messages.Add( _
     Subject:="This is a Message Sent From a Scheduled Service", _
     Text:="This is a test")

   'Add a Recipient
   Set objRecip = objMsg.Recipients.Add(Name:="YourRecipsEmailName")
   objRecip.Resolve

   'Send the Message and Logoff
   objMsg.Send
   objSession.Logoff

   'Cleanup
    Set objRecip = Nothing
    Set objMsg = Nothing
    Set objSession = Nothing
   End Sub
                

Method 3

How to write and register as a Service a Visual Basic Application that uses Collaboration Data Objects (1.1, 1.2, 1.21). The author prefaces this section with a precautionary warning that this method is not supported and is not typically programmatically sound. This process, using MAPI functionality in the example, is documented in the following article in the Microsoft Knowledge Base:

175948 Running Visual Basic applications as Windows NT services


REFERENCES

For information on where to acquire the most recent version of this library tested for client-side use, please see the following article in the Microsoft Knowledge Base:

171440 Where to acquire the CDO (1.x) libraries


For additional information about Collaboration Data Objects (1.x) versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

171422 How to use CDO (1.x) to log on to a MAPI session by using the default profile of the current user



Additional query words: Active Messaging

Keywords: kbhowto kbmsg kbcode KB177851