Mass Changing Default BOM Structure for multiple parts using VBA Code

Mass Changing Default BOM Structure for multiple parts using VBA Code

rhjones74
Contributor Contributor
637 Views
1 Reply
Message 1 of 2

Mass Changing Default BOM Structure for multiple parts using VBA Code

rhjones74
Contributor
Contributor

I am trying to change the default BOM Structure from Normal to Purchased for over 19,000 parts and assemblies using a string of VBA code to avoiding manually changing the setting for each individual part. I tested my code out in Excel and it worked perfectly, however it does not work in Inventor and I've hit a roadblock trying to troubleshoot it. When ran in Excel, it takes your designated file path and changes values in the specified cell in all excel values inside that folder and subsequent subfolders, just like I would like it to run in Inventor. I'm not sure why it does not work, I assume it has something to do with changing from the Excel program and going to a non-Microsoft program in Inventor.

 

I have very little experience in programming and even less in VBA, so any help is appreciated. Thanks!

 

Here is my Excel code that runs just fine:

 

 

Public Sub openWB()
Dim FSO As Object
Dim folder As Object, subfolder As Object
Dim wb As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
folderPath = "INSERT FILE PATH HERE"
Set folder = FSO.GetFolder(folderPath)

With Application
.DisplayAlerts = False
.ScreenUpdating = False
.EnableEvents = False
.AskToUpdateLinks = False
End With

For Each wb In folder.Files
If Right(wb.Name, 3) = "xls" Or Right(wb.Name, 4) = "xlsx" Or Right(wb.Name, 4) = "xlsm" Then
Set masterWB = Workbooks.Open(wb)
Cells(1, 1).Value = 4000
ActiveWorkbook.Close True
End If
Next
For Each subfolder In folder.SubFolders
For Each wb In subfolder.Files
If Right(wb.Name, 3) = "xls" Or Right(wb.Name, 4) = "xlsx" Or Right(wb.Name, 4) = "xlsm" Then
Set masterWB = Workbooks.Open(wb)
Cells(1, 1).Value = 4000
ActiveWorkbook.Close True
End If
Next
Next
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.EnableEvents = True
.AskToUpdateLinks = True
End With
End Sub

 

 

Here is my Inventor code that either gives a run-time error or runs but does not actually change the default BOM structure (bolded are the main changes from the Excel code):

 

 

Public Sub openWB()
    Dim FSO As Object
    Dim folder As Object, subfolder As Object
    Dim wb As Object
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    folderPath = "INSERT FILE PATH HERE"
    Set folder = FSO.GetFolder(folderPath)
    
    'With Application
        '.DisplayAlerts = False
        '.ScreenUpdating = False
        '.EnableEvents = False
        '.AskToUpdateLinks = False
    'End With
        
    For Each wb In folder.Files
        If Right(wb.Name, 3) = "ipt" Or Right(wb.Name, 3) = "iam" Then
            Set masterWB = Workbooks.Open(wb)
                Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
oDoc.ComponentDefinition.BOMStructure = kPurchasedBOMStructure ActiveWorkbook.Close True End If Next For Each subfolder In folder.SubFolders For Each wb In subfolder.Files If Right(wb.Name, 3) = "ipt" Or Right(wb.Name, 3) = "iam" Then Set masterWB = Workbooks.Open(wb) Dim oDoc As PartDocument
Set oDoc = ThisApplication.ActiveDocument
oDoc.ComponentDefinition.BOMStructure = kPurchasedBOMStructure ActiveWorkbook.Close True End If Next Next 'With Application '.DisplayAlerts = True '.ScreenUpdating = True '.EnableEvents = True '.AskToUpdateLinks = True 'End With End Sub

 

 

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

MechMachineMan
Advisor
Advisor

What you had was an ugly mish-mash of excel and inventor coding. Leads me to thing you have 0 understand of how Object-Oriented Programming works.

 

Just because a method/property works on one object in one application, that does not mean it's how another application utilizes it.

 

Workbooks is really only an excel thing. You cannot open an ipt file or an iam file in excel.

 

Something like this is more along the lines of what you are looking for.

 

Also, how you have it coded, it only goes 1 level of sub-folders deep. You need to either create a folder list to work through, or write a recursive function to handle such a feat.

 

Public Sub ChangeBOMStructToPurchased()
    Dim invApp As Inventor.Application
    invApp = ThisApplication

    Dim FSO As Object
    Dim folder As Object, subfolder As Object
    Dim wb As Object
Dim oDoc As Document Set FSO = CreateObject("Scripting.FileSystemObject") folderPath = "INSERT FILE PATH HERE" Set folder = FSO.GetFolder(folderPath) For Each wb In folder.Files If Right(wb.Name, 3) = "ipt" Or Right(wb.Name, 3) = "iam" Then Set oDoc = invApp.Documents.Open(wb) oDoc.ComponentDefinition.BOMStructure = kPurchasedBOMStructure oDoc.Close(False) End If Next For Each subfolder In folder.SubFolders For Each wb In subfolder.Files If Right(wb.Name, 3) = "ipt" Or Right(wb.Name, 3) = "iam" Then Set oDoc = invApp.Documents.Open(wb) oDoc.ComponentDefinition.BOMStructure = kPurchasedBOMStructure oDoc.Close(False) End If Next Next End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes