2

I'm having an VB6 application that opens an excel macro enabled file using workbooks.open method. Inside this file, there is an userform that gonna be automatically opened when the file is opened. The problem is the workbooks.open method in vb6 application keeps running and does not jump to next line even when I close my userform (I do have codes to close workbook, quit excel app when the userform is closed). I can only close the interface of my userform but Excel still running in background. As long as I dont close the Excel app in task manager, my vb6 does not terminate excel process and of course it will not jump to next line either.

Here is my code in vb6 to open excel file that contains the userform above

As I said, workbooks.open method works but it does not terminate and keeps the excel app runnning in background

P/s: don't mind the Japanese characters

Private Sub Form_Load()
    Dim xlapp
    Dim xlwkb
    
    If Not App.PrevInstance Then
        Set xlapp = CreateObject("excel.application")
        Me.Hide
        On Error Resume Next
        Set xlwkb = xlapp.workbooks.open("C:\Users\david\Desktop\sale-system\fixedfile.xlsm", Readonly:= True)
        
        Set xlwkb = Nothing
        
        If Not xlapp Is Nothing Then
            xlapp.Visible = True
            xlapp.Application.quit
            Set xlapp = Nothing
        End If
        Unload Me
    Else
        MsgBox "プログラムが既に起動されています!", vbInformation, "売上管理システム"
        Unload Me
    End If
    
End Sub

Here is my code in excel file when close the userform

Application.DisplayAlerts = False
ThisWorkbook.Close False
 Application.Visible = True
 Application.Quit
 End

I really need to solve this problem in this week, any solution? Thank you guys in advance and please pardon for my english

Here is some images. I hope they will help you guys to understand my problem

This happens when I use Thisworkbook.close False statement: enter image description here

This happens when I dont use Thisworkbook.close False statement: enter image description here

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • Instead of `CreateObject("Excel.Application`)` use `GetObject("C:\Users\david\Desktop\sale-system\fixedfile.xlsm")`. For some compatibility reason Excel Application Object doesn't follow Automation rules but it's Document object (called a Workbook in Excel) does. As per Automation rules once the reference count goes to zero the object unloads, but not Excel's Application object. Word works correctly. You should never need to app.quit. It should be automatic, but not Excel. Reason to hate Excel Number One. – Lundt Dec 15 '22 at 03:42
  • References: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/automat/using-standard-objects. ---- Reason to hate Excel Number Two - it can't use the Windows' clipboard properly either. Word can. – Lundt Dec 15 '22 at 03:51
  • Thanks for your comment but it doesnt work as I expect. I was trying set wb= GetObject(~) as you said, so the code jumped to next line but the excel app is invisible and it still keeps running in background even when i try wb.visible=true – Duong Sansen Dec 15 '22 at 04:33
  • If it is already running it will stay running. – Lundt Dec 15 '22 at 07:06

1 Answers1

3

Inside this file, there is an userform that gonna be automatically opened when the file is opened.

when I close my userform (I do have codes to close workbook, quit excel app when the userform is closed).

You are doing this incorrectly. Do not close/quit it from the UserForm. Do it from VB6 form. This way, vb6 will be able to handle and clear the objects.

Here, try this (I already tried it and it works...). This will not leave an instance of Excel running. I have commented the code. But if you still have questions then simply ask.

Note: Before you try this (Just for testing purpose), close all Excel application. Even from the task manager.

Private Sub Form_Load()
    Dim oXLApp As Object
    Dim oXLWb As Object
    Dim ICreateatedExcel As Boolean
    
    '~~> Establish an EXCEL application object
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")
    
    '~~> If not found then create new instance
    If Err.Number <> 0 Then
        Set oXLApp = CreateObject("Excel.Application")
        '~~> I created instance of Excel
        ICreateatedExcel = True
    End If
    Err.Clear
    On Error GoTo 0
    
    '~~> Check if you have an instance of Excel
    If oXLApp Is Nothing Then
        MsgBox "Unable to get an instance of Excel.", vbCritical, "Excel is Installed?"
        Exit Sub
    End If
    
    Me.Hide
    
    '~~> Show Excel
    oXLApp.Visible = True
    
    '~~> Open file
    Set oXLWb = oXLApp.Workbooks.Open("C:\Tester.xlsm")

    '~~> This and other lines below will not run till the
    '~~> time you close the userform in Excel

    '~~> Close the workbook
    oXLWb.Close (False) '<~~ Set this to True if you want to save changes
    
    Set oXLWb = Nothing
    
    '~~> If I created Excel then quit
    If ICreateatedExcel = True Then oXLApp.Quit
    
    Set oXLApp = Nothing
    
    Unload Me
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Like I mentioned it works just fine for me. There is some other issue. If you are getting `run-time error 429` then see [THIS](https://support.microsoft.com/en-us/topic/you-receive-run-time-error-429-when-you-automate-office-applications-b6070e15-ea78-4349-2751-72bdb5f186f7) – Siddharth Rout Dec 15 '22 at 06:28
  • i have tried your code, and delete all the app.quit in excel file, but it still doesnt work - This below line does work but still return an error number (429) `Set oXLApp = GetObject(,"Excel.Application")` - This line still not running in both case (Getobject and Createobject) and can not close Excel instance `Set oXLWb = oXLApp.Workbooks.Open("C:\Users\david\Desktop\sale-system\fixedfile.xlsm")` - When I try to close the excel instance in task manager, vb6 got `-2147023170(800706be) Automation error. Remote processure has failed` What should I do? – Duong Sansen Dec 15 '22 at 06:46
  • Did you check the link that I gave above? Also there can be many reasons **1.** Excel is corrupt **2.** You are working in a corporate environment where createobject(working withactivex objects) is disabled. **3.** Other other reasons. There is no problem with the code. – Siddharth Rout Dec 15 '22 at 06:49
  • okay, I think I figured out what's happening. The code that you gave me works fine with no problem. But I wanted no hide excel window so that my users can only see the userform, but if I hide excel application then it cause exactly the same error ( can not close excel instance) Is there anyway to hide excel app but also can close the instance? – Duong Sansen Dec 15 '22 at 08:09
  • Yes becuase you are opening the form in modal. Change the way you are showing the userform in the workbook open event. For example Currently you are using say `Userform1.show`. Instead of this use `Userform1.show vbModeless` – Siddharth Rout Dec 15 '22 at 08:29
  • i've just read the dicussion that you gave me. I dont see any check mark on this page. Is there anything else that I have to archieve to have check marks? – Duong Sansen Dec 16 '22 at 04:53