Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Alberto Schiappa
  Where is Alberto Schiappa?
 Brescia
 Italy
 Alberto Schiappa
 Tags
Subject: Excel Automation Events
Thread ID: 251838 Message ID: 251838 # Views: 29 # Ratings: 0
Version: Visual FoxPro 9 SP2 Category: Microsoft Office Automation
Date: Sunday, January 31, 2010 5:46:08 PM         
   


Hi, I'm learning how to do automation tasks with Excel .

In the MSDN
http://msdn.microsoft.com/it-it/library/microsoft.office.tools.excel.workbook.shutdown%28VS.80%29.aspx
there are lot's of events regarding WorkBooks .


I would like to understand ho to use (adding my code) one of those.


There's an interesting article about OLE Automation
http://www.dfpug.de/loseblattsammlung/migration/whitepapers/foxole.htm
from where I've copied and pasted some code with little modification :

o=CREATEOBJECT("myExcel")
*o.visible=.T. && If I decomment appears an error
*o.Quit()
o=.null.

DEFINE CLASS myExcel AS  OLEApplication 
cOLERegName = "Excel.Application"
 
	FUNCTION Destroy()
	MESSAGEBOX("Chiudo") && some my code here
	IF TYPE("this.oOLEApp") == "O" AND this.lCloseAppWhenDone
		this.oOLEApp.Quit()
	ENDIF
	ENDFUNC
ENDDEFINE


DEFINE CLASS OLEApplication AS Custom

	PROTECTED oOLEApp, cOLERegName, lCloseAppWhenDone
	oOLEApp = ""
	cOLERegName = ""
	lCloseAppWhenDone = .T.

	FUNCTION Init()
		IF EMPTY(this.cOLERegName)
		=MESSAGEBOX("Cannot create object directly from class OLEApplication", MB_ICONSTOP, "")
		RETURN .F.
		ENDIF

		IF this.AppRunning()
			this.oOLEApp = this.GetCurrentInstance()
		ELSE
			this.oOLEApp = this.CreateNewInstance()
		ENDIF
	ENDFUNC

	PROTECTED FUNCTION CreateNewInstance()
		RETURN CREATEOBJECT(this.cOLERegName)
	ENDFUNC

	PROTECTED FUNCTION GetCurrentInstance()
		RETURN GETOBJECT(, this.cOLERegName)
	ENDFUNC

	PROTECTED FUNCTION AppRunning()
		*— Returns .T. if app is already running
		LOCAL lcOldError, llRunning
		llRunning = .T.
		lcOldError = ON("ERROR")
		ON ERROR llRunning = .F.
		=GETOBJECT(, this.cOLERegName) &&Attempt to get a reference to a running application
		ON ERROR &lcOldError
		this.lCloseAppWhenDone = !llRunning
		RETURN llRunning
	ENDFUNC
ENDDEFINE


If I run the code and look the list of processes in the task manager I see that
before the click on the MessageBox it show that Excel is working, and after the click it disappear
so it runs like I expect.

But if I decomment the line *o.visible=.T. I get the error "Property VISIBLE is not found"

What I am missing ?

Thank's in advance for help me understand this

Regards

Alberto



COMPLETE THREAD
Excel Automation Events Posted by Alberto Schiappa @ 1/31/2010 5:46:08 PM
RE: Excel Automation Events Posted by Borislav Borissov @ 1/31/2010 6:05:18 PM
RE: Excel Automation Events Posted by Alberto Schiappa @ 1/31/2010 6:28:31 PM
RE: Excel Automation Events Posted by Martin Krivka @ 1/31/2010 6:07:44 PM
RE: Excel Automation Events Posted by Alberto Schiappa @ 1/31/2010 6:35:13 PM
RE: Excel Automation Events Posted by Borislav Borissov @ 1/31/2010 6:54:14 PM
RE: Excel Automation Events Posted by Alberto Schiappa @ 1/31/2010 7:11:12 PM