Help with error - The type initializer for 'DocumentExtensions' threw an exception.

Help with error - The type initializer for 'DocumentExtensions' threw an exception.

JBerns
Advisor Advisor
252 Views
6 Replies
Message 1 of 7

Help with error - The type initializer for 'DocumentExtensions' threw an exception.

JBerns
Advisor
Advisor

Community,

 

An external iLogic rule that worked in Inventor 2022 is not working in Inventor 2025.

 

The error:

The type initializer for 'DocumentExtensions' threw an exception.

 

Captured from Visual Studio debug modeCaptured from Visual Studio debug mode

 

JBerns_0-1750861391083.png

 

I have attached the IAM and Vb code in a Zip file.
Be certain to edit the 'AddVbFile' line to match where you save the external rule (Vb) file.

 

I have tried:

Dim doc As AssemblyDocument = ThisApplication.ActiveDocument

and

Dim doc As Inventor.AssemblyDocument = ThisApplication.ActiveDocument

 

It is likely to be an 'Import' or 'AddReference' that I am missing, but I have been unsuccessful at finding a solution.


I hope the community can help.

 

Thank you for your time and attention. I look forward to your reply.

 

Regards,
Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (2)
253 Views
6 Replies
Replies (6)
Message 2 of 7

JBerns
Advisor
Advisor

Code working in Inventor 2022:

 

Code not working in Inventor 2025:

 

@MjDeck (Mike), You were able to provide a solution to the last issue when code that worked in 2022, failed in 2025.

I hope you may find a solution again.

 

Regards,

Jerry 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 3 of 7

MjDeck
Autodesk
Autodesk
Accepted solution

Hi Jerry - the problem is that the GetObject function is not available in .NET 8. Inventor 2025 runs on .NET 8. There is a way to do the equivalent of GetObject, but it requires extra code. And in any case, it wasn't a good idea to use it even in Inventor 2024 and earlier. It's required when you're running outside of the Inventor process. But in an iLogic rule you always have ThisApplication available. It's just a matter of hooking it up.

Maybe you want to use this same VB file both in an iLogic rule and in an EXE running out of process?  You can probably still do that.
Here's the iLogic-specific fix. Replace this line:

Private ThisApplication As Object = GetObject(, "Inventor.Application")

with this:

Public ThisApplication As Inventor.Application


Then in the rule that calls it, add this line near the top:

DocumentExtensions.ThisApplication = ThisApplication


Let me know if you also want to use that VB file in an external EXE. I can provide details about a replacement for GetObject. But I wouldn't recommend adding it to this particular VB file. It's better to keep it separate.

Note: If you want to edit that VB file in Inventor, you can do it by clicking in the Options tab in the rule editor and checking the "Straight VB Code" option.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 4 of 7

MjDeck
Autodesk
Autodesk
Accepted solution

I forgot to mention: please comment out the line that gets the Vault add-in. If you want to use that in future, I recommend converting it into a Property. Then you can get it on demand.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 5 of 7

JBerns
Advisor
Advisor

Mike,

Thank you for the quick reply.

I got the code working with Inventor 2025. Thank you!

I will attach the internal rule (.TXT) and external rule (.Vb) into a .ZIP file in case others would like to see the solution.

 

Currently, no plans to use any of this code in an EXE. The solution to run in iLogic rules is acceptable for the moment.

If you want to share the GetObject replacement code to assist others following this thread, you are welcome to do so.

 

Thanks again, Mike, for the solution. I can get back to code development.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 6 of 7

JBerns
Advisor
Advisor

Mike,

I did comment out the "vaultApp" line since it too was getting an error (System.NullReferenceException: 'Object reference not set to an instance of an object.')

I would be interested in a solution for that as your time permits.

Regards,

Jerry 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 7 of 7

MjDeck
Autodesk
Autodesk

No problem. In the external rule, replace the commented-out line with this:

Public Readonly Property VaultApp As ApplicationAddIn
	Get
		Return ThisApplication.ApplicationAddIns.ItemById("{48B682BC-42E6-4953-84C5-3D253B52E77B}")
	End Get
End Property

Or you could make it Private. But if you leave it as Public then you can access it from the rule that calls that module.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes