Question

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.

Était-ce utile?

La solution

Could you check it like this? So can you determine which object is Nothing ... maybe it is ActiveDocument in your case?

If ThisApplication Is Nothing Then
    Err.Raise 91, , "ThisApplication is Nothing!"
Else
    If ThisApplication.ActiveDocument Is Nothing Then
        Err.Raise 91, , "ActiveDocument is Nothing!"
    Else
        If ThisApplication.ActiveDocument.File Is Nothing Then
            Err.Raise 91, , "File is Nothing!"
        End If
    End If
End If

Dim f As File
Set f = ThisApplication.ActiveDocument.File

Or just like this (to reduce nesting of ifs):

If ThisApplication Is Nothing Then
    Err.Raise 91, , "ThisApplication is Nothing!"
End If

If ThisApplication.ActiveDocument Is Nothing Then
    Err.Raise 91, , "ActiveDocument is Nothing!"
End If

If ThisApplication.ActiveDocument.File Is Nothing Then
    Err.Raise 91, , "File is Nothing!"
End If
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top