0

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 
7
  • 1
    You are having Excel create a new instance of itself? Why are you doing that? Commented Oct 27, 2017 at 17:40
  • Have you ever tried out an Add-On? (XLAM file) It seems like that would be a more logical approach. Commented Oct 27, 2017 at 17:46
  • @brax This excel macro will be use by several people in a network enviroment and we were having issues with read only documents etc. We don't want to use an addon, we want to use a shortcut on everybody's computers. Commented Oct 27, 2017 at 18:09
  • 1
    Why is the Set objExcel = Nothing after your End Sub? Might want to try GetObject instead of CreateObject. Commented Oct 27, 2017 at 18:11
  • 1
    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. Commented Oct 27, 2017 at 18:18

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Also you can't Set objExcel = Nothing before objExcel.Application.Quit... you'll get an error...
Stubbornly holding on to a flawed architecture is going to crumble, one day or another. Take the time to heed the advice you were given, and do things right. Code that "works" isn't necessarily good code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.