XL: CreateObject Function Starts Invisible Instance of Excel |
When you use the CreateObject function in a Visual Basic version 3.0
procedure to create a Microsoft Excel OLE Automation object, a new
instance of Microsoft Excel starts, but you cannot see it.
For example, the following Visual Basic command starts a new instance
of Microsoft Excel, but you cannot see it, and it does not appear in
the Task List:
Set x = CreateObject("Excel.Application")
If you want to see Microsoft Excel when you use the CreateObject function to access the application object, use the Visible property as in the following example:
Sub Run_Excel
Dim x As Object
Set x = CreateObject("Excel.Application")
x.Visible = true
End Sub
When you use the CreateObject function to run Microsoft Excel, you
start a new hidden instance of Microsoft Excel. Because this new
instance uses memory and resources on your system, you should exit
the program within the same procedure that runs the instance. If you
don't exit the program in the procedure, the invisible instance runs
until you exit Microsoft Windows.
To free the memory used by the object variable assigned to the OLE
Automation object, set the variable equal to Nothing. The following
Visual Basic procedure uses the CreateObject function to start an
invisible instance of Microsoft Excel, quits the instance, and sets
the OLE Automation object variable equal to Nothing.
Sub Run_Excel
' Dimension variable x as Object type
Dim x As Object
' Set x equal to Excel object
Set x = CreateObject("Excel.Application")
' Make running instance of Excel visible
x.Visible = true
' Insert desired Excel commands here
' Quit Microsoft Excel
x.Quit
' Set x equal to nothing to free memory object was using
Set x = Nothing
End Sub
Function Behavior
---------------------------------------------------------------------------
CreateObject("Excel.Application") Always loads a new invisible instance
GetObject("", "Excel.Application") Always loads a new visible instance
GetObject(, "Excel.Application") Either returns an already running
instance, or fails with error message
"OLE Automation server cannot create
object"
Sub Run_Word ()
' Dimension variable word as Object type
Dim word As Object
' Set word equal to Word for Windows object
' Start Word for Windows if not already running
Set word = CreateObject("Word.Basic")
' Create new file
word.FileNew
' Insert text in new file
word.Insert "Some Text"
' Save file as TEXT.DOC
word.FileSaveAs "text"
' Quit Word if it was not already running before this procedure ran
' Set word equal to nothing to free memory used for object variable
Set word = Nothing
End Sub
NOTE: The above information applies both to Visual Basic version 3.0, and
Visual Basic for applications. However, because Microsoft Excel has an
object library, use the functions defined in that library when you access
Microsoft Excel objects in a Visual Basic, Applications Edition procedure,
rather than the GetObject or CreateObject function. Because Microsoft Word
for Windows does not have an object library, you must use the CreateObject
or GetObject function to access a Microsoft Word object in any version of
Visual Basic.
ole and automation and getobject
For more information about the GetObject Function and the CreateObject Function, choose the Search button in the Visual Basic Reference and type:
OLE Automation
Additional query words: 7.00 officeinterop Word6 B_VBasic
Keywords : kbprg
Version : WINDOWS:3.0,5.0,5.0c
Platform : WINDOWS
Issue type :
Technology :
|
Last Reviewed: February 22, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |