none
Outlook 2003 mail window freezs on click of Send button RRS feed

  • Question

  • Hi all,

    i've developed an Outlook 2003 addin with VSTO + VS 2008.

    There is a VB 6 application which creates an instance of Outlook mail, attaches a document to it and shows this to user to send.

    When user clicks "send" button the mail window freezes. It happens on production machines only.

    The VB6 code is as follows:

    Private Sub Command1_Click()
    Dim objOlApp As New Outlook.Application
    Dim objMailItem As Outlook.MailItem
    Dim objAttachments As Attachments
    Dim arrFilesToAttach(1) As String
     
      Set objOlApp = New Outlook.Application
      Set objMailItem = objOlApp.CreateItem(olMailItem)
      Set objAttachments = objMailItem.Attachments
      
      arrFilesToAttach(0) = Text1.Text
     
      For l = 0 To 0
        strTemp = arrFilesToAttach(l)
        If strTemp <> "" Then
          objAttachments.Add arrFilesToAttach(l)
        End If
      Next
       
      objMailItem.Display True
      
      Set objOlApp = Nothing
      Set objMailItem = Nothing
      Set objAttachments = Nothing
    End Sub

    To see if it's my addin is creating an issue. i created a simple outlook addin and added some File IO code to the startup event Inside try...catch block. i disabled previous addin and installed this new sample addin.

    The result was surprising, the sample addin too, was creating the problem. It changes the LoadBehaviour in registry to 2 for the sample addin. The try...catch block is not catching the exception. i've added an handler for Appdomain's unhandledException, but that too, is not getting fired.

    Please help... Thanks in Advance.

     


    Tuesday, April 26, 2011 2:50 PM

Answers

  • Hi Hemant,

     

    Ignore my last post, I don't think it's a timing issue. After I read your response again I realized I hadn't paid close enough attention to the errors you were getting.

     

    I used part of your code and ran it from Word VBA to see how it would open Outlook and I added some code from Outlook's Help for creating a mail item.  It works here and I hope you can adapt it for your needs.

       

     Dim objOlApp As Outlook.Application

    '    Dim objMailItem As Outlook.MailItem

    '    Dim objAttachments As Attachments

    '    Dim arrFilesToAttach(1) As String

     

        On Error Resume Next

     

        Set objOlApp = GetObject(, "OUTLOOK.Application")

        If Err.Number <> 0 Then

            Set objOlApp = CreateObject("OUTLOOK.Application")

            objOlApp.Visible = True

        End If

        Dim objMailItem As Outlook.MailItem

        'Create e-mail item

        Set objMailItem = objOlApp.CreateItem(olMailItem)

        With objMailItem

            'Set body format to HTML

            .BodyFormat = olFormatHTML

            .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY> Please enter the message text here. </BODY></HTML>"

            .Display

        End With


    Regards, Rich
    Wednesday, April 27, 2011 6:29 PM

