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.
Hi,
If the files are exactly the same, you only change their name then using ReplaceReference would be the best choice, you could even use it outside Inventor through Apprentice:
http://adndevblog.typepad.com/manufacturing/2012/08/replace-the-file-reference-by-inventor-api.html
So what you could do is:
1) Make copy of part files (e.g. using windows API) with the name you want
2) Open assemblies that use these parts
3) Use ReplaceReference to change their references to the new parts
4) You can delete the old part files
In this case Inventor would not come up with the message dialog about missing references.
Cheers,
Adam
'Define the open document Dim openDoc As Document openDoc = ThisDoc.Document 'Look at all of the files referenced in the open document Dim docFile As Document For Each docFile In openDoc.AllReferencedDocuments 'format file name Dim FNamePos As Long FNamePos = InStrRev(docFile.FullFileName, "\", -1) Dim docFName As String docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
Reverse Engineer the following code to do what you want it to do. Cheers!
@Adam
We don't have access to Apprentice here. Or vault.
Do you know how to solve the actual problem? Even your way does not solve the problem, as it still needs to be able to cycle through the program by according to their suffixes.
Because we don't have access to either of the programs listed above, I created a program that does just that using the VBA editor. It makes a copy of the specified folder in to my C:\ drive under "InventorTempFolder", which later gets deleted once the user clicks cancel or the entire operation finishes. It then uses a code that renames the suffixes according to the user input using the old path, the old prefix name, and a new prefix name. Finally, in form 3, the user has the opportunity to browse the assemblies based on the previously chosen directory. When the user clicks Open, it uses a silent operation to open the assembly without error messages. I will include some JPEGs of the program in action below.
Now what I need to do is be able to cycle through and replace the components. I just can't figure out what syntax I should use in my NewNameiLoop and OldNameiLoop to get it to cycle through properly. Once it is able to do this, I just need to fix a couple of bugs and it will be completely. I am considering posting the full code on this forum in the end, depending on if my company allows me too. I have seen several people with this problem.
can you .zip the test folder and attach it here for us to test?
With the ilogic implemented. It would make it so much easier for us to figure it out.
I haven't added in the code for SkipButton_Click yet, just a heads up
Hi Ali,
Did you mean to attach something to your last comment?
Apprentice is part of Inventor. See how it can be used: http://modthemachine.typepad.com/my_weblog/2010/03/iproperties-without-inventor-apprentice.html
Cheers,
This should be fairly updated, Adam. The VBA code is included as well.
Updated Module 4 Code (It still doesn't work, but it's made some progress):
Option Explicit Public i As Integer Public oOccurrence As ComponentOccurrence Public Sub ReplaceComponent() Dim NameStr As String Dim NewNamePath As String Dim NameStr2 As String Dim OldNamePath As String NameStr = Renamer.New_Name.Text 'Concatenates the full new file path NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt" NameStr2 = Renamer.Old_Name_Display.Text 'Concatenates the old file NAME OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt" 'Creates a ton of errors that have been giving me a headache Dim oOcc As ComponentOccurrence For Each oOcc In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then Set oOccurrence = oOcc End If Do While i < 99 oOcc.Replace NewNamePath, True Exit For If i = 99 Then DeletetheDirectory 'Will close the file Resolve_and_Open.Show vbModal 'Reopens form 3 to select the next assembly Else: For i = 1 To 99 Step 1 Next i End If Loop End Sub
New update, it seems the problem is stemming from "ComponentDefinition". I'm going to make a new thread about this one.
Hi Ali,
As I said, if the files are exactly the same, you just moved them or renamed them, then you could do it using ReplaceReference.
It should also be faster because in this case Inventor does not have to try to hook things up again with the new document.
And in that case you do not have to care about the ComponentDefinition either.
Somehting like this:
Dim f As File Set f = ThisApplication.ActiveDocument.File Dim fd As FileDescriptor For Each fd In f.ReferencedFileDescriptors ' This is based on the current location ' so if the file got renamed before the ' assembly was opened, then it might have been ' resolved to the wrong document If fd.FullFileName = OldNamePath Then fd.ReplaceReference (NewNamePath) End If Next
To avoid working with references pointing at the wrong files the easiest thing is if you keep the original documents until the end of the update process.
Cheers,
This gives me a run-time error '-2147467261 (80004003)' Method 'ThisApplication" of Object 'VbaApplication' failed...when I click help it tells me "Unable to Open Help". When I click help from there it opens help to explain to me what "Unable to Open Help" means...
I think I understand what you're saying about the replace references now, but do you know why it would give me that error? I've used "ThisApplication" several times with no problems until now.
Thanks,
Ali
So the helpfile was very helpful 🙂
This is the only thing I've found - not sure if it has anything to do with what you're doing:
If not, could you provide the steps how you are running the code?
Did you copy paste the code from the forum? Sometimes people had issues with funny characters in iLogic and parameter names when copying code form the net.
Maybe you could play around with removing spaces around the words?
Have a nice weekend!
So it changed its mind and is okay with that now. But now has a run-time error 91 object or with block variable not set starting on this line:
Set f = ThisApplication.ActiveDocument.File
If I delete the ".File" part and then put it right back, it moves on to the same error on this line:
For Each fd In f.ReferencedFileDescriptors
I see your note about it possibly resolving to the wrong location, and that could make sense, but it won't even compile
Then it does sounds like a character issue - e.g. the new line character is not correct or at the end of the word there is an invisible special character.
I think some people tried copying the whole code to notepad, clean it up if needed, then copy/paste it back into the iLogic editor.
Wait, aren't iLogic and VBA separate? Sorry, I'm still new to this 🙂
And I'll do that.
It's difficult to keep track of who is using what 🙂 - and I missed that you were using the "Set" keyword.
Yes, VBA and iLogic are different, but what you said about deleting a word and then putting it back suddenly makes the error disappear from a given line of code does seem to suggest that some funny invisible characters could be causing it.
Also I went on through Notepad and "cleaned it up". I got the same error again.
I feel like it's probably "ActiveDocument" causing it. So basically the same problem it had last week, but with a different way of telling me. I don't understand why though. "ActiveDocument" should be perfectly acceptable lingo