<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic automate re-referencing files with lost reference in Inventor Programming - iLogic, Macros, AddIns &amp; Apprentice</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13148240#M173914</link>
    <description>&lt;P&gt;I have many assemblies and simplified parts with broken references due to renamed or relocated files.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Most of the time, these broken references are due to a restructured file organization, where about 80% of all created parts are now stored in a single folder (let’s say the path is "C:\BigFolder") without much change to their names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When a file has lost a reference due to a name change, it's primarily because the naming format was originally like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;[Partnumber as numbers only] [Partname]&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And now it’s structured like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;[Partnumber as numbers only]_[Partname]&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, the only change is that the seperator is now an underscore instead of a space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My idea was to let the macro search for the first part of the name (the part number) in "C:\BigFolder" and attempt to find the closest match. If nothing is found, the macro should skip to the next broken reference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the name is very similar and the new path is known, I think automating this should be possible.&lt;/P&gt;&lt;P&gt;Mmaybe with a VBA macro, iLogic or something similar.&lt;/P&gt;&lt;P&gt;But I'm unsure how to start—both in terms of accessing the reference data and implementing the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;Does anyone have ideas on automating this?&lt;/P&gt;</description>
    <pubDate>Wed, 13 Nov 2024 15:06:31 GMT</pubDate>
    <dc:creator>torben_schroederXDK86</dc:creator>
    <dc:date>2024-11-13T15:06:31Z</dc:date>
    <item>
      <title>automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13148240#M173914</link>
      <description>&lt;P&gt;I have many assemblies and simplified parts with broken references due to renamed or relocated files.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Most of the time, these broken references are due to a restructured file organization, where about 80% of all created parts are now stored in a single folder (let’s say the path is "C:\BigFolder") without much change to their names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When a file has lost a reference due to a name change, it's primarily because the naming format was originally like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;[Partnumber as numbers only] [Partname]&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And now it’s structured like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;[Partnumber as numbers only]_[Partname]&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see, the only change is that the seperator is now an underscore instead of a space.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My idea was to let the macro search for the first part of the name (the part number) in "C:\BigFolder" and attempt to find the closest match. If nothing is found, the macro should skip to the next broken reference.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the name is very similar and the new path is known, I think automating this should be possible.&lt;/P&gt;&lt;P&gt;Mmaybe with a VBA macro, iLogic or something similar.&lt;/P&gt;&lt;P&gt;But I'm unsure how to start—both in terms of accessing the reference data and implementing the macro.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-align-left"&gt;Does anyone have ideas on automating this?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2024 15:06:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13148240#M173914</guid>
      <dc:creator>torben_schroederXDK86</dc:creator>
      <dc:date>2024-11-13T15:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13148531#M173918</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/15156028"&gt;@torben_schroederXDK86&lt;/a&gt;.&amp;nbsp; I don't think I have ever created a whole automation solution for a situation like this, but I know that I have touched on this general subject on multiple occasions in the past.&amp;nbsp; If I remember correctly the following method was involved in that process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=DesignProjectManager_ResolveFile" target="_blank" rel="noopener"&gt;DesignProjectManager.ResolveFile&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, that method is associated with the &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-DesignProjectManager" target="_blank" rel="noopener"&gt;DesignProjectManager&lt;/A&gt; Inventor API object.&amp;nbsp; That object can be found directly under the &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-Application" target="_blank" rel="noopener"&gt;Application&lt;/A&gt; object (ThisApplication).&amp;nbsp; I have not really use that method that much myself, because I always resolve files manually, using the user interface dialogs, so I can not offer much as far as working examples of using it, or more detailed explanations about how it works beyond what its help page offers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another method that may get involved is this one:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=FileDescriptor_ReplaceReference" target="_blank" rel="noopener"&gt;FileDescriptor.ReplaceReference&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you can see, it is associate with the &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-FileDescriptor" target="_blank" rel="noopener"&gt;FileDescriptor&lt;/A&gt; Inventor API object.&amp;nbsp; And that FileDescriptor object can be obtained from 4 different possible routes, as you can see under its 'Accessed From' portion of its online help page.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you should understand the difference between an &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-Document" target="_blank" rel="noopener"&gt;Inventor.Document&lt;/A&gt; and an &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-File" target="_blank" rel="noopener"&gt;Inventor.File&lt;/A&gt; first.&amp;nbsp; The File is essentially only the data that has been written to a long term storage medium, like a hard drive, or USB drive.&amp;nbsp; The Document is essentially an organized group of data being held within Inventor's session memory.&amp;nbsp; The document can exist before or after a File exists for it, because we can create new documents that have not been 'saved' yet.&amp;nbsp; And when a Document does have a File associated with it, it may contain different data than what is in the File, because we may have made changes to the Document that have not yet been saved to that file.&amp;nbsp; There are several different derived types of Documents, and each of them has a ReadOnly Property named &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=Document_File" target="_blank" rel="noopener"&gt;File&lt;/A&gt;, that is supposed to return an Inventor.File type object, if it exists.&amp;nbsp; That File object has the&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=File_ReferencedFileDescriptors" target="_blank" rel="noopener"&gt;File.ReferencedFileDescriptors&lt;/A&gt;&amp;nbsp;property, which will return a &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-FileDescriptorsEnumerator" target="_blank" rel="noopener"&gt;FileDescriptorsEnumerator&lt;/A&gt; object.&amp;nbsp; You may also notice that the File has a&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=File_HasLoadedDocuments" target="_blank" rel="noopener"&gt;File.HasLoadedDocuments&lt;/A&gt;&amp;nbsp;property, which tells us that it is possible for a single File to contain the required data for defining multiple Documents.&amp;nbsp; Good to keep in mind.&amp;nbsp; Pay no attention to the&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=File_HasReferencingFiles" target="_blank" rel="noopener"&gt;HasReferencingFiles&lt;/A&gt;&amp;nbsp;property, or the&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=File_ReferencingFiles" target="_blank" rel="noopener"&gt;ReferencingFiles&lt;/A&gt;&amp;nbsp;properties, because they will only be able to contain anything when those other Files &amp;amp;/or documents are currently loaded into Inventor's session memory, other wise they can not be found by Inventor.&amp;nbsp; That's something that Vault can likely help with, if you have it.&amp;nbsp; Back to the FileDescriptorsEnumerator...as you iterate through that collection, you encounter individual &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-FileDescriptor" target="_blank" rel="noopener"&gt;FileDescriptor&lt;/A&gt; objects, like we mentioned earlier.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Nov 2024 17:17:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13148531#M173918</guid>
      <dc:creator>WCrihfield</dc:creator>
      <dc:date>2024-11-13T17:17:34Z</dc:date>
    </item>
    <item>
      <title>Re: automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13149664#M173934</link>
      <description>&lt;P&gt;Thank you for your detailed answer. It seems like I’ll need to look for it myself, for now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I still don’t quite understand is how to interact with the resolve-file dialog. My knowledge of this area in VBA is quite limited. Could you explain what I can do to make a macro run &lt;EM&gt;before&lt;/EM&gt; the file resolve dialog appears? I imagine that once it pops up, I won’t be able to run my macro until the window is closed...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe there’s something I could try using a trigger when opening a document.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any other ideas?&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2024 06:32:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13149664#M173934</guid>
      <dc:creator>torben_schroederXDK86</dc:creator>
      <dc:date>2024-11-14T06:32:48Z</dc:date>
    </item>
    <item>
      <title>Re: automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13150290#M173952</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/15156028"&gt;@torben_schroederXDK86&lt;/a&gt;.&amp;nbsp; After seeing your response this morning, and thinking about it, I thought I remembered there being an 'Event' related to this 'action' , so I looked around a bit and found it.&amp;nbsp; There is an object called&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-FileAccessEvents" target="_blank" rel="noopener"&gt;FileAccessEvents&lt;/A&gt;&amp;nbsp;right under the main &lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-Application" target="_blank" rel="noopener"&gt;Application&lt;/A&gt; object, and it has an Event named&amp;nbsp;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=FileAccessEvents_OnFileResolution" target="_blank" rel="noopener"&gt;OnFileResolution&lt;/A&gt;&amp;nbsp;which seems to be what you are looking for.&amp;nbsp; Its online help page actually contains quite a bit of useful information that you should review.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend that you avoid creating new automation content within VBA, and stick with iLogic rules.&amp;nbsp; The folks at Autodesk stopped including VBA in their standard installations back around 2021, partially due to security issues (among others).&amp;nbsp; This is partially because Microsoft stopped maintaining &amp;amp; developing that coding resource several years ago.&amp;nbsp; It is also pretty outdated now.&amp;nbsp; However, because it is 'free' to use, readily available to Inventor users, and its user interface is more advanced than the iLogic rule editor dialog, it can still be valuable resource to keep handy.&amp;nbsp; Many people like its visual Object Browser dialog, and the information that its Locals window can provide, when researching new objects or new functionality.&amp;nbsp; The iLogic add-in allows us to use the newer VB.NET coding system, and iLogic rules are much easier to 'trigger' to run when events happen.&amp;nbsp; However, when it comes to Inventor API Events (as opposed to just using the iLogic Event Triggers dialog), these are usually best managed by an Inventor add-in than with iLogic rules.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you seem relatively new to coding with VBA (and possibly with iLogic too), then you may also be relatively new to 'handling events' by code, without relying on the iLogic Event Triggers dialog, so I will attach a PDF document that you can review, which may help familiarize you with this concept.&amp;nbsp; It can be a bit advanced, and challenging to manage properly at first.&amp;nbsp; That document is several years old now, and could likely use some updating, but it will at least get you started in that direction.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2024 13:00:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13150290#M173952</guid>
      <dc:creator>WCrihfield</dc:creator>
      <dc:date>2024-11-14T13:00:18Z</dc:date>
    </item>
    <item>
      <title>Re: automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13152080#M173979</link>
      <description>&lt;P&gt;You're a saint—thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solution I’ve come up with so far avoids interacting with the resolve-file dialog entirely. Instead, I run a macro that replaces all parts with missing references (ActiveDocument.File.ReferencedFileDescriptors.ReferenceMissing).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What it essentially does is loop through every file in the active assembly (or drawing for that matter, if I set the FileReference as ReferencedObjects(1)) and check if the file has a reference. If it doesn’t, it replaces it; otherwise, it moves to the next file.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I’ll look into the PDF and the event you mentioned—thank you again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You guessed right that I’m still fairly new to VBA. I didn’t realize it was so… “frowned upon,” it seems. Besides VB.NET being newer, are there any other major differences from VBA? I’ll make sure to dive deeper into it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;edit:&lt;/P&gt;&lt;P&gt;would you be interested in the code at all? (is it normal in this forum that if somebody has come up with his own solution that the code is posted as a solution?)&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2024 07:45:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13152080#M173979</guid>
      <dc:creator>torben_schroederXDK86</dc:creator>
      <dc:date>2024-11-15T07:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13152890#M173995</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/15156028"&gt;@torben_schroederXDK86&lt;/a&gt;.&amp;nbsp; I'm glad that I was able to help you develop a working solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Using VBA (Visual Basic for Applications) is not really frowned upon.&amp;nbsp; It is just highly recommended (not just by me, but by the majority of the coding/automation community) that, because VBA has not been maintained in several years, and there are newer/better options readily available to us, that any 'new' automation solution development be done using one of the newer systems.&amp;nbsp; Since Inventor's iLogic system has been around for a great many years now, and it has been using the newer VB.NET coding language for most of those years, it just seems like the next logical step in that direction.&amp;nbsp; And since VB.NET sort of shares some of the same ancestry as VBA, stepping forward into VB.NET is 'relatively' easy.&amp;nbsp; Likely easier for those with less experience using VBA, than for those with lots of experience using it.&amp;nbsp; Many of the VBA sample programs that Autodesk has published in its online help, can be pretty easily converted to VB.NET so that they can be used in iLogic rule, by removing certain keywords (such as 'Set', 'Let', &amp;amp; 'Call'), and fixing a few minor formatting differences.&amp;nbsp; VB.NET also lets you 'declare' and 'set' the value of a variable naturally in one line, while VBA generally requires 2 lines, so equivalent vb.net code is often shorter, or requires less code.&amp;nbsp; There are too many differences &amp;amp; benefits to mention here.&amp;nbsp; VB.NET offers many new features/functionality that were not present back in the VBA era also (&lt;A href="https://en.wikipedia.org/wiki/Comparison_of_Visual_Basic_and_Visual_Basic_.NET" target="_blank" rel="noopener"&gt;Link&lt;/A&gt;).&amp;nbsp; If you look at the history of VB.NET versions, there have been several 'branches' of it (Standard, Framework, Core), and lots of versions over the years, so it is not actually that 'new', just 'newer' than VBA.&amp;nbsp; In fact, even the VB part of VB.NET may be nearing the end of its 'new development' phase, while other forms of the&amp;nbsp;&lt;A href="https://en.wikipedia.org/wiki/.NET" target="_blank" rel="noopener"&gt;.NET&lt;/A&gt;&amp;nbsp;platform (C#.Net, F#.NET, ASP.NET, and others) will likely continue forward if VB development ceases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, it is common or forum users to post the final code that they consider to be 'the solution' to their question/problem/challenge.&amp;nbsp; However, it is not necessarily required.&amp;nbsp; If your finished/final code contains anything that is personal/confidential/proprietary, then you can either change that part to something 'neutral/generic' or simply not post it at all, if you do not want to.&amp;nbsp; It is relatively common for someone else to find a forum topic that closely matches what they are searching for, and appears to be 'solved', but the 'solution' is not included in the forum discussion, so they may ask for the solution to be posted, so they can benefit from it, or use it themselves to further their own automation goals.&amp;nbsp; Some folks may simply not visit this forum much after asking a question and getting an answer though, so it is very possible that the 'solution' may never get posted, it was not posted originally.&amp;nbsp; And yes, if you eventually post your own 'solution' to your own question/problem/challenge, it is OK to mark your own forum reply as the/a 'accepted solution'.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2024 15:03:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13152890#M173995</guid>
      <dc:creator>WCrihfield</dc:creator>
      <dc:date>2024-11-15T15:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: automate re-referencing files with lost reference</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13176639#M174367</link>
      <description>&lt;P&gt;for everyone interested in my solution i came up with. its part german part english, sry.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;Sub ResolveParts()

    Dim oFile As Inventor.file
    Dim oFileDescriptor As FileDescriptor
    Dim oFileDescriptor2 As FileDescriptor
    Dim oFileName As String
    Dim PartNumber As String
    Dim SearchFolder As String
    Dim FileFound As String
    Dim FileFoundTemp As String
    Dim FileType As String
    Dim RefMis As String
    Dim Response As VbMsgBoxResult
    Dim MacroRun As VbMsgBoxResult
    Dim pos As Integer
    Dim MatchingFiles() As String
    Dim FileIndex As Integer
    Dim test As String
    Dim UnresolvedParts() As String ' Array for unresolved parts
    Dim UnresolvedIndex As Integer
    Dim MaxUnresolved As Integer
    Dim UnresolvedIndex2 As Integer
    Dim FoundButUnresolvableParts() As String ' Array for parts that were found but could not be resolved
    Dim FoundButUnresolvableIndex As Integer
    Dim ReferenceCount As Integer
    Dim CurrentPartCounter As Integer
    
    Dim oDoc As Document
    Dim oApp As Application
    
    Set oApp = ThisApplication
    Set oDoc = oApp.ActiveDocument
    
    ' Set the search folder path
    SearchFolder = "C:\BigFolder"

    ' Initialize array for unresolved parts
    ReDim UnresolvedParts(0)
    UnresolvedIndex = 0
    MaxUnresolved = 50 ' Maximum unresolved parts before displaying a message
    ' Initialize array for parts that were found but not resolvable
    ReDim FoundButUnresolvableParts(0)
    FoundButUnresolvableIndex = 0
    CurrentPartCounter = 0
    
    If Not oDoc.DocumentType = kPartDocumentObject Then
    
    
