VBA Save and replace all instances of a part

VBA Save and replace all instances of a part

pball
Mentor Mentor
6,106 Views
8 Replies
Message 1 of 9

VBA Save and replace all instances of a part

pball
Mentor
Mentor

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.

 

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.

 

Public Sub Save_Replace_All()
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType <> 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 " & filename_noext(opartdoc.FullFileName) & "?", "New File Name", filename_noext(opartdoc.FullFileName))
    NewFilePath = ThisApplication.FileLocations.Workspace & "\" & NewFileName & "." & 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)
'This replaces the selected part but I need to replace them all Call oPart.Replace(NewFilePath, False) End Sub

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
Accepted solutions (1)
6,107 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

You only have to set the second parameter to TRUE. That's all.

 

oPart.Replace(NewFilePath, True)
0 Likes
Message 3 of 9

pball
Mentor
Mentor
I still get an error from the vba editor.

Compile error:
Expected: =
Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
0 Likes
Message 4 of 9

pball
Mentor
Mentor
Accepted solution

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.

 

Public Sub Save_Replace_All()
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType <> 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 " & filename_noext(opartdoc.FullFileName) & "?", "New File Name", filename_noext(opartdoc.FullFileName))
    NewFilePath = ThisApplication.FileLocations.Workspace & "\" & NewFileName & "." & 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

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
Message 5 of 9

Anonymous
Not applicable

Get a compile error:

 

Sub or Fnction not defined

 

filename_noext

0 Likes
Message 6 of 9

Anonymous
Not applicable

And fileExists

 

I don't have your function in the project. But it still works if I delete functions. 

 

It will be better if it uses ThisApplication.CreateFileDialog.

 

0 Likes
Message 7 of 9

pball
Mentor
Mentor

Well here are the couple of functions I use in this script if anyone is interested.


Also apparently that is my old version, since I did add a file save dialog afterwards.

 

Public Sub Save_Replace_All()
    On Error GoTo err:
    'Checks if open document is assembly
    If (ThisApplication.ActiveDocument.DocumentType <> 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 <> "" 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 <> "" Then
        filename_noext = Mid(spth, InStrRev(spth, "\") + 1, InStrRev(spth, ".") - InStrRev(spth, "\") - 1)
    Else:
        filename_noext = ""
    End If
End Function

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style
Message 8 of 9

Chad.Snyder8B8AB
Explorer
Explorer

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. 

 

I am very very very raw with macros and just trying to get this to work to save me some steps.

0 Likes
Message 9 of 9

Yijiang.Cai
Autodesk
Autodesk

This idea has been implemented within Autodesk Inventor 2024.2. Please review the Inventor 2024.2 What's New article here. For more information regarding how you may leverage the feature, and please review this page. Special thanks to everyone who cast a vote for it.

Thanks,
River Cai

Inventor Quality Assurance Team
Autodesk, Inc.
Email: River-Yijiang.Cai@autodesk.com
0 Likes