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?

22 REPLIES 22
SOLVED
Reply
Message 1 of 23
alyssaweaver
1263 Views, 22 Replies

Run-Time Error 91?

This run-time error 91 doesn't make sense to me. Generally the issue is I didn't define something, but I've noted everything this time.

The error is on this line:

 

Set f = ThisApplication.ActiveDocument.File

 ThisApplication is defined towards the beginning. File is defined right before that line.

Here is my code:

    Option Explicit


    Public Sub ReplaceReference()

    Dim invApp As Inventor.Application
    Set invApp = ThisApplication

    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"
        If Err.Number <> 0 Then MsgBox "Error Found After NewNamePath:" & Err.Description
    Err.Clear

    NameStr2 = Renamer.Old_Name_Display.Text      'Concatenates the old file NAME
    OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"
        If Err.Number <> 0 Then MsgBox "Error Found After OldNamePath:" & Err.Description
    Err.Clear

    Do While i < 99

    Dim f As File
    Set f = ThisApplication.ActiveDocument.File
    Dim fd As FileDescriptor
        For Each fd In f.ReferencedFileDescriptors
            If fd.FullFileName = OldNamePath Then
                fd.ReplaceReference (NewNamePath)
            End If
        Next


            Loop

    End Sub

 I know run-time error 91 is common and seems silly, but I just don't know what is wrong with it.

Best Wishes,
Ali

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

Looks like the Do/While is out of sequence. Your current code looks like it's looking for files that match an old file name  and switchs the reference to the new file name. 

 

One issue is Your Do While i < 99 is in the wrong spot. You set the name up top but begin looping after ward. i never changes. So it could be a memory error as you are running in an inifinite loop. 

 

In your current code The Do While i < 99 should be like this.

 

   Option Explicit


    Public Sub ReplaceReference()

    Dim invApp As Inventor.Application
    Set invApp = ThisApplication

    Dim NameStr As String
    Dim NewNamePath As String

    Dim NameStr2 As String
    Dim OldNamePath As String

    Do While i < 99
        'Concatenates the full new file path
        NameStr = Renamer.New_Name.Text               
        NewNamePath = Renamer.Path_Text.Text & "\" + NameStr & "-" & Right("00" & i, 3) & ".ipt"
        If Err.Number <> 0 Then MsgBox "Error Found After NewNamePath:" & Err.Description
        Err.Clear
        'Concatenates the old file NAME
        NameStr2 = Renamer.Old_Name_Display.Text      
        OldNamePath = NameStr2 & "-" & Right("00" & i, 3) & ".ipt"
        If Err.Number <> 0 Then MsgBox "Error Found After OldNamePath:" & Err.Description
        Err.Clear

        Dim f As File
        Set f = ThisApplication.ActiveDocument.File
        Dim fd As FileDescriptor
        For Each fd In f.ReferencedFileDescriptors
            If fd.FullFileName = OldNamePath Then
                fd.ReplaceReference (NewNamePath)
            End If
        Next
    Loop

    End Sub

 While this may work there is some redunancies as well. Everytime it's going throught the For Each loop its looping through ever referenced file. In this case you're looping throug 99 times.

 

