简体   繁体   中英

Error message “ActiveX component can't create object” appears for all objects

I'm unable to create any objects in an Excel macro I am trying to write. Initially I was trying to work with MSXML2.DOMDocument60, however, I realized that i'm not able to get ActiveX to create any objects. For example, I get the same error (Run-time 429 'ActiveX component can't create object') for this line of code:

Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")

Obviously since i'm in excel, the program is installed correctly and it should have access to it. I've checked several other Stackoverflow pages for this. It doesn't appear to be an issue with a reference and I can't imagine the simple code above not working because of missing dlls since I am again already in Excel.

Is there possibly a security feature on my computer blocking this action?

Again, my issue is not with the above code. This was just a simple way to show that Create object is not working.

My main goal is to get the following to work: Dim objXML As MSXML2.DOMDocument60 Set objXML = CreateObject("MSXML2.DOMDocument60")

I've already verified that I have the correct reference and I re-registered the Dlls.

There's no class with an Excel.Sheet progID in the Excel object model. You probably meant Excel.Worksheet , but that won't work either, and it's not because of CreateObject or any kind of obscure security feature.

Simply put, not all classes/types are creatable : In the Excel type library Application is, and that's about it: everything else needs to be created within the object model, using the factory methods provided by that API.

In other words the only way you can create an Excel.Worksheet , is if Excel creates it for you.

Set excelSheet = Excel.Application.ActiveWorkbook.Worksheets.Add

Same with an Excel.Workbook :

Set excelBook = Excel.Application.Workbooks.Add

There are other ways (eg copying an existing Worksheet without specifying a Target argument will create a new Workbook that contains a copy of that source Worksheet ), but rule of thumb if you can't New up a class in a referenced type library, there's little chance CreateObject can do any better (unless the type registration explicitly prevents early-binding usage).


The question was edited, but this answer stands: don't use CreateObject to create instances of classes that are already resolved and readily available.

Set objXml = New MSXML2.DOMDocument60 'works fine

If you really want to use CreateObject , then you need to use progID strings that exist in your registry.

Set objXml = CreateObject("MSXML2.DOMDocument60") ' blows up on my machine as well
Set objXml = CreateObject("MSXML2.DOMDocument") ' works fine

But using CreateObject to create an instance of a class the compiler already knows about, is a very, very roundabout way to do this.

I found the answer I was looking for. My company has the C drive and another network drive locked down, which prevents CreateObject from working when an excel sheet is saved to either of these locations. By saving the excel sheet to my desktop instead I am able to get it to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM