Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 10th, 2007, 01:08 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default make outlook.exe process close properly

hello,
I have some VB code that automatically composes an send an e-mail via Outlook 2002 and I've been having the problem of after the VB code executable has finished running the Outlook.exe process is left running in the background, which I can see when I look in the Processes tab of Windows Task Manager.
What changes do I have to do to the code to make the Outlook.exe process properly close after the e-mail has been sent and the VB code has finished executing?
thanks in advance,
david
------------
Code:
Dim OutApp As Object
    Dim OutMail As Object
    Dim SigString As String
    Dim Signature As String

    Set OutApp = CreateObject("Outlook.Application")
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)

    'figure out how to randomize the number to go between 2 and whatever # I'm up to'
    SigString = "C:\Documents and Settings\Admin\Application Data\Microsoft\Signatures\dgr2.htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
       Signature = "where's the signature?"
    End If

    On Error Resume Next
    With OutMail
        .To = "m.Min@RorSncis.com"
        .CC = "ra@critdjume.com"
        .BCC = ""
        .Subject = "CABCALLS & CABLETTERS record counts for " & Format(Date, "mm/dd/yy")
        .HTMLBody = "Hello Pam:<br><br>" & _
                "For " & Format(Date, "mm/dd/yy") & ":&nbsp;#160;&nbsp;#160;&nbsp;#160;&nbsp;#160;&nbsp;#160;# of Records<br>" & _
                "CABLETTERS.TXT&nbsp;#160;&nbsp;#160;" & CABLETTERSlineCount & "<br>" & _
                "CABCALLS.TXT&nbsp;#160;&nbsp;#160;&nbsp;#160;&nbsp;#160;" & CABCALLSlineCount & "<br><br>" & Signature
        .Display
    End With

    On Error GoTo 0

    TimeDelay (4)
    AppActivate OutMail
    VbSendKeys "^{ENTER}"
    AppActivate OutMail
    VbSendKeys "%{F4}"
    AppActivate OutMail
    VbSendKeys "Y"

    Set OutMail = Nothing
    Set OutApp = Nothing
------------

 
Old April 10th, 2007, 02:25 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Perhaps
Code:
        On Error Resume Next       ' <——<<< This is a bad idea.  You
        With OutMail               '        should trap anticipated 
        .To = "m.Min@RorSncis.com" '        errors, issuing any ap- 
        .CC = "ra@critdjume.com"   '        propriate Resume Next
        .BCC = ""                  '        statements from the error
                                   '        handler.
        .Subject = "CABCALLS & CABLETTERS record counts for " & _
                   Format(Date, "mm/dd/yy")
        .HTMLBody = "Hello Pam:<br><br>" & _
                    "For " & Format(Date, "mm/dd/yy") & _
                    ":&nbsp;#160;&nbsp;#160;&nbsp;#160;&nbsp;#160;" & _
                    "&nbsp;#160;# of Records<br>" & _
                    "CABLETTERS.TXT&nbsp;#160;&nbsp;#160;" & _
                    CABLETTERSlineCount & "<br>" & _
                    "CABCALLS.TXT&nbsp;#160;&nbsp;#160;&nbsp;#160;" & _
                    "&nbsp;#160;" & CABCALLSlineCount & "<br><br>" & _
                    Signature
        .Display
    End With

    On Error GoTo 0 ' 

    TimeDelay (4)
    AppActivate OutMail
    VbSendKeys "^{ENTER}"
    AppActivate OutMail
    VbSendKeys "%{F4}"
    AppActivate OutMail
    VbSendKeys "Y"

    OutApp.Quit   ' I believe this is the one that will do the trick.
    OutMail.Exit  ' These others might be what you need though.
    OutMail.Close ' It’ll be one of these 3.

    Set OutMail = Nothing
    Set OutApp = Nothing
 
Old April 18th, 2007, 02:11 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

modifying the last few lines of the code like this worked well for me....thank you
Code:
   VbSendKeys "Y"
    TimeDelay (4)
    Set OutMail = Nothing
    OutApp.Quit
    TimeDelay (4)
    Set OutApp = Nothing
 
Old April 18th, 2007, 03:51 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Good. Glad I was helpful.

See the following:
Code:
    VbSendKeys "Y"
    TimeDelay (4)          ' VB interprests parens as indicating that
    Set OutMail = Nothing  ' there will be a return value.  Don’t use 
    OutApp.Quit            ' parens for arguments to Subs.
    TimeDelay 4            ' <————<<<  This line does nothing for you.
    Set OutApp = Nothing   '           (See explanation, below.)
When you use the key word “Set,” you establish a reference; all that does is give you a way to refer to– and communicate with an object.

The Operating System counts references. If there are any references to a given object, the OS will not destroy that object.

(In the case of a reference to a running application, the running instance is a reference—so to speak.) When all the refernces are gone, and the app is closed, the memory and resources it used are released.

When a reference variable that refers to an application is set to nothing, all you do is lose the ability to communicate with the app through that variable. You need not wait for the app to close to set it to Nothing, and you need not set it to Nothing just because the app has been told to quit.

In your case, setting OutApp to Nothing is the right thing to do, but the time delay between .Quit and setting the OutApp to Nothing does nothing for you but slow your app down.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Kill all winword.exe process before opening Word mckly Pro VB Databases 1 March 6th, 2008 01:34 AM
process could not make a genat the Subscriber SenthilKannan SQL Server 2000 0 June 21st, 2006 09:55 PM
Auto-attach to process '[1988] aspnet_wp.exe' on m mcdeeiis BOOK: ASP.NET Website Programming Problem-Design-Solution 6 April 21st, 2006 10:20 AM
MyConnection.Close does not kill process busher ASP.NET 1.0 and 1.1 Professional 9 January 13th, 2006 06:00 PM
COM out of process server (EXE) instantiated twice sebdejonghe C++ Programming 0 March 31st, 2004 09:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.