Here's an alternate version that loops through once working for any number of files.

   Option Explicit


    Public Sub ReplaceReference()

    Dim invApp As Inventor.Application
    Set invApp = ThisApplication

        Dim NameStr As String
        Dim NewNamePath As String

        Dim NameStr2 As String
        Dim OldNamePath As String

        'Concatenates the full new file path
        NameStr = Renamer.New_Name.Text               
        NewNamePath = Renamer.Path_Text.Text & "\"
        If Err.Number <> 0 Then MsgBox "Error Found After NewNamePath:" & Err.Description
        Err.Clear
        'Concatenates the old file NAME
        OldNamePath = Renamer.Old_Name_Display.Text      
        If Err.Number <> 0 Then MsgBox "Error Found After OldNamePath:" & Err.Description
        Err.Clear

        Dim sLen As Long
        Dim sep As Long
        Dim fileName as String
        Dim f As File
        Dim fd As FileDescriptor

        Set f = ThisApplication.ActiveDocument.File
        For Each fd In f.ReferencedFileDescriptors            
            sLen = Len(fd.FullFileName)
            sep = InStrRev(fd.FullFileName, "\")
            fileName = Right(fd.FullFileName, sLen - sep)

            If InStr(fileName, OldNamePath) = 1 Then
                fd.ReplaceReference (NewNamePath & Replace(fileName, OldNamePath, NameStr))
            End If
        Next

    End Sub

 This will get the new target directory and new prefix. Then it loops through the referenced files. If any file has a name beginning with the oldname it renames it replaces it with the new one.

 

Calling Replace("abcde-001.ipt", "abcde", "xyz") returns "xyz-001.ipt".

 

 

Message 3 of 23
alyssaweaver
in reply to: rjay75

Ah, perfect. Something seemed off about where it was placed but I couldn't figure it out. Going off of your second solution (thanks for that, by the way), I do still get the same error, but this will be great once this error is fixed!!! Thanks, Rodney!

Best Wishes,
Ali

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

2 quick questions. Do you have an assembly open when you run it? What version of Inventor are you running.
Message 5 of 23
alyssaweaver
in reply to: rjay75

Aha, that moved on to a new error! Yippee! 😄 I opened an assembly when I did it. This creates a problem though. I need it to run without the assembly document being open when the program is first initialized. The user is supposed to select and open the assembly in part 3 of the program... Hmmm..

 

And I'm running 2011 here.

 

Now I get on run-time error 5 on this:

fd.ReplaceReference (NewNamePath & Replace(fileName, OldNamePath, NameStr))

 

Baby steps, right? Haha

 

Best Wishes,
Ali

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

I added an "On Error Resume Next" to forego that problem (it seems to have worked....I think) but still having that new run-time 5 error now

Best Wishes,
Ali

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

Yet another update, it now runs, but it doesn't do anything. Replace reference should work, right?

Best Wishes,
Ali

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

Try this to see what is giving the error message

 

Change this line:

fd.ReplaceReference (NewNamePath & Replace(fileName, OldNamePath, NameStr))

to

Dim newFileName As String = NewNamePath & Replace(fileName, OldNamePath, NameStr)
fd.ReplaceReference (newFileName)

This will to see which is failing. The string replace function or the ReplaceReference function. It may also fail if newFileName is not a valid file.

Message 9 of 23
rjay75
in reply to: alyssaweaver

Also Remove the "On Error Resume Next", this just says skip and restart the loop if you get an error. So if it errors every time it will look like nothing happened.
Message 10 of 23
alyssaweaver
in reply to: rjay75

That's invalid syntax

Best Wishes,
Ali

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

Removed that. I'll also attach my code.

Best Wishes,
Ali

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

Got it to mock run through your code. And it worked ok with the final section looking like this.

 

        Dim sLen As Long
        Dim sep As Long
        Dim FileName As String
        Dim f As File
        Dim fd As FileDescriptor
        Dim Names As String

        'On Error Resume Next
        
        Set f = ThisApplication.ActiveDocument.File
        For Each fd In f.ReferencedFileDescriptors
            sLen = Len(fd.FullFileName)
            sep = InStrRev(fd.FullFileName, "\")
            FileName = Right(fd.FullFileName, sLen - sep)

            If InStr(FileName, OldNamePath) = 1 Then
                Dim NewFileName As String
                NewFileName = NewNamePath & Replace(FileName, OldNamePath, NameStr)
                fd.ReplaceReference (NewFileName)
                Names = Names & vbCrLf & NewFileName
            End If
        Next
        MsgBox Names

 I commented out the Error trapping because we want to know if there's an error here. I added a Names variable so I can get a list of names and display at the end. When run it will show a dialog of all the replaced names. You can temporarily leave the error trap turned on so you can see the list of names to verify them so it doesn't stop on every error.

 

The error 5 is on the ReplaceReference call. There 2 reasons why this call is failing.

1. The file name you are trying to replace the reference on does not exist. (The path could be incorrect. Or the name itself could be incorrect.) 

2. The file reference you are replacing is not the same as the one being replaced.

 

Try running it in an assembly that needs the references replaced with the new files existing already.

Message 13 of 23
alyssaweaver
in reply to: rjay75

It won't even compile because of the error 5...

 

I have such a document open too.

Best Wishes,
Ali

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

Comment out the line to replace the reference. If you added the lines to show a list of file names does the new names look correct?
Message 15 of 23
alyssaweaver
in reply to: rjay75

Yes

Best Wishes,
Ali

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

Do the replacement files exist at this point? If so were they created by just copying the originals?

Message 17 of 23
alyssaweaver
in reply to: alyssaweaver

Yes. Here is how the program works:

 

1. User runs program

2. The user clicks browse, which opens a folder dialog

3. In the background, a temporary folder called "Inventor Temp Folder" is created in the C: drive.

4. The contents of the user-selected folder are copied in to this temp folder

5. The user selects okay.

6. The user enters the old prefix of the files and the new one they want

7. The user selects okay

8. The user clicks "Browse for Assemblies", opening a file dialog limited to assembly searches

9. The user selects "Open" and the assembly opens

10. Files are replaced in the assembly with the new corresponding folders (tentative)

11. The temporary folder is deleted (tentative)

12. The assembly is saved and closed (tentative)

Best Wishes,
Ali

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

Would it be better if I made this a whole separate program? So, when the assembly is opened, it can set it to launch that program? Not sure how to do that, but could that fix these problems?

Best Wishes,
Ali

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

EDIT (won't let me):

 

After the user selects okay, the files in the original folder are renamed.

Best Wishes,
Ali

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

You're doing a manual copy design. A few notes and questions that I used to walk through the workflow.

 

1. User runs program

2. The user clicks browse, which opens a folder dialog

     c:\Project1

3. In the background, a temporary folder called "Inventor Temp Folder" is created in the C: drive.

     c:\Inventor Temp Folder

4. The contents of the user-selected folder are copied in to this temp folder

     c:\Project1 -> c:\Inventor Temp Folder (The files should be renamed at this point)

5. The user selects okay.

6. The user enters the old prefix of the files and the new one they want

     Prj1 , Prj2

7. The user selects okay

8. The user clicks "Browse for Assemblies", opening a file dialog limited to assembly searches

     Prj1.asm or Prj2.asm (Should be Prj2.asm)

9. The user selects "Open" and the assembly opens

10. Files are replaced in the assembly with the new corresponding folders (tentative)

     ReplaceReferences should run at this time point files to new location of c:\Inventor Temp Files\Prj2.....

11. The temporary folder is deleted (tentative)

     The temporary folder holds the renamed files. They can't be deleted or moved at this point.

12. The assembly is saved and closed (tentative)

      The assembly is closed and moved.

 

Since you don't have access to vault and would like to do this from Inventor here's an alternate workflow.

 

1. User opens assembly they wish to copy.

2. User does a SaveAs saving the assembly file to a new folder location.

3. Enters in Prefixes for the old and new files.

4. Programmatically open each referenced file and saveas the file to the new location and name. Close the referenced file. Replace the reference to the newly saved file name.

 

When I get a moment I can mock up what the saving/replace procedure looks like.

 

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

Post to forums  

Autodesk Design & Make Report