• Capturing the send event (Outlook XP)

    Author
    Topic
    #390730

    Hello, I am new to Outlook programming. I want to display a form when the user presses send in Outlook. What event is that? Is there an AutoSend event?

    Also, how do I refer to the current e-mail in Outlook. Is there an ActiveItem property, as in Word there is the Activedocument property?

    Thanks.

    Viewing 0 reply threads
    Author
    Replies
    • #695185

      The Outlook Application object has an ItemSend event. You can handle it as follows:
      Switch to the Visual Basic Editor (Alt+F11).
      Make sure that the Project Explorer is visible (Ctrl+R).
      Expand Microsoft Outlook Objects.
      Double click ThisOutlookSession.
      Select Application from the Object dropdown list in the module that appears.
      You’ll get the “skeleton” for the ItemSend event procedure:

      Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

      End Sub

      Item is a reference to the mail being sent; you can inspect its Subject property, for instance.
      Cancel is False by default, but you can set it to True to prevent the item from being sent.

      Example (more or less from the online help):

      Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
      If MsgBox(“Are you sure you want to send ” & Item.Subject & “?”, _
      vbYesNo + vbQuestion, “Sample”) = vbNo Then
      Cancel = True
      End If
      End Sub

      The “active item” is ActiveInspector.CurrentItem. Be careful, this will raise an error if no item is open, so you must test if it is Nothing:

      If Not (ActiveInspector.CurrentItem Is Nothing) Then
      MsgBox ActiveInspector.CurrentItem.Subject
      End If

      • #695199

        Hans,

        Many thanks – that worked. Now I need to know how to save and distribute the code.
        The template with the code is saved as
        C:DOCUMENTS and SETTINGSAPPLICATION DATAMICROSOFTOUTLOOKvbaproject.otm.

        Is it possible to save it to another location? with another name? In Word, there is a startup folder, is there something like this in Outlook?

        Thanks.

        • #695201

          It is not really possible to distribute “ordinary” Outlook modules, since Outlook only uses the one VbaProject.otm; you would have to replace an existing file of that name, thereby destroying existing code.

          If you really want to distribute Outlook code, you must write a COM add-in, but that is not really easy. I can’t help you with that, but there are probably other Loungers who can. If you don’t receive replies in the next few days, consider posting a request in the VB / VBA forum, with a reference to this thread.

          See OL2002: Managing and Distributing Outlook VBA Projects and the links provided there.

        • #695392

          Depending on what you want to do with the send event you might be able to put the code in a custom form. Then you distribute the form and don’t have to worry about the project file.

          • #695499

            I have put the code into a custom form, similar to what Hans suggested. But my problem is how to distribute this form to other people. Do you know of another way (Apart from creating a COM Add-in) ,to distribute a custom form in Outlook without overwriting the vbaproject.otm file?

            Also, if I do have to create a COM Add-in, how do I link it to the SEND event in Outlook?

            I am also wondering how to determine what type of item caused the send event. Is there a way to determine if it was a mailitem, or appointmentitem etc?

            Thanks.

            • #695566

              > Also, if I do have to create a COM Add-in, how do I link it to the SEND event in Outlook?

              In the code module for the Designer, you set up something like this:

              Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
                          ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
                          ByVal AddInInst As Object, custom() As Variant)
              
              ' set up event handler for new Inspectors
              Call EventSetup
              End Sub

              In a class module (named, for example, CallOnMe, you have something like this:

              Public WithEvents OLapp As Application
               
              Private Sub OLapp_ItemSend(ByVal Item As Object, Cancel As Boolean)
              'your Send code here
              End Sub

              In a regular code module, you have something like this:

              Dim OLEvents As New CallOnMe
               
              Sub EventSetup
              Set OLEvents.OLapp = Outlook.Application

              In theory, then, when the COM add-in is loaded, Outlook should be all set up to intercept the Send command. But I haven’t tested with the Send event.

            • #695702

              If the code is all in a form then you don’t need to worry about a com add-in or the vba project. You just publish the form in a way that others can get at it. If you’re on Exchange then one good place is the Organizational Forms library. Notes has something comparable. Public Folders might be an option too. If you’re not on one of those, you can do a Save As and send the resulting .OFT file to them. They add it to their personal forms library.

    Viewing 0 reply threads
    Reply To: Capturing the send event (Outlook XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: