Is it possible to open an iPart table through code?

Is it possible to open an iPart table through code?

Brock_Olly
Collaborator Collaborator
191 Views
5 Replies
Message 1 of 6

Is it possible to open an iPart table through code?

Brock_Olly
Collaborator
Collaborator

Hi

We use iParts for sheetmetal production. I was wondering if there's a way to open the iPart excel table using code so I can link it to a form and/or a button on the ribbon.

Brock_Olly_0-1745924866693.png



Thanks in advance!

0 Likes
Accepted solutions (1)
192 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Yes, sort of.  There are likely multiple ways to do that.  One way is through the iPartFactory.ExcelWorkSheet property, which gives us an Excel Worksheet object that we can edit by code.  But just accessing that property does not visibly show that Worksheet alone.  You would have to get the Worksheet's parent application, make it visible, and that sort of thing.  Then there is probably the 'command' that gets executed when you click that option in the right-click menu.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

Brock_Olly
Collaborator
Collaborator

Thanks for the insight.

I managed to get it to open an excel that's a copy current table however it's not linked to the file and updating this excel does nothing to the ipart.

 

 

Here's the code that sadly doesn't work:

Dim oDoc As PartDocument = ThisApplication.ActiveDocument
Dim oDef As PartComponentDefinition = oDoc.ComponentDefinition

If oDef.IsiPartFactory Then
    Dim oFactory As iPartFactory = oDef.iPartFactory

    ' Try to access and show the Excel worksheet
    Try
        Dim oSheet As Object = oFactory.ExcelWorkSheet
        Dim oExcelApp As Object = oSheet.Application
        oExcelApp.Visible = True
        oSheet.Parent.Activate()
    Catch
        MessageBox.Show("Excel couldn't be opened.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
Else
    MessageBox.Show("This is no iPart.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If


 Any other ideas?

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Had a lot of other stuff going on at work, but I'm back now (for a moment 😉).  I think that 'activate' line of code is what may be causing the error.  I can use the simple example code below and it will open the Excel Worksheet just fine.  I included a message in the middle of the code, to intentionally pause it, so that we can look at the opened Worksheet, but that message dialog may end up 'under' the Worksheet, so you may have to click on Inventor again to see the message dialog.  When you click OK on the message, that will let the code continue, where it will then close the Excel stuff, and negate the variables.

Dim oPDoc As PartDocument = TryCast(ThisDoc.Document, Inventor.PartDocument)
If oPDoc Is Nothing Then Return
Dim oPDef As PartComponentDefinition = oPDoc.ComponentDefinition
If Not oPDef.IsiPartFactory Then Return
Dim oFactory As iPartFactory = oPDef.iPartFactory
Dim oWS As Object = oFactory.ExcelWorkSheet 'Worksheet
Dim oWB As Object = oWS.Parent 'Workbook
Dim oExcelApp As Object = oWS.Application 'Application
'this worked OK for me
oExcelApp.Visible = True
MsgBox("Look At Opened Excel Worksheet." & vbCrLf & _
"When you click OK, it will be closed again.", vbInformation, "iLogic")
'<<< do stuff with the Worksheet here, if needed >>>
'oWB.Save()
oWB.Close()
oExcelApp.Quit()
oWS = Nothing 'Worksheet
oWB = Nothing 'Workbook
oExcelApp = Nothing 'Application

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 6

WCrihfield
Mentor
Mentor

The way I coded that, if you manually close Excel before you allow the code to close it, then when the code continues and attempts to close it, it will throw an error.  It was just a quickie code example that I typed up as an example, and could obviously use a lot of refinement and further development.

The other possible way to show that Worksheet, as mentioned before, is to execute the command (ControlDefinition) named "AppLaunchViaSpreadSheetCmd".  But I believe that you would have to 'select' that iPart Table in the model tree before you execute that command, so that the command knows what it should be acting upon.  Using selections and command executions is a way to sort of 'simulate' manual user interactions, but is not usually ideal, when there is an alternative way to do it.  For example, if you used selection and command execution here, you would not have any control over the Excel Worksheet by code, and would have to do all further interactions manually, instead of by code.  The original example (in my previous reply) allows the code to maintain control over the Worksheet, so that you can edit the Worksheet by code, if needed.  Just an alternative way of doing things.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6

Brock_Olly
Collaborator
Collaborator

Hi, thanks for taking your time to review this.

Your code works quite well, there's some things that are different from the actual built-in table though however I think I can work with it.

Thanks!

0 Likes