PDA

View Full Version : Clsoing an Excel File



tla
11-01-2007, 11:00 AM
I have code that opens an excel file and reads in some data from it then continues on. I want to close the Excel file after reading the data. but the command I used doesn't seem to work:

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.Workbooks.Open myGlossary
myTerm = xlApp.Range("A5:A10").Value

Now I have two lines of code and neither works:
Windows("Test Document.doc").Activate (to activate the word file again)
xlApp.Workbooks(myGlossary).Close (to close the excel file)

The first line is ignored, the 2nd generates an error.

Any ideas?

Norie
11-01-2007, 11:13 AM
How is myGlossary getting a value?

Does that value include the path of the file as well as the filename?

If so that's probably your problem.

It would probably be better to do something like this.

Set wbOpen = xlApp.Workbooks.Open (myGlossary)

You could then use this to close the file.

wbOpen.Close

tla
11-02-2007, 04:14 AM
Hmmm didn't solve my problem. It closes the Excel file, but doesn't reactivate the word window since it doesn't close Excel itself. I suppose I can't have it Close the Excel application as other fiels might be legitimately open. I not sure why I can't get the Word Window to reactivate though.

TonyJollans
11-02-2007, 05:25 AM
You could - and should if you opened it - close the Excel application. If it has been available to the user you should, however, check that there are no other workbooks open first.

That, however, would not necessarily activate the Word application. I have never had a lot of joy activating windows - I think the Activate method activates a document window within Word rather than the Word window within Windows.

Norie
11-02-2007, 05:42 AM
tla

As Tony has said you probably should close the Excel instance.

Since you created it using CreateObject then that shouldn't affect any other open workbooks as they won't be open in that instance.

xlApp.Quit