How do I modify this VBA to close Outlook?

Public Sub OutlookStart()
    On Error Resume Next
 
    Dim objOL As Object
   
    'check if Outlook is running
    Set objOL = GetObject(, "Outlook.Application")
   
    'on error start Outlook
    If Err.Number <> 0 Then                                          '
        Err.Clear
        '**Open Outlook and select profile
        Shell "C:\Program Files\Microsoft Office\Office12\outlook.exe" & " /profile leanretail", vbMinimizedFocus

        'Shell "C:\Program Files\Microsoft Office\Office12\outlook.exe", vbMinimizedFocus
        DoEvents
    End If
 
    Set objOL = Nothing

End Sub


Thanks
kosenrufuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieVBA ExpertCommented:
If Outlook is already open and GetObject has been succesful you can use
objOL.Quit

Open in new window

If it's been opened using Shell then you'll need something else.

0
kosenrufuAuthor Commented:
It is opened using Shell

Shell "C:\Program Files\Microsoft Office\Office12\outlook.exe" & " /profile leanretail", vbMinimizedFocus
0
NorieVBA ExpertCommented:
I see that.

What are you doing with Outlook?

Is this code just meant to open it and nothing else?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

NorieVBA ExpertCommented:
If you have opened Outlook using Shell you can use something like this to close it.
Option Explicit


Private Declare Function FindWindow _
                          Lib "user32" _
                              Alias "FindWindowA" ( _
                              ByVal lpClassName As String, _
                              ByVal lpWindowName As String) _
                              As Long


Private Declare Function PostMessage _
                          Lib "user32" _
                              Alias "PostMessageA" ( _
                              ByVal hWnd As Long, _
                              ByVal wMsg As Long, _
                              ByVal wParam As Long, _
                              lParam As Any) _
                              As Long

Private Const WM_CLOSE = &H10

Sub CloseApp()
Dim AppHandle As Long
Dim strWindow As String

    ' Windows caption of application you want to close
    strWindow = "Inbox - outlook - Microsoft Outlook"

    AppHandle = FindWindow(vbNullString, strWindow)
    PostMessage AppHandle, WM_CLOSE, 0&, 0&

End Sub

Open in new window

Obviously change strWindow to reflect the caption you have.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kosenrufuAuthor Commented:
imnorie:

I tried using your code but it did not work
0
NorieVBA ExpertCommented:
Did you change the caption?

You wouldn't need this code if you opened Outlook with CreateObject instead of Shell.

If you did that you could close it with the first code I posted.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
As imnorie said:

What are you doing with Outlook after you open it with your Shell command?
0
kosenrufuAuthor Commented:
I am sending email using VBA code
0
NorieVBA ExpertCommented:
If that's what you are doing why are you using Shell?

Opening Outlook with Shell doesn't give you any control in your code to do anything with it.

Try using CreateObject("Outlook.Application") instead.

  On Error Resume Next
 
    Dim objOL As Object
    
    'check if Outlook is running
    Set objOL = GetObject(, "Outlook.Application")
    
    'on error start Outlook
    If Err.Number <> 0 Then                                          '
        Err.Clear
        Set objOL = CreateObject("Outlook.Application")    
    End If
 
    ' use code to create and send your email with Outlook

   ' close Outlook

    objOL.Quit
    Set objOL = Nothing

Open in new window

0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
0
kosenrufuAuthor Commented:
I ma using Outlook profile. How would I select a profile with VBA?

    Shell "C:\Program Files\Microsoft Office\Office12\outlook.exe" & " /profile leanretail", vbMinimizedFocus

0
NorieVBA ExpertCommented:
What are you using the profile for?
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
And again - why are you using the Shell command to open Outlook if you're just sending emails? Does the user need to interact with OUtlook in some way OTHER than sending an email? If you use SendObject, and you set the last parameter to True (see the link above), then Access will open your email message, and the user can interact with it.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.