Message 1 of 2
InvalidCastException when creating new part
Not applicable
03-05-2012
08:20 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to create a new part in Inventor from Microsoft Visual Studio and the code I have so far is:
Dim invApp As Inventor.Application
Try
invApp = GetObject(, "Inventor.Application")
Catch ex As Exception
Try
invApp = CreateObject("Inventor.Application")
invApp.Visible = True
Catch ex2 As Exception
MsgBox("Unable to connect to or start Inventor.")
Exit Sub
End Try
End Try
Dim oDoc As XDocument
oDoc = invApp.Documents.Add(Inventor.DocumentTypeEnum.kPartDocumentObject, invApp.FileManager.GetTemplateFile(Inventor.DocumentTypeEnum.kPartDocumentObject))When I run this, it opens Inventor and creates a new part then gives me the following InvalidCastException was unhandled:
Unable to cast COM object of type 'System.__ComObject' to class type 'System.Xml.Linq.XDocument'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
Does anyone know how to rectify this?
Thanks in advance,
Panos