Microsoft KB Archive/141766

= Visual Basic Add Method Fails If Object Already Exists =

Article ID: 141766

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q141766



SYMPTOMS
In Microsoft Excel, when you create a Microsoft Visual Basic for Applications macro and you attempt to add an object for the second time, the Add method fails and you receive the following error message:

Run time error 1004: Add method of class failed

Where is the object collection used.



CAUSE
This error message occurs when the collection object of the same class and name already exists, and you attempt to add it a second time with the same name.

This behavior is by design of Microsoft Excel, because it eliminates the possibility of having two objects of the same class with the same name.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. For example, the following macro Sub Add_Style For x = 1 to 2 ActiveWorkbook.Styles.Add Name:="mystyle" Next End Sub produces the following error in the second iteration of the loop:

Run time error 1004: Add method of Styles class failed

