VBA - Looping through multiple subfolders to change the default BOM Structure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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