Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Run-Time Error 91

8 REPLIES 8
Reply
Message 1 of 9
alyssaweaver
425 Views, 8 Replies

Run-Time Error 91

I haven't marked my last answer as solved yet, since I'm not sure it's solved. I'm getting a run-time error 91 on this code and I'm not sure why. Does anyone have any ideas? I'm thinking it might be related to the Inventor aspect, not the general code. I put the place of the error in bold, underlined, italics, and in red font.

 

Option Explicit
Public i As Integer


    Public Function ReplaceComponent()
    
    
    Dim NameStr As String
    Dim NewNamePath As String
    
    Dim NameStr2 As String
    Dim OldNamePath As String

    
    NameStr = Renamer.New_Name.Text
    NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"
    
        
    NameStr2 = Renamer.Old_Name_Display.Text
    OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"


    Dim oOccurrence As ComponentOccurrence
    Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath
    oOccurrence.Replace NewNamePath, True
    
    Do While i < 99
    i = i + 1

    Loop
    
    End Function

 

Best Wishes,
Ali

|---------------------------------------------------------------------------------------------------------------------|
"It's a screwdriver, not a water pistol! What are you gonna do? Construct a cabinet at them?!"
8 REPLIES 8
Message 2 of 9
rjay75
in reply to: alyssaweaver

Set oOccurrence = ThisApplication.ActiveDocument.ComponentDefinition.Occurrences.OldNamePath

This code has an error. It will not give you the occurence with the old name. To get that you would need to:

 

Sub ReplaceComponent()
Dim NameStr As String
Dim NewNamePath As String

Dim NameStr2 As String
Dim OldNamePath As String

For i = 0 To 99 Step 1
NameStr = Renamer.New_Name.Text
NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"

NameStr2 = Renamer.Old_Name_Display.Text
OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"

Dim oOccurrence As ComponentOccurrence
For Each oOcc As ComponentOccurrence in ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
Set oOccurrence = oOcc
Exit For
End If
Next oOcc

'Then you can replace
oOccurrence.Replace NewNamePath, True
Next i
End Sub

 

 This will loop through the Occurrences in the document and when it finds a match to OldNamePath it sets a reference to the occurrence and exits the loop. After replace all occurences with the new path.

 

Edited to show in context of full subroutine.

Message 3 of 9
alyssaweaver
in reply to: rjay75

This returns an "Expected : In" error

Best Wishes,
Ali

|---------------------------------------------------------------------------------------------------------------------|
"It's a screwdriver, not a water pistol! What are you gonna do? Construct a cabinet at them?!"
Message 4 of 9
alyssaweaver
in reply to: rjay75

Thank you so much for helping with this 🙂 I really appreciate it. I'm very excited to be almost done with this project!!!

Best Wishes,
Ali

|---------------------------------------------------------------------------------------------------------------------|
"It's a screwdriver, not a water pistol! What are you gonna do? Construct a cabinet at them?!"
Message 5 of 9
alyssaweaver
in reply to: rjay75

Updated code, with strikethrough through portion that needs to be deleted. Now I'm getting an oOcc not defined message. Baby steps!

 

Sub ReplaceComponent()
Dim NameStr As String
Dim NewNamePath As String

Dim NameStr2 As String
Dim OldNamePath As String

For i = 0 To 99 Step 1
NameStr = Renamer.New_Name.Text
NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"

NameStr2 = Renamer.Old_Name_Display.Text
OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"

Dim oOccurrence As ComponentOccurrence
For Each oOcc As ComponentOccurrence In ThisApplication.ActiveDocument.ComponentDefinition.Occurrences
If oOcc.ReferencedDocumentDescriptor.FullDocumentName = OldNamePath Then
Set oOccurrence = oOcc
Exit For
End If
Next oOcc

oOccurrence.Replace NewNamePath, True
Next i
End Sub
Best Wishes,
Ali

|---------------------------------------------------------------------------------------------------------------------|
"It's a screwdriver, not a water pistol! What are you gonna do? Construct a cabinet at them?!"
Message 6 of 9
rjay75
in reply to: alyssaweaver

Here's the iLogic code to do what you want. It will prompt you for the name portion to replace in the file names to rename them.

 

Create a new iLogic rule and paste this code into it.

 

Imports System.IO

Sub Main()

	'Check to make sure this is an assembly document
	If Not ThisDoc.Document.DocumentType = kAssemblyDocumentObject Then
		MessageBox.Show("Must run from an assembly")
		Exit Sub
	End If

	Dim oldName As String
	Dim newName As String
	
	'Get Portion of name to replace
	oldName = InputBox("Enter the old name:", "Replace References", "")
	'Get new name to replace old portion with
	newName = InputBox("Enter the new name:", "Replace References", "")
	
	'Check to make sure you have an old and new name portion
	If String.IsNullOrEmpty(oldName) Or String.IsNullOrEmpty(newName) Then
		MessageBox.Show("Must enter an old and new name")
		Exit Sub
	End If
	
	Dim occRef As ComponentOccurrence
	
	'Begin Replacing
	ReplaceRefs(ThisDoc.Document.ComponentDefinition.Occurrences, oldName, newName)
End Sub

Sub ReplaceRefs(curOccs As ComponentOccurrences, oldName As String, newName As String)
	'Loop Through All Occurrences in Assembly
	For Each oOcc As ComponentOccurrence In curOccs
		'If SubAssembly Replace its SubComponent
		If oOcc.SubOccurrences.Count > 0 Then
			ReplaceRefs(oOcc.SubOccurrences, oldName, newName)
		End If
		
		'Check to make sure occurence is not a virtual part
		If Not oOcc.ReferencedDocumentDescriptor Is Nothing Then
			Dim fullName As String
			Dim oldFileName As String
			Dim newFileName As String
			Dim filePath As String
			Dim newFullName As String
			
			'Get Current Occurrence FileName
			fullName = oOcc.ReferencedDocumentDescriptor.FullDocumentName
			'Get Directory Location of File
			filePath = System.IO.Path.GetDirectoryName(fullName)
			'Get Old Filename
			oldFileName = System.IO.Path.GetFileName(fullName)

			'Check to see if old name contains portion Match
			If oldFileName.Contains(oldName) Then
				'Replace old portion with new portion
				newFileName = oldFileName.Replace(oldName, newName)
				'Create new Full FileName. (Assumes new file is in the same location as old file)
				newFullName = filePath & "\" & newFileName
				
				'Try and Replace with new file
				Try
					oOcc.Replace(newFullName, True)
				Catch
					'Show or Collect files that couldn't be found.
					'MessageBox.Show("Could not find file: " & newFullName)
				End Try
			End If
		End If
	Next oOcc
End Sub

 

Message 7 of 9
alyssaweaver
in reply to: rjay75

If only "oOcc" could work, I could modify it for my project! Thanks so much for that!

 

Best Wishes,
Ali

|---------------------------------------------------------------------------------------------------------------------|
"It's a screwdriver, not a water pistol! What are you gonna do? Construct a cabinet at them?!"
Message 8 of 9
rjay75
in reply to: alyssaweaver

Take out the portion that has the striked out portion. As ComponentOccurrence.

Message 9 of 9
alyssaweaver
in reply to: rjay75

I did. It opened Pandora's Box. I get all kinds of fun runtime and new for errors and variable not defined errors now 🙂

Best Wishes,
Ali

|---------------------------------------------------------------------------------------------------------------------|
"It's a screwdriver, not a water pistol! What are you gonna do? Construct a cabinet at them?!"

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report