All replies

  • Hi Hemant,

    It appears that you are trying to create 2 instances of Outlook and that would cause the problem.

    You have a Dim statement creating a New instance and there is a Set command creating another.

    You really should first check if an instance of Outlook is already running and then perform accordingly by either setting your object to the currently running instance, or create a new instance if it's not already active.

    Hope this helps


    Regards, Rich
    Tuesday, April 26, 2011 7:54 PM
  • Hello Rich,

     

    Thanks a lot for the suggestion it has helped me fix the problem partially. Partially means if Outlook is already running I don't have the issue now, the addin is not getting disabled and the mail window is not freezing on clicking of "Send" button.

    The modified vb6 code i'm using now is like this:

    Private Sub Command1_Click()
    
    On Error Resume Next
    
    'Dim objOlApp As New Outlook.Application
    
    Dim objOlApp 
    
    Dim objMailItem As Outlook.MailItem
    
    Dim objAttachments As Attachments
    
    Dim arrFilesToAttach(1) As String
    
     
    
     'Set objOlApp = New Outlook.Application
    
    
    
     Set objOlApp = GetObject(,”Outlook.Application”)
    
     If objOlApp is nothing then
    
      Err.Clear
    
      Set objOlApp = CreateObject(“Outlook.Application”)
    
     End if
    
    
    
     Set objMailItem = objOlApp.CreateItem(olMailItem)
    
     Set objAttachments = objMailItem.Attachments
    
     
    
     arrFilesToAttach(0) = Text1.Text
    
     
    
     For l = 0 To 0
    
     strTemp = arrFilesToAttach(l)
    
     If strTemp <> "" Then
    
      objAttachments.Add arrFilesToAttach(l)
    
     End If
    
     Next
    
     
    
     objMailItem.Display True
    
     
    
     Set objOlApp = Nothing
    
     Set objMailItem = Nothing
    
     Set objAttachments = Nothing
    
    End Sub
    
    

    But the problem occurs if Outlook is not running on the machine. it gives error on If objOlApp is nothing then statement and terminates the vb6 aplication. If i declare objOlApp as object, the mail window gets opened but issue remains same. i.e. it disables my addin and freezes the mail window on clicking on send.

    If i don't include On Error Resume Next statement, vb6 application throw an exception. It reads like this 'Runtime error 429. ActiveX component can't create object..;

    What am i missing?

    Thanks,

    Hemant


    Wednesday, April 27, 2011 3:18 PM
  • Hi Hemant,

     

    What's probably happening is a timing problem because it’s going to take a second or two for Outlook to load and your code needs to wait until it knows Outlook is up and running.

     

    You can do that a number of ways.  One is to check if Outlook is running and if not load an instance even before entering this subroutine.

     

    Another way would be to put a wait loop into this sub’s code.  It might look something like this:

    Private Sub Command1_Click()

     

    On Error Resume Next

     

    Dim objOlApp

     

    Dim objMailItem As Outlook.MailItem

     

    Dim objAttachments As Attachments

     

    Dim arrFilesToAttach(1) As String

     

     objOlApp = GetObject(, "Outlook.Application")

     

     If objOlApp Is Nothing Then

     

                Err.Clear()

     

                objOlApp = CreateObject("Outlook.Application")

     

     End If

     Call HavePatience()

            'check the error flag and take action

            ‘ your code continues

     

     

        End Sub

     

      Sub HavePatience()

            On Error Resume Next

            Dim StopCount As Integer

            Dim objOlApp

     

            If StopCount > 9 Then

                MsgBox("Sorry, Outlook failed to open.")

                StopCount = 0

                'set an error flag

                Exit Sub

            End If

     

            StopCount = StopCount + 1

     

            objOlApp = GetObject(, "Outlook.Application")

     

            If objOlApp = Nothing Then Application.OnTime( _

                Now + TimeValue("00:00:01"), "HavePatience")

     

    End Sub

     


    Regards, Rich
    Wednesday, April 27, 2011 4:43 PM
  • Hi Hemant,

     

    Ignore my last post, I don't think it's a timing issue. After I read your response again I realized I hadn't paid close enough attention to the errors you were getting.

     

    I used part of your code and ran it from Word VBA to see how it would open Outlook and I added some code from Outlook's Help for creating a mail item.  It works here and I hope you can adapt it for your needs.

       

     Dim objOlApp As Outlook.Application

    '    Dim objMailItem As Outlook.MailItem

    '    Dim objAttachments As Attachments

    '    Dim arrFilesToAttach(1) As String

     

        On Error Resume Next

     

        Set objOlApp = GetObject(, "OUTLOOK.Application")

        If Err.Number <> 0 Then

            Set objOlApp = CreateObject("OUTLOOK.Application")

            objOlApp.Visible = True

        End If

        Dim objMailItem As Outlook.MailItem

        'Create e-mail item

        Set objMailItem = objOlApp.CreateItem(olMailItem)

        With objMailItem

            'Set body format to HTML

            .BodyFormat = olFormatHTML

            .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY> Please enter the message text here. </BODY></HTML>"

            .Display

        End With


    Regards, Rich
    Wednesday, April 27, 2011 6:29 PM
  • Hello Rich,

    Thanks a lot for the hlep. I really reallly appreciate. i've marked your reply as anwer.

    The code still disables my addin though when outlook is not already running. can you think of anything else?

    thanks again for the help.

     

    Thanks,

    Hemant

    Tuesday, May 3, 2011 8:41 AM
  • Hi Hemant,

    Post the code that you currently are trying to use.


    Regards, Rich
    Tuesday, May 3, 2011 5:27 PM
  • hello Rich,

    sorry for the late reply. below is the code i'm using:

    Sub RunOutlook()
      Dim objOlApp As Outlook.Application
      Dim boolNewOutlookApplication As Boolean
      
      On Error Resume Next
      
      Set objOlApp = GetObject(, "OUTLOOK.Application")
      
      If Err.Number <> 0 Then
        Set objOlApp = CreateObject("OUTLOOK.Application")
        'objOlApp.Visible = True
        boolNewOutlookApplication = True
      Else
        boolNewOutlookApplication = False
      End If
      Dim objMailItem As Outlook.MailItem
    
      'Create e-mail item
      Set objMailItem = objOlApp.CreateItem(olMailItem)
      Set objAttachments = objMailItem.Attachments
      With objMailItem
        
        'Attach the attachments
        strTemp = "c:\temp\test.pdf"
        If strTemp <> "" Then
          objAttachments.Add strTemp
        End If
        
        'Set body format to HTML
        .BodyFormat = olFormatHTML
        .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY> Please enter the message text here. </BODY></HTML>"
        .Display
      End With
      Set objMailItem = Nothing
      objOlApp.Quit
      Set objOlApp = Nothing
    End Sub
    

    there are 2 problems when outlook is not running

    1- it disables my Excel addin. (but i'm not worried about it bcz i've a service which enables the disabled addin automatically)

    2- the outlook instance is keep running in background after mail window closes. it just not terminates

    plz help.

    Thanks,

    Hemant

    Wednesday, May 11, 2011 6:35 AM
  • Below is revised code. I added an Option Explicit, revised the code on attaching a file to the email, and added the test of the Boolean value that will quit the Outlook application, only if the function created the instance.

    Hope this helps

     

    Option Explicit

    Sub RunOutlook()

          Dim objOlApp As Outlook.Application

          Dim boolNewOutlookApplication As Boolean

          Dim strTemp As String

         

          On Error Resume Next

         

          Set objOlApp = GetObject(, "OUTLOOK.Application")

         

          If Err.Number <> 0 Then

            Set objOlApp = CreateObject("OUTLOOK.Application")

            'objOlApp.Visible = True

            boolNewOutlookApplication = True

          Else

            boolNewOutlookApplication = False

          End If

        Dim objMailItem As Outlook.MailItem

       

        'Create e-mail item

        Set objMailItem = objOlApp.CreateItem(olMailItem)

        With objMailItem

         

          'Attach the attachments

          strTemp = "c:\BU of Document2.docx"

          If strTemp <> "" Then

            objMailItem.Attachments.Add strTemp

          End If

         

          'Set body format to HTML

          .BodyFormat = olFormatHTML

          .HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY> Please enter the message text here. </BODY></HTML>"

          .Display

        End With

    ‘Insert code to send the email

     

        Set objMailItem = Nothing

       

        If boolNewOutlookApplication = True Then

          objOlApp.Quit

          Set objOlApp = Nothing

        End If

     

    End Sub


    Regards, Rich
    Wednesday, May 11, 2011 11:05 AM