<?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 Re: VBA Save and replace all instances of a part in Inventor Programming Forum</title>
    <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4827819#M125990</link>
    <description>&lt;P&gt;Well I finally got everything working. Found out how to save a copy of the selected part/assembly and replace all instances in the active assembly. So it won't affect sub-assemblies or parent assemblies if you are editing a sub-assembly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Public Sub Save_Replace_All()
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType &amp;lt;&amp;gt; kAssemblyDocumentObject) Then
        MsgBox "This is NOT an assembly document!", vbExclamation
        Exit Sub
    End If
    
    Dim oPart As ComponentOccurrence
    Dim opartdoc As Document
    Dim oPartdoc2 As Document
    Dim NewFileName As String
    Dim NewFilePath As String
    
    Set oPart = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the part to save and replace all.")
    Set opartdoc = oPart.Definition.Document
        
    NewFileName = InputBox("What is the new file name for " &amp;amp; filename_noext(opartdoc.FullFileName) &amp;amp; "?", "New File Name", filename_noext(opartdoc.FullFileName))
    NewFilePath = ThisApplication.FileLocations.Workspace &amp;amp; "\" &amp;amp; NewFileName &amp;amp; "." &amp;amp; file_ext(opartdoc.FullFileName)
    
    If (fileExists(NewFilePath)) Or (NewFileName = "") Then
        MsgBox ("Error! Either file exists or no filename given")
        Exit Sub
    End If

    Call opartdoc.SaveAs(NewFilePath, True)

    Dim fd As DocumentDescriptor
    For Each fd In ThisApplication.ActiveEditDocument.ReferencedDocumentDescriptors
        If (fd.FullDocumentName = opartdoc.FullFileName) Then fd.ReferencedFileDescriptor.ReplaceReference (NewFilePath)
    Next
End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Feb 2014 15:55:57 GMT</pubDate>
    <dc:creator>pball</dc:creator>
    <dc:date>2014-02-20T15:55:57Z</dc:date>
    <item>
      <title>VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4825685#M125986</link>
      <description>&lt;P&gt;I'm trying to make a script to replace all instances of a part/assembly in an assembly with a new copy of that file. So I'm trying to make an improved version of the "save and replace component" feature since it will only replace one part. I've created an ideastation post to have "save and replace component" be able to replace all instances of the part you replacing, but I want this feature in the mean time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far the script prompts you to select a part, enter a few filename, and does a check on the new file name. I need help with saving a new copy of the selected part and replacing all instances of the original part in the assembly. Is there a simple way to replace all instances or will it require a loop through all components and checking the filename then replacing ones that match, if so that's something I should be able to do. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Public Sub Save_Replace_All()
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType &amp;lt;&amp;gt; kAssemblyDocumentObject) Then
        MsgBox "This is NOT an assembly document!", vbExclamation
        Exit Sub
    End If
    
    Dim oPart As ComponentOccurrence
    Dim opartdoc As Document
    Dim oPartdoc2 As Document
    Dim NewFileName As String
    Dim NewFilePath As String
    
    Set oPart = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the part to save and replace all.")
    Set opartdoc = oPart.Definition.Document
        
    NewFileName = InputBox("What is the new file name for " &amp;amp; filename_noext(opartdoc.FullFileName) &amp;amp; "?", "New File Name", filename_noext(opartdoc.FullFileName))
    NewFilePath = ThisApplication.FileLocations.Workspace &amp;amp; "\" &amp;amp; NewFileName &amp;amp; "." &amp;amp; file_ext(opartdoc.FullFileName)
    
    If (fileExists(NewFilePath)) Or (NewFileName = "") Then
        MsgBox ("Error! Either file exists or no filename given")
        Exit Sub
    End If

    'needs to save a copy of opartdoc as newfilepath, found this in another script but the script editor doesn't like it
    opartdoc.SaveAs(newfilepath,False)&lt;BR /&gt;
    'This replaces the selected part but I need to replace them all
    Call oPart.Replace(NewFilePath, False)
End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2014 19:03:52 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4825685#M125986</guid>
      <dc:creator>pball</dc:creator>
      <dc:date>2014-02-19T19:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4826697#M125987</link>
      <description>&lt;P&gt;You only have to set the second parameter to TRUE. That's all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;oPart.Replace(NewFilePath, &lt;STRONG&gt;True&lt;/STRONG&gt;)&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Feb 2014 06:19:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4826697#M125987</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-02-20T06:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4827275#M125988</link>
      <description>I still get an error from the vba editor.&lt;BR /&gt;&lt;BR /&gt;Compile error:&lt;BR /&gt;Expected: =</description>
      <pubDate>Thu, 20 Feb 2014 12:30:36 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4827275#M125988</guid>
      <dc:creator>pball</dc:creator>
      <dc:date>2014-02-20T12:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4827819#M125990</link>
      <description>&lt;P&gt;Well I finally got everything working. Found out how to save a copy of the selected part/assembly and replace all instances in the active assembly. So it won't affect sub-assemblies or parent assemblies if you are editing a sub-assembly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Public Sub Save_Replace_All()
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType &amp;lt;&amp;gt; kAssemblyDocumentObject) Then
        MsgBox "This is NOT an assembly document!", vbExclamation
        Exit Sub
    End If
    
    Dim oPart As ComponentOccurrence
    Dim opartdoc As Document
    Dim oPartdoc2 As Document
    Dim NewFileName As String
    Dim NewFilePath As String
    
    Set oPart = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the part to save and replace all.")
    Set opartdoc = oPart.Definition.Document
        
    NewFileName = InputBox("What is the new file name for " &amp;amp; filename_noext(opartdoc.FullFileName) &amp;amp; "?", "New File Name", filename_noext(opartdoc.FullFileName))
    NewFilePath = ThisApplication.FileLocations.Workspace &amp;amp; "\" &amp;amp; NewFileName &amp;amp; "." &amp;amp; file_ext(opartdoc.FullFileName)
    
    If (fileExists(NewFilePath)) Or (NewFileName = "") Then
        MsgBox ("Error! Either file exists or no filename given")
        Exit Sub
    End If

    Call opartdoc.SaveAs(NewFilePath, True)

    Dim fd As DocumentDescriptor
    For Each fd In ThisApplication.ActiveEditDocument.ReferencedDocumentDescriptors
        If (fd.FullDocumentName = opartdoc.FullFileName) Then fd.ReferencedFileDescriptor.ReplaceReference (NewFilePath)
    Next
