XL: CreateObject Function Starts Invisible Instance of Excel

Last reviewed: January 16, 1998
Article ID: Q111311
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Visual Basic 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

REFERENCES

For more information about the GetObject Function and the CreateObject Function, choose the Search button in the Visual Basic Reference and type:

    OLE Automation


KBCategory: kbprg
KBSubcategory:

Additional query words: 7.00 5.00 officeinterop Word6 B_VBasic
Keywords : kbprg kbprg
Version : 5.00 5.00c 7.00
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: January 16, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.