VBA - Looping through multiple subfolders to change the default BOM Structure

VBA - Looping through multiple subfolders to change the default BOM Structure

rhjones74
Contributor Contributor
1,414 Views
2 Replies
Message 1 of 3

VBA - Looping through multiple subfolders to change the default BOM Structure

rhjones74
Contributor
Contributor

I am trying to write a VBA code to change the default BOM Structure for thousands of parts in Inventor. These parts are located in a folder with multiple subfolders that I would like to be able to drill through and change all at once without having to change my folder path. Right now I have a code that works perfectly fine when it comes to opening a part, changing the BOM Structure from "Normal" to "Purchased", and then saving and closing the part. My only issue is that this code only digs through one subfolder, where as I would like it to be able to dig through infinitely many subfolder (in reality it maybe only needs to drill through 5 or 6) using most likely a recursive function. Any help is greatly appreciated.

 

Here is the code I have now:

 

 

Public Sub ChangingBOMStructure()
    Dim invent As Inventor.Application
    Set invent = 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 = "\\Tg-dc1\fpe\FORMS\Mason Murphy Forms\Dummy Purchased Parts\Bearing\Ami\3 Bolt Flange Bearings"
    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 = invent.Documents.Open(wb)
                oDoc.ComponentDefinition.BOMStructure = kPurchasedBOMStructure
                oDoc.Save
		oDoc.Close
        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 = invent.Documents.Open(wb)
                oDoc.ComponentDefinition.BOMStructure = kPurchasedBOMStructure
		oDoc.Save
                oDoc.Close
            End If
        Next
    Next
End Sub

 

0 Likes
1,415 Views
2 Replies
Replies (2)
Message 2 of 3

mcgyvr
Consultant
Consultant

First link I found with a recursive folder looping example..

https://forums.autodesk.com/t5/visual-basic-customization/vba-macro-to-loop-through-folder-and-sub-f...

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 3 of 3

MechMachineMan
Advisor
Advisor

Sample here for assembly files:

 

https://forums.autodesk.com/t5/inventor-customization/change-bom-structure-of-all-assy-children-to-p...

 

Just add all of the files to a temporary assembly and run the rule....


--------------------------------------
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