VBA code only working in some .idws and with some .iams
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
So, I was able to work with some code a number of you had helped me with about a week ago. Everything seemed to be going fine with my occurrence tracking/labeling until I would put a certain sub-assembly into the "main" assembly. I started to look at the difference between that sub and other subs in the assembly and can find no discernable difference as far as settings go. Then for yucks, I tried the VBA code on a new .idw file (my code is in a standard macro/VBA form that opens when I open inventor). Turns out, when I run the VBA in the new idw it does not create the table at all, with a main assemlby that was working in a previous drawing. What am I missing. It doesn't error out either, just never creates a new table.
So the idea is that the code reads through an assembly, and burrows down through it. It looks for all occurrences that aren't integers but are infact text. This is how we label instances of the same sensors and proxes, etc. This is how I determine my qty of inputs on a machine (and outputs) as well as make tags which are then given to controls department...it keeps continuity in naming...
So, I am seeing two issues.
1) Why does this code only seem to work in one drawing? When I create a new drawing and put the "main" assembly in it, it does not create a table. It only works in the existing drawing I have. (of which I created an attached screen shot).
2) Why does it not work even in the "good" drawing if certain sub assemblies are present? (as soon as i delete them, it will create the table for me).
Unfortunately, I cannot share any of the actual CAD files due to proprietary issues with our customer.
As near as I can tell, this code does have to run in the .idw with the BOM sturcture set to Parts Only.
Here's the code:
Public Sub CreateNewCustomTable()
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = oDoc.ActiveSheet.DrawingViews.Item(1).ReferencedDocumentDescriptor.ReferencedDocument
Dim oDef As AssemblyComponentDefinition
Set oDef = oAsmDoc.ComponentDefinition
Dim sensorCnt As Integer
Dim oContents() As String
Dim IsInteger As Boolean
Dim LArray() As String
Dim occ As ComponentOccurrence
For Each occ In oDef.Occurrences.AllLeafOccurrences
On Error Resume Next
LArray = Split(occ.Name, ":")
IsInteger = ConvertToInteger(LArray(1))
If IsInteger = False Then
sensorCnt = sensorCnt + 1
End If
Next
If sensorCnt > 0 Then
sensorCnt = sensorCnt * 2
ReDim oContents(sensorCnt - 1) As String
Dim i As Integer
i = 0
For Each occ In oDef.Occurrences.AllLeafOccurrences
LArray = Split(occ.Name, ":")
IsInteger = ConvertToInteger(LArray(1))
If IsInteger = False Then
oContents(i) = LArray(0)
oContents(i + 1) = LArray(1)
i = i + 2
End If
Next
End If
' Set a reference to the active sheet.
Dim oSheet As Sheet
Set oSheet = oDoc.ActiveSheet
' Set the column titles
Dim oTitles(1 To 2) As String
oTitles(1) = "Part Number"
oTitles(2) = "Sensor Description"
' Set the column widths (defaults to the column title width if not specified)
Dim oColumnWidths(1 To 2) As Double
oColumnWidths(1) = 4.5
oColumnWidths(2) = 5.5
oDoc.StylesManager.ActiveStandardStyle.ActiveObjectDefaults.TableStyle.HeadingGap = 0.1
' Create the custom table
Dim oCustomTable As CustomTables
Set oCustomTable = oSheet.CustomTables.Add("Input Locations & Descriptions", ThisApplication.TransientGeometry.CreatePoint2d(32.7, 27), _
2, sensorCnt / 2, oTitles, oContents, oColumnWidths)
' Change the 3rd column to be left justified.
oCustomTable.Columns.Item(1).ValueHorizontalJustification = kAlignTextLeft
oCustomTable.Columns.Item(2).ValueHorizontalJustification = kAlignTextLeft
' Create a table format object
Dim oFormat As TableFormat
Set oFormat = oSheet.CustomTables.CreateTableFormat
' Set inside line color to red.
oFormat.InsideLineColor = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
' Set outside line weight.
oFormat.OutsideLineWeight = 0.05
' Modify the table formats
oCustomTable.OverrideFormat = oFormat
End Sub
Function ConvertToInteger(v1 As String) As Boolean
On Error GoTo 100:
Dim i As Integer
i = CInt(v1)
ConvertToInteger = True
Exit Function
100:
ConvertToInteger = False
End Function