0
votes

We have tried to fix these problem for a couple of days and we posted on another forums but still no answer, may be you guys can help us here.

I am having a problem with excel running after the Userform is closed, I think what I have supposed to end the application but it doesn't, it keeps excel running in the background and I have to end the process with task manager. I created a vba file to copy a file to the temp folder and run a macro which opens excel and the userform. Any help is appreciate it.

This is the vba script that copy and opens the macro:

    Dim FSO 
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    FSO.CopyFile "C:\pdfv6.xlsm", "C:\Windows\Temp\" 
    Set objExcel = CreateObject("Excel.Application") 
    objExcel.Application.Run "'C:\Windows\Temp\pdfv6.xlsm'!module1.macro1" 
    objExcel.DisplayAlerts = False 
    Set objExcel = Nothing
    objExcel.Application.Quit 

And this is part of the vba module:

    Sub Macro1() 
    Application.Visible = False 
    UserForm1.Show 
    End Sub
    Set objExcel = Nothing 

And this is the Sub that deals with closing the Userform and the application.

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) 
If CloseMode = 0 Then 
    Unload Me
    ThisWorkbook.Close savechanges = False 
    Application.Quit 
    End 
  End If 
  End Sub 
1
You are having Excel create a new instance of itself? Why are you doing that? - braX
Have you ever tried out an Add-On? (XLAM file) It seems like that would be a more logical approach. - braX
It's unclear how the components are interacting, maybe that has something to do with why you haven't received any answers yet. Please edit to clearify. Also, you need to read this blog post about UserForm.Show (full disclaimer: I own that blog). - Mathieu Guindon
Why is the Set objExcel = Nothing after your End Sub? Might want to try GetObject instead of CreateObject. - mooseman
What @Mitch said. The problem stems from implementing application logic in a form's code-behind, as I explain in the blog post linked above. The form has no business calling Application.Quit, ThisWorkbook.Close, or Ending execution, and Unload Me is bug-prone at best. - Mathieu Guindon

1 Answers

2
votes

I found the problem. I was using Application.Quit twice Once in the vbs script and then when I was closing the form. I got rid of the one in the vbs script and everything is fine now. thank you for your help.