+ Reply to Thread
Results 1 to 6 of 6

VBA not closing excel (visible in the task manager)

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    VBA not closing excel (visible in the task manager)

    Hi All,

    the following code does some simple stuff in outlook. When i run the code once, everything goes as planned.
    Upon its second run, it comes up with various errors. I've looked what the difference is between the first and the next runs and it seems that excel isn't closed (its still visible in the task manager). as soon as i close it manually (task manager), i can run the code once again - but only once since excel will still end up in the task manager.

    Thanks for any guidance!

    Sub test3()
    
    Dim objInsp As Inspector
    Dim objOl As Outlook.Application
    Dim objSel As Outlook.Folder
    Dim objItem As Object
    Dim intMaxItems As Integer
    Dim x As Inspector
    Dim sText As String
    Dim obMail As Outlook.MailItem
    Dim xlApp As Excel.Application
    Dim wb As Excel.Workbook
    Dim wk As Excel.Worksheet
    
    Set objOl = Application
    Set objSel = objOl.ActiveExplorer.CurrentFolder
    
    Set xlApp = New Excel.Application
    Set wb = xlApp.Workbooks.Open("C:\Users\xxx\Desktop\Schichtwunsche.xlsx")
    
    For Each objItem In objSel.Items
        If objItem.Class = olMail And objItem.Subject = "Schichtwunsch" Then
            Set x = objItem.GetInspector
                    'Debug.Print x.ModifiedFormPages(1).item("lblName").Caption
                    'Debug.Print x.ModifiedFormPages(1).item("lblTMName").Caption
                    'Debug.Print x.ModifiedFormPages(1).item("OlkDateControl1").Value
                    wb.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Value = objItem.SenderEmailAddress
                    wb.Sheets(1).Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = objItem.SentOn
                    wb.Sheets(1).Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Value = x.ModifiedFormPages(1).item("OlkDateControl1").Value
                    wb.Sheets(1).Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Value = x.ModifiedFormPages(1).item("txtTMName").Value
            objItem.UnRead = False
        End If
    Next objItem
    
    
    wb.Close savechanges:=True
    Set wb = Nothing
    xlApp.Workbooks.Close
    xlApp.UserControl = False
    xlApp.Quit
    Set xlApp = Nothing
    Set objOl = Nothing
    Set objSel = Nothing
    Set objItem = Nothing
    
    End Sub
    If you think that my answer was helpful, please click on the "Add to this user's Reputation" button.

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA not closing excel (visible in the task manager)

    Hi Bishonen,

    I noticed two things.

    1. there is no error handling which may prevent the xlApp.Quit statement from being executed
    2. I am not familiar with xlApp.UserControl (frequently use the Excel app instance in memory trick - never used this method)

    I would suggest to add error trapping

    Sub test3()
    On Error Goto ErrorTrapping
    
    ...your code
    
    RoutineExit:
      On Error Resume Next
      wb.Close savechanges:=True
      Set wb = Nothing
      xlApp.Workbooks.Close
      xlApp.UserControl = False
      xlApp.Quit
      Set xlApp = Nothing
      Set objOl = Nothing
      Set objSel = Nothing
      Set objItem = Nothing
      Exit Sub
    
    ErrorTrapping:
      Resume RoutineExit
    
    End Sub
    If you like my contribution click the star icon!

  3. #3
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: VBA not closing excel (visible in the task manager)

    Thanks a lot for your reply. Unfortunately that still does not get rid of the excel instance ;/ I'm fighting with this problem since ~1.5h googling my way through dozens of forums and I still didn't find a solution that would work...

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA not closing excel (visible in the task manager)

    Quote Originally Posted by Bishonen View Post
    Thanks a lot for your reply. Unfortunately that still does not get rid of the excel instance ;/ I'm fighting with this problem since ~1.5h googling my way through dozens of forums and I still didn't find a solution that would work...
    Have you tried removing statement xlApp.UserControl=False?

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: VBA not closing excel (visible in the task manager)

    When you automate a different application you must properly qualify all of its Objects - you have not done so when you refer to Rows.Count. You can use:
    Sub test3()
    
       Dim objInsp                     As Inspector
       Dim objOl                       As Outlook.Application
       Dim objSel                      As Outlook.Folder
       Dim objItem                     As Object
       Dim intMaxItems                 As Integer
       Dim x                           As Inspector
       Dim sText                       As String
       Dim obMail                      As Outlook.MailItem
       Dim xlApp                       As Excel.Application
       Dim wb                          As Excel.Workbook
       Dim wk                          As Excel.Worksheet
       Dim lRows                       As Long
    
       Set objOl = Application
       Set objSel = objOl.ActiveExplorer.CurrentFolder
    
       Set xlApp = New Excel.Application
       Set wb = xlApp.Workbooks.Open("C:\Users\xxx\Desktop\Schichtwunsche.xlsx")
       lRows = wb.Sheets(1).Rows.Count
    
       For Each objItem In objSel.Items
          If objItem.Class = olMail And objItem.Subject = "Schichtwunsch" Then
             Set x = objItem.GetInspector
             'Debug.Print x.ModifiedFormPages(1).item("lblName").Caption
             'Debug.Print x.ModifiedFormPages(1).item("lblTMName").Caption
             'Debug.Print x.ModifiedFormPages(1).item("OlkDateControl1").Value
             wb.Sheets(1).Cells(lRows, 1).End(xlUp).Offset(1, 0).Value = objItem.SenderEmailAddress
             wb.Sheets(1).Cells(lRows, 2).End(xlUp).Offset(1, 0).Value = objItem.SentOn
             wb.Sheets(1).Cells(lRows, 3).End(xlUp).Offset(1, 0).Value = x.ModifiedFormPages(1).Item("OlkDateControl1").Value
             wb.Sheets(1).Cells(lRows, 4).End(xlUp).Offset(1, 0).Value = x.ModifiedFormPages(1).Item("txtTMName").Value
             objItem.UnRead = False
          End If
       Next objItem
    
    
       wb.Close savechanges:=True
       Set wb = Nothing
       xlApp.Workbooks.Close
       xlApp.UserControl = False
       xlApp.Quit
       Set xlApp = Nothing
       Set objOl = Nothing
       Set objSel = Nothing
       Set objItem = Nothing
    
    End Sub

  6. #6
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA not closing excel (visible in the task manager)

    Else make the xlApp visible and step through your code using debug to see where it goes wrong.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Totally Closing Out of Excel (EXCEL.EXE left in task manager)
    By mnh2374 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-26-2012, 11:46 AM
  2. Excel Instance not closing in task manager.
    By MattShoreson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-01-2009, 11:26 AM
  3. [SOLVED] excel 2003 Resides in Task manager.
    By Pandi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-09-2006, 10:50 AM
  4. [SOLVED] excel.exe is not terminating from task manager
    By Inthi in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-28-2005, 09:45 AM
  5. Task Manager and Excel 2003
    By Mike in forum Excel General
    Replies: 1
    Last Post: 04-20-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1