How to Properly Loop through ReferencedDocuments.

How to Properly Loop through ReferencedDocuments.

sgwilliams
Collaborator Collaborator
843 Views
2 Replies
Message 1 of 3

How to Properly Loop through ReferencedDocuments.

sgwilliams
Collaborator
Collaborator

I have a part number called abc-123. The relevant files include an idw and 3-4 ipt files one for each operation the part goes through in the manufacturing process. The file names are as follows:

Drawing filename "abc-123.idw"

Model Files Include:

Finished Part name "abc-123.ipt"

op-10 part name "abc-123_op10.ipt"

op-20 part name "abc-123_op20.ipt"

op-30 part name "abc-123_op30.ipt"

op-40 part name "abc-123_op40.ipt"

 

Here is my code:

Dim app As Application: Set app = ThisApplication
Dim doc As Document: Set doc = app.ActiveDocument
Dim fso As New Scripting.FileSystemObject

Dim ffName: Set ffName = doc.fullFileName 'Gets path with filename and extension

Dim ext: Set ext = fso.GetExtensionName(ffName) 'Gets just the extension
Dim fName: Set fName = fso.GetFileName(ffName) 'Gets just the filename with extension
Dim pNo: Set pNo = Left(fName, Len(fName) - Len(ext) - 1)

If TypeOf doc Is DrawingDocument Then
Dim imFile As Document 'inventor model file Set imFile = doc.ReferencedDocuments(1) 'The (1) represents the Item1 found in the Locals path tree Set designInfo = imFile.PropertySets.Item("Design Tracking Properties") Dim rdpNo As Property Set rdpNo = designInfo.Item("Part Number") 'Referenced Document Part Number

The problem I'm running into is that all the idw files don't always have the Finished part name as Item1:

Set imFile = doc.ReferencedDocuments(1)

 there are actually 5 items. Sometimes it will be Item2, Item3, Item4 or Item5. I have to use the PropertySets from the Finished PartName as this is where we store all our iProperties Data. 

 

So my Plan was to do an If statement that compares "pNo" to rdpNo" if they match then use that item PropertySet.

 

Just looking for some guidance on if this is the correct way to achieve this or do I need to code it differently.

Work Station : Custom built, Liquid Cooled, Asus Pro WS X570-ACE Motherboard; AMD Ryzen 9 3900X 3.8 GHz 12-Core Processor; ASUS TUF Gaming NVIDIA GeForce RTX 3060 V2 OC Edition Video Card; 32 GB CORSAIR Dominator Platinum DDR4-2132 Memory ; Samsung 850 EVO 1TB SSD Drive.
0 Likes
Accepted solutions (1)
844 Views
2 Replies
Replies (2)
Message 2 of 3

sgwilliams
Collaborator
Collaborator

I figured it out. Here is the code that works.

 

 If TypeOf doc Is DrawingDocument Then
    
    Dim imFileCount As Variant
        imFileCount = doc.ReferencedDocuments.Count
                
        For i = 1 To imFileCount
    
            Dim imfFile As Variant
                imfFile = doc.ReferencedDocuments(i).FullFileName
            
            Dim rDoc As Document
            Set rDoc = doc.ReferencedDocuments(i)
        
            Dim fExt As String
                fExt = fso.GetExtensionName(imfFile)
            
            Dim imFile As String
                imFile = fso.GetFileName(imfFile)
        Next
        
        Dim rDocFN As Variant
        rDocFN = pNo + "." + fExt
        
        Dim mFile As Variant
        mFile = imFile
        
        Dim x As Integer
        x = StrComp(rDocFN, mFile)
        
        If x = 0 Then
            
             Set engInfo = rDoc.PropertySets.Item("Design Tracking Properties") 'get the checkedBy and authorBy approval Property Set
        
             Dim rdpNo As Property
             Set rdpNo = engInfo.Item("Part Number") 'Referenced Document Part Number
        
             Dim checkedBy As Property
             Set checkedBy = engInfo.Item("Checked by")
 
             Dim authorBy As Property
             Set authorBy = engInfo.Item("Engr Approved By")
 
             Set salesInfo = rDoc.PropertySets.Item("Inventor User Defined Properties") 'get the salesBy approval Property Set
 
             Dim salesBy As Property
             Set salesBy = salesInfo.Item("Sales Rep.")
        
        Else: Return
        
        End If
Work Station : Custom built, Liquid Cooled, Asus Pro WS X570-ACE Motherboard; AMD Ryzen 9 3900X 3.8 GHz 12-Core Processor; ASUS TUF Gaming NVIDIA GeForce RTX 3060 V2 OC Edition Video Card; 32 GB CORSAIR Dominator Platinum DDR4-2132 Memory ; Samsung 850 EVO 1TB SSD Drive.
0 Likes
Message 3 of 3

sgwilliams
Collaborator
Collaborator
Accepted solution

Ok, I had the for loop, next statement in the wrong place and had a return statement that did not belong. here is the final working code.

 

 If TypeOf doc Is DrawingDocument Then
    
    Dim imFileCount As Variant
        imFileCount = doc.ReferencedDocuments.Count
                
        For i = 1 To imFileCount
    
            Dim imfFile As Variant
                imfFile = doc.ReferencedDocuments(i).FullFileName
            
            Dim rDoc As Document
            Set rDoc = doc.ReferencedDocuments(i)
        
            Dim fExt As String
                fExt = fso.GetExtensionName(imfFile)
            
            Dim imFile As String
                imFile = fso.GetFileName(imfFile)
           'Next
        
            Dim rDocFN As Variant
                rDocFN = pNo + "." + fExt
        
            Dim mFile As Variant
            mFile = imFile
        
            Dim x As Integer
                x = StrComp(rDocFN, mFile)
        
           If x = 0 Then
               
                Set engInfo = rDoc.PropertySets.Item("Design Tracking Properties") 'get the checkedBy and authorBy approval Property Set
           
                Dim rdpNo As Property
                Set rdpNo = engInfo.Item("Part Number") 'Referenced Document Part Number
           
                Dim checkedBy As Property
                Set checkedBy = engInfo.Item("Checked by")
    
                Dim authorBy As Property
                Set authorBy = engInfo.Item("Engr Approved By")
    
                Set salesInfo = rDoc.PropertySets.Item("Inventor User Defined Properties") 'get the salesBy approval Property Set
    
                Dim salesBy As Property
                Set salesBy = salesInfo.Item("Sales Rep.")
           
           End If
           
        Next
End If
Work Station : Custom built, Liquid Cooled, Asus Pro WS X570-ACE Motherboard; AMD Ryzen 9 3900X 3.8 GHz 12-Core Processor; ASUS TUF Gaming NVIDIA GeForce RTX 3060 V2 OC Edition Video Card; 32 GB CORSAIR Dominator Platinum DDR4-2132 Memory ; Samsung 850 EVO 1TB SSD Drive.