End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2014 15:55:57 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4827819#M125990</guid>
      <dc:creator>pball</dc:creator>
      <dc:date>2014-02-20T15:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4966812#M125991</link>
      <description>&lt;P&gt;Get a compile error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sub or Fnction not defined&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;filename_noext&lt;/P&gt;</description>
      <pubDate>Thu, 17 Apr 2014 08:36:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4966812#M125991</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-04-17T08:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4966904#M125992</link>
      <description>&lt;P&gt;And fileExists&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't have your function in the project. But it still works if I delete functions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It will be better if it uses&amp;nbsp;ThisApplication.CreateFileDialog.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Apr 2014 09:16:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4966904#M125992</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2014-04-17T09:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4985716#M125993</link>
      <description>&lt;P&gt;Well here are the couple of functions I use in this script if anyone is interested.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Also apparently that is my old version, since I did add a file save dialog afterwards.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Public Sub Save_Replace_All()
    On Error GoTo err:
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType &amp;lt;&amp;gt; kAssemblyDocumentObject) Then
        MsgBox "This is NOT an assembly document!", vbExclamation
        Exit Sub
    End If
    
    Dim oPart As ComponentOccurrence
    Dim opartdoc As Document
    Dim NewFileName As String
    Dim NewFilePath As String
    
    Set oPart = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the part to save and replace all.")
    If (oPart Is Nothing) Then Exit Sub
    Set opartdoc = oPart.Definition.Document

   'Create a new FileDialog object.
    Dim oFileDlg As FileDialog
    Call ThisApplication.CreateFileDialog(oFileDlg)

    'check file type and set dialog filter
    If opartdoc.DocumentType = kPartDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
    ElseIf opartdoc.DocumentType = kAssemblyDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    End If

    oFileDlg.InitialDirectory = ThisApplication.FileLocations.Workspace
    oFileDlg.filename = filename(opartdoc.FullFileName)
    oFileDlg.CancelError = True
    
    On Error Resume Next
    
    Call oFileDlg.ShowSave

    If err Then
        Exit Sub
    ElseIf oFileDlg.filename &amp;lt;&amp;gt; "" Then
        NewFilePath = oFileDlg.filename
    End If

    ThisApplication.SilentOperation = True
    Call opartdoc.SaveAs(NewFilePath, True)
    ThisApplication.SilentOperation = False

    Dim fd As DocumentDescriptor
    For Each fd In ThisApplication.ActiveEditDocument.ReferencedDocumentDescriptors
        If (fd.FullDocumentName = opartdoc.FullFileName) Then fd.ReferencedFileDescriptor.ReplaceReference (NewFilePath)
    Next
    
    Exit Sub
err:
    ThisApplication.SilentOperation = False
    On Error GoTo 0
    Resume
End Sub

Function filename_noext(spth As String)
    'Returns filename without extension from full path
    If spth &amp;lt;&amp;gt; "" Then
        filename_noext = Mid(spth, InStrRev(spth, "\") + 1, InStrRev(spth, ".") - InStrRev(spth, "\") - 1)
    Else:
        filename_noext = ""
    End If
End Function&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Apr 2014 12:04:25 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/4985716#M125993</guid>
      <dc:creator>pball</dc:creator>
      <dc:date>2014-04-24T12:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/9881929#M125994</link>
      <description>&lt;P&gt;I am trying to use this macro and its coming up with an error message that says compile error can't find project or library.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am very very very raw with macros and just trying to get this to work to save me some steps.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 20:24:44 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/9881929#M125994</guid>
      <dc:creator>Chad.Snyder8B8AB</dc:creator>
      <dc:date>2020-11-19T20:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: VBA Save and replace all instances of a part</title>
      <link>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/12364817#M125995</link>
      <description>&lt;P&gt;&lt;SPAN&gt;This idea has been implemented within Autodesk Inventor 2024.2. Please review the Inventor 2024.2 What's New article&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-A542F6DE-7FFC-4697-9565-0CB04CB8510B" target="_blank" rel="nofollow noopener noreferrer"&gt;here&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;For more information regarding how you may leverage&amp;nbsp;the feature, and please review this&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-E94DD7CF-AA05-4BDB-8547-E394C040802C" target="_blank" rel="nofollow noopener noreferrer"&gt;page&lt;/A&gt;&lt;SPAN&gt;. Special thanks to everyone who cast a vote for it.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Nov 2023 06:20:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/inventor-programming-forum/vba-save-and-replace-all-instances-of-a-part/m-p/12364817#M125995</guid>
      <dc:creator>Yijiang.Cai</dc:creator>
      <dc:date>2023-11-09T06:20:40Z</dc:date>
    </item>
  </channel>
</rss>

