How to close embedded workbook properly

Y

Yulia Gillerson

Hello!

1. I create new MS Word 2003 document.
2. Add reference to Excel.
3. Insert embedded Excel object.
4. Run the following code:

Function OpenAndCloseExcel()

Dim objWorkBook As excel.Workbook
Dim objExcel As excel.Application

ActiveDocument.InlineShapes(1).OLEFormat.DoVerb
VerbIndex:=wdOLEVerbPrimary

Set objWorkBook = ActiveDocument.InlineShapes(1).OLEFormat.Object
Set objExcel = objWorkBook.Application

objWorkBook.Close
objExcel.Quit

End Function

Function fails - error 1004, method 'close' of object '_workbook' failed.

5. I run the same function again, Excel objext already activated on step 4.
Function does not fails, Excel removed from task manager.

Could you, please, describe
1. Why function fails on step 4?
2. How to close embedded workbook & excel application properly?

Thank you very much in advance.
 
M

macropod

Hi Yulia,

Provided your activated object will be the one processing keyboard commands,
you can use SendKeys to deactivate the object. For example:
SendKeys "{esc}", Wait:= True

However, if you have a UserForm open while you're activating objects, the
UserForm will trap the keyboard events and this approach won't work.

Cheers
 
Y

Yulia Gillerson

macropod,
Provided your activated object will be the one processing keyboard commands,
you can use SendKeys to deactivate the object. For example:
SendKeys "{esc}", Wait:= True
Thank you for your answer. I use this method, but Word often hangs until
I'll press Ctrl-Alt-Del and open task manager :(.

I hope, that exists more stable and simple method.
 

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