InvalidCastException when creating new part

InvalidCastException when creating new part

Anonymous
Not applicable
450 Views
1 Reply
Message 1 of 2

InvalidCastException when creating new part

Anonymous
Not applicable

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

0 Likes
451 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

Never mind, I figured it out.  I had to define:

Dim oDoc as Inventor.Document

 

0 Likes