Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make outlook.exe process close properly

Status
Not open for further replies.

dgr7

IS-IT--Management
Dec 29, 2006
43
US
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
------------
 
Why are you activating Outlook with AppActivate? Here is a version of code that I use that has never failed for me. It is geared for Outlook 2000, but I've also used it in 2003.

Normally, Outlook is always open on my PCs. If not, the e-mail simply goes to the Outbox and waits for it to be activated. Is this why?

Anyway:

Code:
Public Sub SendEMail()

    Dim strIniPath, strLine As String
    Dim FSO, txtFile1
        
    Set myOlApp = CreateObject("Outlook.Application")
    Set myitem = myOlApp.CreateItem(olMailItem)
        myitem.Body = vbNewLine & vbNewLine & _
            vbNewLine & vbNewLine & "Here are your files for " & eDate
    Set myAttachments = myitem.Attachments

'''Send the attachment
    myAttachments.Add App.Path & "\history\" & FName & ".xls", _
        olByValue, 1, FName
    If DOW = 2 Then
        myitem.Subject = "Weekly Journal 4"
    Else
        myitem.Subject = "Daily Journal 4"
    End If
    
    
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    '''Get the e-mail addresses in a text file
    strIniPath = App.Path & "\Address.txt"
    If FSO.fileexists(strIniPath) Then
        Set txtFile1 = FSO.OpenTextFile(strIniPath, 1, False)
        On Error GoTo ExitErr
        
        Do While Not txtFile1.AtEndOfStream
        
            strLine = txtFile1.readline
            myitem.Recipients.Add (strLine)
            
        Loop
        
    End If
ExitErr:
    txtFile1.Close
    myitem.Display ''Or myItem.Send for automation
        
End Sub

I hope this helps.


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top