Closing excel from a macro

G

Guest

I am currently opening excel from a java application. I use the excel object
(this workbook) to run several macros. The java application opens excel, so
every time excel is opened, some code must be run and then excel must be
close. The code I am trying to use is:

Private Sub Workbook_Open()
Macro1
tbmfile = "C:\excelfile.xls"
tbmxcelfile = "excelfile"
Windows(tbmxcelfile).Activate
ActiveWorkbook.Save
Application.Quit

If I step through this code it works fine and closes excel. But when I open
excelfile.xls from my documents, the macros are triggered to run, but excel
does not close. Why is that?
 
T

Tom Ogilvy

Your java application probably still has a reference to Excel which needs to
be released before excel can close.

I would also change

tbmxcelfile = "excelfile"

to
tbmxcelfile = "excelfile.xls"

if that is the name of the file.
 
G

Guest

Dear Tom

Thank you for the rapid response. The problem is not with the Java
application still maintaining a link. I ran a test for that; the java
application was not running and was closed. The excel sheet containing the
code to be launched on the open command was opened manually. I also
changed the file name as you have suggested and the problem still persists.
If I step through the code, the Application.Quit command works fine and excel
closes. But if I open excel, and the excelfile.xls then the code runs up
until the ActiveWorkbook.Save command and then does not quit. Why is this
happening?

I thank you for your help in advance.
 
G

Guest

As you say, the code runs fine in one instance, thus there is little to be
said based on looking at the code you have provided. I gave you the most
likely cause and you said that is not a problem.
 
G

Guest

Thank you for the reply. I ran the excel file on a different machine. A
machine which does not have the software installed to run the java
application or the java files which call upon this excefile, so I am mistaken
in saying that there is no chance that a link could still exist? The same
problem persists. I know that this is annoying and I am sorry for bother you
again. I noticed another strange thing; when I open excel and the excel file
the first time the Application.Quit does not work; (a box comes up saying
code execution has been interuppted without me causing the break) even when I
press continue. This break occurs on the Application.Quit command. When i
then close this workbook (and not excel) and open it again the
Application.Quit command works. The things bothering me is why does the code
execution has been interuppted box pop up unprompted and why does the
Application.Quit command only work the second time round? What I would want
to happen is when excel is opened and then the exelfile is open, the macros
must run, the workbook must be save (both of these commands work) and then
excel must close (this does not work).

Your help is greatly appreciated.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top