replaceit:
    
        ' Get the active document's file reference
        If oDoc.DocumentType = kDrawingDocumentObject Then
            Set oFile = oDoc.ReferencedDocuments(1).file
        Else
            Set oFile = oDoc.file
        End If
        
        ' Loop through each referenced file descriptor
    
        For Each oFileDescriptor In oFile.ReferencedFileDescriptors
    
            ' Check if the reference is missing
            If oFileDescriptor.ReferenceMissing Then
                
                If Not nrr = 1 Then
                    nrr = 1
                    
                    For Each oFileDescriptor2 In oFile.ReferencedFileDescriptors

                        If oFileDescriptor2.ReferenceMissing Then
                        
                            ReferenceCount = ReferenceCount + 1
                                                        
                        End If
                        
                    Next
                    
                    MacroRun = MsgBox("Would you like to try resolving the " &amp;amp; ReferenceCount &amp;amp; " parts?", vbYesNo + vbQuestion)
                    
                End If
                
                If MacroRun = vbYes Then
                
                    RefMis = oFileDescriptor.FullFileName
                    RefMis = Right(RefMis, Len(RefMis) - InStrRev(RefMis, "\"))
                    
                    ' Extract the part number from the missing reference's filename
                    If InStr(RefMis, " ") &amp;gt; 1 Then
                        PartNumber = Left(RefMis, InStr(RefMis, " ") - 1)
                    ElseIf InStr(RefMis, "_") &amp;gt; 1 Then
                        PartNumber = Left(RefMis, InStr(RefMis, "_") - 1)
                    ElseIf InStr(RefMis, "-") &amp;gt; 1 Then
                        PartNumber = Left(RefMis, InStr(RefMis, "-") - 1)
                    Else
                        ' No delimiter found, skip this iteration
                        GoTo NextFileDescriptor
                    End If
                    
                    ' Initialize the array for storing potential matches
                    FileIndex = 0
                    ReDim MatchingFiles(0)
                    
                    ' Search for a matching file in the specified folder
                    FileType = Right(RefMis, 4)
                    FileFoundTemp = SearchFolder &amp;amp; PartNumber &amp;amp; "*" &amp;amp; FileType
                    FileFound = Dir(FileFoundTemp, vbNormal)
                    
                    ' Collect all matching files in the array
                    Do While FileFound &amp;lt;&amp;gt; ""
                        ReDim Preserve MatchingFiles(FileIndex)
                        MatchingFiles(FileIndex) = FileFound
                        FileIndex = FileIndex + 1
                        FileFound = Dir
                    Loop
                    
                    ' If potential matches were found, prompt the user
                    If FileIndex &amp;gt; 0 Then
                        CurrentPartCounter = CurrentPartCounter + 1
                        For FileIndex = LBound(MatchingFiles) To UBound(MatchingFiles)
                            Response = MsgBox("The missing part was found:" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; _
                                              RefMis &amp;amp; " (old)" &amp;amp; vbCrLf &amp;amp; MatchingFiles(FileIndex) &amp;amp; " (new)" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; _
                                              "Would you like to resolve it?", _
                                              vbYesNoCancel + vbExclamation, "Part " &amp;amp; CurrentPartCounter &amp;amp; "/" &amp;amp; ReferenceCount &amp;amp; " Resolve")
                            
                            If Response = vbYes Then
                                ' Resolve the reference using the selected file
                                On Error Resume Next
                                Dim TryCount As Integer
                                TryCount = 0
                                
                                Do
                                    Err.Clear
                                    oFileDescriptor.ReplaceReference SearchFolder &amp;amp; MatchingFiles(FileIndex)
                                    If Err.Number = 5 Then
                                        TryCount = TryCount + 1
                                        If TryCount &amp;gt;= 3 Then
                                            ' If not resolvable, add to array
                                            ReDim Preserve FoundButUnresolvableParts(FoundButUnresolvableIndex)
                                            FoundButUnresolvableParts(FoundButUnresolvableIndex) = RefMis
                                            FoundButUnresolvableIndex = FoundButUnresolvableIndex + 1
                                            MsgBox "Reference could not be replaced for: " &amp;amp; vbCrLf &amp;amp; RefMis, vbCritical
                                            Exit Do
                                        End If
                                    Else
                                        Exit Do ' Successfully replaced, exit loop
                                    End If
                                Loop
                                On Error GoTo 0
                                
                                Exit For
                            ElseIf Response = vbCancel Then
                                ' Cancel the entire operation if the user selects Cancel
                                MsgBox "Procedure cancelled.", vbCritical
                                Exit Sub
                            End If
                        Next FileIndex
                    Else
                        ' If no match was found, add to UnresolvedParts array
                        ReDim Preserve UnresolvedParts(UnresolvedIndex)
                        UnresolvedParts(UnresolvedIndex) = RefMis
                        UnresolvedIndex = UnresolvedIndex + 1
                        UnresolvedIndex2 = UnresolvedIndex2 + 1
                        
                        ' Check if 20 unresolved parts have been found
                        If UnresolvedIndex &amp;gt;= MaxUnresolved Then
                            MsgBox UnresolvedIndex2 &amp;amp; " unresolved parts were found:" &amp;amp; vbCrLf &amp;amp; Join(UnresolvedParts, vbCrLf), vbCritical, "Unresolved Parts"
                            UnresolvedIndex = 0
                        End If
                    End If
                End If
                
                If oDoc.DocumentType = kDrawingDocumentObject Then
                    oDoc.ReferencedDocuments(1).Update
                    oDoc.ReferencedDocuments(1).Update2
                End If
            End If
NextFileDescriptor:
        Next

        ' Ausgabe der verbleibenden nicht auflösbaren Teile
        If UnresolvedIndex &amp;gt; 0 Then
            MsgBox "Die folgenden Teile konnten nicht in" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; SearchFolder &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; "aufgelöst werden:" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; Join(UnresolvedParts, vbCrLf), vbInformation, "Nicht auflösbare Teile"
            Erase UnresolvedParts
            UnresolvedIndex = 0
            
        End If
        
        ' Ausgabe der Teile, die gefunden aber nicht auflösbar waren
        
        ' Zweiter Suchdurchgang in anderem Ordner
        If Not nr = 2 Then
            nr = 2
            SearchFolder = Left(oDoc.FullFileName, InStrRev(oDoc.FullFileName, "\"))
            GoTo replaceit
        End If
        
        If FoundButUnresolvableIndex &amp;gt; 0 Then
            MsgBox "Die folgenden Teile wurden gefunden, konnten aber nicht aufgelöst werden:" &amp;amp; vbCrLf &amp;amp; vbCrLf &amp;amp; Join(FoundButUnresolvableParts, vbCrLf), vbCritical, "Gefunden aber nicht auflösbar"
        End If
        
        ThisApplication.ActiveDocument.Update
        ThisApplication.ActiveDocument.Update2
        
    Else
    
        MsgBox "Bitte nur aus einer Baugruppe oder aus einer Zeichnung starten. Abgeleitete Baugruppen können nicht mit diesem Makro aufgelöst werden.", vbExclamation
        
    End If
        

End Sub&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Nov 2024 07:49:16 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-ilogic/automate-re-referencing-files-with-lost-reference/m-p/13176639#M174367</guid>
      <dc:creator>torben_schroederXDK86</dc:creator>
      <dc:date>2024-11-27T07:49:16Z</dc:date>
    </item>
  </channel>
</rss>

