I'm working on a replace program from the VBA editor. What I'm trying to do now is tie this all together. A set of files is renamed. That part is most of my program. From there, it allows you to select your assembly using an open file dialog and click open. When you open it it uses a silent operation to skip the "Oh no! I lost your files!" box. Now I'm on my link resolve issue.
So what I have is say, a couple files named:
UglyPonies-001.ipt
UglyPonies-002.ipt
UglyPonies-003.ipt
I've now renamed them to:
PrettyPonies-001.ipt
PrettyPonies-002.ipt
PrettyPonies-003.ipt
and I've opened up:
PrettyPonies-004.iam
To identify these files, I have two modules called OldNameiLoop and NewNameiLoop. It concatenates a file path adding on a suffix and adding one to it each time. Originally I had a MsgBox set up to display this file name, but I've now deleted it. I will post the codes below. So what I'm trying to do now is basically tell Inventor to replace the component OldNamePath with NewNamePath and then add i + 1 to the prefix name and do that same with that path. Anyone have any idea of how to tell Inventor to do that?
OldNameiLoop:
Option Explicit Public Sub OldNameiLoop() Dim i As Double Dim NameStr2 As String Dim OldNamePath As String NameStr2 = Renamer.Old_Name_Display.Text OldNamePath = NameStr & "-" & Right("00" & i, 3) & ".ipt" Do While i < 99 i = i + 1 If 'Something Happens Here' Then '3-character string created by using the Right() function Next i Else: Exit Sub End If Loop End Sub
NewNameiLoop:
Option Explicit Public Function NewNameiLoop() Dim i As Double Dim NameStr As String Dim NewNamePath As String NameStr = Renamer.New_Name.Text NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt" Do While i < 99 'Counts with the file name up to -099 i = i + 1 If 'Something happens here' Then Loop Else: Exit Function End If End Function
ReplaceComponent:
Option Explicit Public Function ReplaceComponent() Dim oOccurrence As ComponentOccurrence Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath oOccurrence.Replace NewNamePath, True End Function
This all links to UserForm3 when the user clicks "Open":
Private Sub Open_Button_Click() ThisApplication.SilentOperation = True 'Suppresses the resolve links dialog Dim myPath As String myPath = FileName.Text 'Gets the string, FileName, from module 1 Dim Shell As Object Set Shell = CreateObject("Shell.Application") Shell.Open (myPath) 'Opens selected file Resolve_and_Open.Hide 'Hides module ReplaceComponent End Sub
Solved! Go to Solution.
Solved by adam.nagy. Go to Solution.
I am going to post my code in case anyone has any ideas and would like to try them. Here is the .ivb, attached. I left the Module in question as "Module1"
John Hurt (War Doctor) - Doctor Who, my gf's fav show. 😉 you picked the right quote.
You're planning to continue this in a "Doctor Who" thread? 😉
I've looked at the code and the problem seems to be that there is no ActiveDocument at the moment of the ReplaceReference function being called.
I only provided that code to give an idea about what to do.
You probably know better the logic of your application, but basically you would need to open up the assembly file:
Dim doc As Document Set doc = ThisApplication.Documents.Open(Resolve_and_Open.FileName, False) ' you can open it invisible too Do While i < 99 Dim f As File 'Set f = ThisApplication.ActiveDocument.File Set f = doc.File Dim fd As FileDescriptor For Each fd In f.ReferencedFileDescriptors If fd.FullFileName = OldNamePath Then fd.ReplaceReference (NewNamePath) End If Next Loop doc.Save doc.Close
Cheers,
Hahaha not so sure I do know the logic of my own code. I'm so new to this, it's a wonder I've made it this far in the program.
That is indeed the problem. I've been working with rjay in this thread on that. He's recommending I basically change the workflow of the whole code. I would really rather not since I've been working on this for two months and would hate to scrap it, but I guess it happens sometimes.
Really the only problem left is that it isn't recognizing this line in his suggested code:
fd.ReplaceReference (NewFileName)
So that's unfortunate. I think the whole thing would work if that one line would work. Oh, the woes of VBA.