Using OLE Automation in Visual FoxPro
Using OLE Automation in Visual FoxPro
Quick review
You need to consider only two FoxPro functions to retrieve another application's objects: CreateObject and GetObject. Once you have obtained the object, you can immediately begin to access its properties and methods. CreateObject creates an object from a class definition or an OLE object. It is also used to create objects of built-in FoxPro or user-defined classes: < m e m v a r >=C R E A T E O B J E C T ( c C l a s s N a m e ) cClassName specifies the OLE object from which the new object is created. FoxPro will search for the class name you specify in the following order: 1. Visual FoxPro base classes. 2. User-defined class definitions in memory in the order they were loaded. 3. Classes in the current program. 4. Classes in .VCX class libraries opened with SET CLASSLIB. 5. Classes in procedure files opened with SET PROCEDURE. 6. Classes in the Visual FoxPro program execution chain. 7. The Windows registry (for OLE objects). OLE objects are created using the following syntax for cClassName: A p p l i c a t i o n N a m e . C l a s s For example, to create a Microsoft Excel object using OLE Automation, use the following: x l=C R E A T E O B J E C T ( " E x c e l . A p p l i c a t i o n " ) When this code is run, Microsoft Excel is started but is not visible. If Microsoft Excel is already running, another separate instance is created when you execute this command. GetObject activates an OLE Automation object and creates a reference to the object. For example:
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm 1/8
7/9/13
G E T O B J E C T ( [ c F i l e N a m e[ ,c C l a s s N a m e ] ] ) cFileName specifies the full path and filename to activate. The application does not need to be specified, because the OLE dynamic-link libraries (DLLs) determine which application to start based on the filename provided. For example, the following code launches Microsoft Excel, opens a file named BUDGET.XLS, and creates a reference through an object memory variable named xlBudget : x l B u d g e t=G E T O B J E C T ( " C : \ E X C E L \ W O R K \ B U D G E T . X L S " ) cClassName specifies the class name of the object to retrieve. Some applications can store more than one object type in the same file, allowing you to use the class name to specify the object to activate. For example, if a word-processing application stores its documents, macro definitions, and toolbar objects in the same file, you can create a reference to the document file with the following command: m D o c F i l e=G E T O B J E C T ( " C : \ W R D P R O C \ M Y D O C . D O C " ," W r d P r o c . D o c u m e n t " ) With some server applications, each time a GetObject function is called an additional instance of the application is started, which uses additional memory. For example, if Microsoft Excel is already running, you can prevent additional instances from starting by omitting cFileName and including cClassName, as in this example: x l=G E T O B J E C T ( ," E x c e l . A p p l i c a t i o n " ) If Microsoft Excel is not running, a trappable error will be generated. Trapping the error and resorting to the CreateObject function is a common technique to test if an application is already running. An alternative technique is to use dynamic data exchange (DDE) to establish a link with the server application on the System topic, which all DDE applications support. If the link is successfully established, the application is running.
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm
2/8
7/9/13
D E F I N EC L A S SO L E A p p l i c a t i o nA SC u s t o m P R O T E C T E Do O L E A p p ,; c O L E R e g N a m e ,; l C l o s e A p p W h e n D o n e
o O L E A p p=" "
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm 3/8
7/9/13
I Ft h i s . A p p R u n n i n g ( ) t h i s . o O L E A p p=t h i s . G e t C u r r e n t I n s t a n c e ( ) E L S E t h i s . o O L E A p p=t h i s . C r e a t e N e w I n s t a n c e ( ) E N D I F E N D F U N C
* A d d i t i o n a lm e t h o d sg e ti n s e r t e dh e r e E N D D E F I N E This defines the core functionality of your abstract OLEApplication class. First check to ensure that a user of the class is not trying to instantiate it directly. If so, just cancel the INIT method, which prevents the object from being created. Then check to see if the application is running. If it is, create a reference to it. If not, create a new instance. You need to determine if the application you're trying to control is currently running because certain applications, such as Microsoft Excel, let you create multiple instances, while others, such as Word, don't. Here are two suggestions to determine if an application is already running. The first involves using the GetObject function to attempt to retrieve a reference to an OLE Automation server. At the time of this writing, this option works fine with Microsoft Excel, but not with Word. Override this function when you define a Word for Windows class, but for now, define this option as a protected function within your OLEApplication class. (You define the function as protected because you will only be calling this function from within the OLEApplication class or any class that you derive from it. There is no need to expose it to the "outside world.")
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm 4/8
7/9/13
t h i s . l C l o s e A p p W h e n D o n e=! l l R u n n i n g R E T U R Nl l R u n n i n g E N D F U N C To complete the definition of the OLEApplication class, create two methods, also defined as protected functions, that you will use to create references to new instances or retrieve references to current instances. Notice that each method contains only one statement. You could eliminate the overhead of the function call and place the CreateObject and GetObject calls directly in the INIT method, but then you wouldn't be able to customize the behavior of these methods in your subclasses. P R O T E C T E DF U N C T I O NC r e a t e N e w I n s t a n c e ( ) R E T U R NC R E A T E O B J E C T ( t h i s . c O L E R e g N a m e ) E N D F U N C
P R O T E C T E DF U N C T I O NG e t C u r r e n t I n s t a n c e ( ) R E T U R NG E T O B J E C T ( ,t h i s . c O L E R e g N a m e ) E N D F U N C
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm
5/8
7/9/13
c O L E R e g N a m e=" E x c e l . A p p l i c a t i o n "
P R O T E C T E DF U N C T I O NA p p R u n n i n g ( ) L O C A Ll n C h a n n e l ,; l l R u n n i n g ,; l l D D E O l d S a f e t y
l l D D E O l d S a f e t y=D D E S E T O P T I O N ( " S a f e t y " ) = D D E S E T O P T I O N ( " S a f e t y " ,. F . ) l n C h a n n e l=D D E I N I T I A T E ( " W i n W o r d " ," S y s t e m " ) I Fl n C h a n n e l< >1 * I t ' sr u n n i n g t h i s . l C l o s e A p p W h e n D o n e=. F . = D D E T E R M I N A T E ( l n C h a n n e l )
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm 6/8
7/9/13
l l R u n n i n g=. T . E N D I F
= D D E S E T O P T I O N ( " S a f e t y " ,l l D D E O l d S a f e t y )
R E T U R Nl l R u n n i n g
P R O T E C T E DF U N C T I O NG e t C u r r e n t I n s t a n c e ( ) R E T U R NG E T O B J E C T ( " " ,t h i s . c O L E R e g N a m e ) E N D F U N C
E N D D E F I N E It is interesting to note that Word does not support creating multiple instances of itself through OLE Automation, while Microsoft Excel does. Using the classes presented here will prevent you from having to worry about inadvertently creating another instance of Microsoft Excel. Another interesting difference between these two applications is that when releasing a reference to Word that was started through OLE Automation, the Word instance is automatically terminated. However, a reference to Microsoft Excel will not terminate in this fashion. Instead, we must send the Quit method to the Microsoft Excel object, explicitly instructing it to terminate. Furthermore, Microsoft Excel will terminate even if the reference to it was created from a previously running instance.
7/9/13
specializing in helping companies improve business processes. He can be reached on CompuServe at 71510,3217.
www.dfpug.de/loseblattsammlung%5Cmigration%5Cwhitepapers/foxole.htm
8/8