Microsoft KB Archive/111311

{|
 * width="100%"|

XL: CreateObject Function Starts Invisible Instance of Excel

 * }

-

The information in this article applies to:


 * Microsoft Excel for Windows, versions 5.0, 5.0c
 * Microsoft Visual Basic Standard Edition for Windows, version 3.0

-

SYMPTOMS
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")

WORKAROUND
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

MORE INFORMATION
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

CreateObject Versus GetObject Functions
CreateObject and GetObject are two Visual Basic functions that you can use to return an OLE Automation object. The CreateObject function creates an OLE Automation object; the GetObject function retrieves an OLE Automation object from a file. The way an application behaves when you start it with one of these functions depends on the application. For example, when you use the CreateObject function to access a Microsoft Excel Application object, an invisible instance of Microsoft Excel runs. When you use the CreateObject function to access a Microsoft Word WordBasic object, a visible instance of Microsoft Word runs.

Use the following list to determine the behavior of Microsoft Excel when you use the CreateObject or GetObject function:

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"

Microsoft Word for Windows
When you use the CreateObject function to access a Microsoft Word for Windows WordBasic object, a new visible instance runs if Word for Windows is not currently running. If you set the variable returned by the CreateObject function equal to nothing in this case, Word for Windows is closed. Otherwise, if Word for Windows is already running, the CreateObject function uses the running instance. If you set the variable returned by the CreateObject function equal to nothing in this case, Word for Windows is not closed, because the instance was running before you ran the procedure.

Note that in Word for Windows 95, version 7.0, an invisible instance is run rather than a visible one.

The following Visual Basic procedure uses the CreateObject function to access the Word for Windows WordBasic object, performs some Word commands, and then sets the OLE Automation object variable equal to Nothing.

  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.

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide."

For more information on the GetObject function, query on the following words in the Microsoft Knowledge Base:

"ole and automation and getobject"