Microsoft KB Archive/108661

From BetaArchive Wiki

Article ID: 108661

Article Last Modified on 10/10/2006



APPLIES TO

  • Microsoft Excel 95 Standard Edition
  • Microsoft Excel 5.0 Standard Edition
  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 4.0 Standard Edition



This article was previously published under Q108661


SYMPTOMS

When you run code in a Microsoft Visual Basic version 3.0 application and the code uses a method or property of an object linking and embedding (OLE) automation object, you may receive the following error message

Method not applicable for this object

CAUSE

Some of the methods and properties of OLE Automation objects are keywords in Visual Basic version 3.0 and will cause the error message above when you use them in a Visual Basic application.

WORKAROUND

To use a method or property of a Microsoft Excel object in a Visual Basic application when the method or property is a Visual Basic keyword, use square brackets ([]) around the keyword in the Visual Basic code. For example, to use the Close method of the Microsoft Excel Workbooks object in a Visual Basic application, use the following syntax:

XL.Workbooks(1).[close]


MORE INFORMATION

OLE Automation is an industry standard that applications use to expose OLE objects to development tools, macro languages, and other containers that support OLE Automation. A few of the objects exposed by Microsoft Excel are workbooks, worksheets, and charts. These objects can be accessed with Visual Basic commands.

You use Visual Basic commands to manipulate these objects by invoking methods on the object or by getting and setting the objects' properties (just as you would with the objects in Visual Basic).

Steps to Reproduce Behavior

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.

  1. Run Microsoft Excel.
  2. Run Microsoft Visual Basic version 3.0.
  3. In Microsoft Visual Basic, enter and run the following code:

             ' Dimension variable xl as object type
             Dim xl as Object
             ' Activate Microsoft Excel and assign to variable xl
             Set xl = GetObject(,"Excel.Application.5")
             ' Create a new workbook so we can close it
             xl.Workbooks.Add
             ' Close the workbook we just created
             xl.ActiveWorkbook.Close

The following error message appears

Method not applicable to this object

even though Close is a method of the Microsoft Excel Workbooks object.

To make this piece of code work correctly, change the last line to:

xl.ActiveWorkbook.[Close]


REFERENCES

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

OLE Automation



Additional query words: XL

Keywords: kbprogramming KB108661