Thank you for helping with the code. I’ve put together this. It does exactly what I need, but it doesn't work on an assembly. Do you know how it could be adjusted? I'm not a good programmer, and AI helps me a lot with the code.
The code runs for a long time, but that's not a big problem.
Sub iPart_table_v1()
' Connect to Inventor
Dim inventorApp As Object
On Error Resume Next
Set inventorApp = GetObject(, "Inventor.Application")
On Error GoTo 0
' If unable to connect to Inventor
If inventorApp Is Nothing Then
MsgBox "Cannot connect to Inventor"
Exit Sub
End If
' Get the active document (assuming it is an iPart factory)
Dim oDoc As Object
Set oDoc = inventorApp.ActiveDocument
' Verify if the document is an iPart factory
If oDoc.ComponentDefinition.iPartFactory Is Nothing Then
MsgBox "This document is not an iPart factory."
Exit Sub
End If
' Get the iPart factory
Dim oFactory As Object
Set oFactory = oDoc.ComponentDefinition.iPartFactory
' Start Excel application in the background
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
' Optimize performance by turning off screen updates and events
xlApp.ScreenUpdating = False
xlApp.DisplayAlerts = False
xlApp.EnableEvents = False
xlApp.Visible = False ' Excel runs in the background
' Define the workbook and worksheet
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.WorkSheet
Set xlWorksheet = oFactory.ExcelWorkSheet ' Get the Excel sheet from iPart
' Get Custom iProperties
Dim oPropSets As PropertySets
Set oPropSets = oDoc.PropertySets
Dim oCustomProps As PropertySet
Set oCustomProps = oPropSets.Item("Inventor User Defined Properties")
' Get Summary iProperties
Dim oSummaryProps As PropertySet
Set oSummaryProps = oPropSets.Item("Inventor Summary Information")
' Get the value of Title from Summary iProperties
Dim titleValue As String
titleValue = oSummaryProps.Item("Title").Value
' Get values from Custom iProperties
Dim modificationtext_0 As String
Dim add_titleblockinfo As String
Dim cad_system As String
Dim surface_finish As String
Dim item_id As String
On Error Resume Next ' Error handling
modificationtext_0 = oCustomProps.Item("modificationtext_0").Value
add_titleblockinfo = oCustomProps.Item("add_titleblockinfo").Value
cad_system = oCustomProps.Item("CAD System").Value
surface_finish = oCustomProps.Item("Surface finish").Value
item_id = oCustomProps.Item("Item ID").Value
On Error GoTo 0 ' Resume normal error handling
' Check and add columns
Dim nextColumn As Integer
' Add column for Title
If Not ColumnExists("Title [Inventor Summary Information]", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "Title [Inventor Summary Information]"
Else
MsgBox "The 'Title' column already exists."
End If
' Add column for Item ID
If Not ColumnExists("Item ID [Inventor User Defined Properties]", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "Item ID [Inventor User Defined Properties]"
Else
MsgBox "The 'Item ID' column already exists."
End If
' Add column for add_titleblockinfo
If Not ColumnExists("add_titleblockinfo [Inventor User Defined Properties]", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "add_titleblockinfo [Inventor User Defined Properties]"
Else
MsgBox "The 'add_titleblockinfo' column already exists."
End If
' Add column for Surface finish
If Not ColumnExists("Surface finish [Inventor User Defined Properties]", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "Surface finish [Inventor User Defined Properties]"
Else
MsgBox "The 'Surface finish' column already exists."
End If
' Add column for modificationtext_0
If Not ColumnExists("modificationtext_0 [Inventor User Defined Properties]", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "modificationtext_0 [Inventor User Defined Properties]"
' Write the value "First revision" to all rows
For rowIndex = 2 To xlWorksheet.UsedRange.Rows.Count
xlWorksheet.Cells(rowIndex, nextColumn).Value = "First revision"
Next rowIndex
Else
MsgBox "The 'modificationtext_0' column already exists."
End If
' Add column for CAD System
If Not ColumnExists("CAD System [Inventor User Defined Properties]", xlWorksheet) Then
nextColumn = FindFirstFreeColumn(xlWorksheet)
xlWorksheet.Cells(1, nextColumn).Value = "CAD System [Inventor User Defined Properties]"
' Write the value "Inventor" to all rows
For rowIndex = 2 To xlWorksheet.UsedRange.Rows.Count
xlWorksheet.Cells(rowIndex, nextColumn).Value = "Inventor"
Next rowIndex
Else
MsgBox "The 'CAD System' column already exists."
End If
' Get reference to the workbook
Set xlWorkbook = xlWorksheet.Parent
' Save and close the workbook
xlWorkbook.Save
xlWorkbook.Close
' Restore Excel settings
xlApp.ScreenUpdating = True
xlApp.DisplayAlerts = True
xlApp.EnableEvents = True
' Quit Excel application
xlApp.Quit
Set xlApp = Nothing
' Display completion message
MsgBox "Finish!"
End Sub
' Function to check if a column exists
Function ColumnExists(columnName As String, xlWorksheet As Excel.WorkSheet) As Boolean
Dim cell As Excel.Range
For Each cell In xlWorksheet.Rows(1).Cells
If cell.Value = columnName Then
ColumnExists = True
Exit Function
End If
Next
ColumnExists = False
End Function
' Find first free column
Function FindFirstFreeColumn(xlWorksheet As Excel.WorkSheet) As Integer
Dim cell As Excel.Range
For Each cell In xlWorksheet.Rows(1).Cells
If IsEmpty(cell.Value) Then
FindFirstFreeColumn = cell.Column
Exit Function
End If
Next
' If no free column exists, add a new one at the end
FindFirstFreeColumn = xlWorksheet.Cells(1, xlWorksheet.Columns.Count).End(xlToLeft).Column + 1
End Function