Intermittent VBA project errors within an assembly for other users, but not on the system it was created on

Lasma.smiukse
Explorer
Explorer

Intermittent VBA project errors within an assembly for other users, but not on the system it was created on

Lasma.smiukse
Explorer
Explorer

Hello everyone,
I have created an assembly of iParts that users can modify by selecting values from a VBA user form. Once values are selected a copy of assembly is saved and each iPart is replaced to a different iPart and the required member row is selected.

The process works on my computer, but all other users in our company experience intermittent errors with it. I created a basic error log to show me around which lines of VBA code the process fails. It seems like the issue might be around not accessing the files and user parameters within the assembly like they haven't been set or found.

I was thinking it could be due to the fact that the new assembly is opened invisibly and it has the same VBA project name as the original, but I couldn't understand why it works without any issues on my computer. I have tried to replicate the errors other users experienced (i.e., opening multiple different documents in Inventor that also have VBA projects with some code in it), but with no luck.

I am using Inventor Professional 2019 and have created the code within VBA editor. All the iParts required by the assembly are checked into Vault. The code is activated by an iLogic rule that calls a macro within the document. The macro runs through a module, that shows a VBA form. I created a function to ensure I am accessing the required VBA project within my current assembly, please see below:

 

Public Function AssyDocDef() As AssemblyDocument
    
    Const sProcName As String = "AssyDocDef"
    On Error GoTo ErrorHandler
    
    ' This should ensure that the code works even if there are multiple documents open
    ' and of different types
    Dim oDoc As Document
    Dim oDocDisplayName As String
    Dim oVBAProjectName As String
    
    On Error Resume Next
    For Each oDoc In ThisApplication.Documents
        ' should make sure this VBA project is actually within template document, not any other newly created assembly
        oVBAProjectName = oDoc.VBAProject.Name
        oDocDisplayName = oDoc.DisplayName
        
        If (oVBAProjectName = "ThisConfigVBAProject") And (oDocDisplayName = "This-Assembly-Template") Then
            Set AssyDocDef = oDoc
        Else
            ' This is not the template assembly document
        End If
    Next

    Dim oMsgBoxCritical As VbMsgBoxStyle
    oMsgBoxCritical = vbOKOnly & vbCritical

    If AssyDocDef Is Nothing Then
        Call Error_Handle(sProcName, "Custom", "The template assembly definition was not set.", _
        Err.Source, Erl)
        
        Call MsgBox("Inventor failed to set an assembly definition of the template assembly configurator." & _
        vbCrLf & "Please close the configurator, re-open and re-start it.", _
        oMsgBoxCritical, "Critical Error - Template Assembly Configurator")
        
        Exit Function
    Else
    End If
    
    Exit Function
ErrorHandler:
    Call Error_Handle(sProcName, Err.Number, Err.Description, Err.Source, Erl)

End Function

After assembly definition has been set, I am accessing user parameters and information selected within the form by the user to obtain new assembly name and names of parts to be replaced.

Could anyone please point me in the right direction how to ensure this works for all our users?

Thanks in advance!

 

 

0 Likes
Reply
Accepted solutions (1)
507 Views
2 Replies
Replies (2)

yan.gauthier
Advocate
Advocate
Accepted solution

Hi,

 

Ok I have several suggestions for you:

 

First, if an object is nothing, testing it will cause an error. There is a VBA trick you can use as a workaround :

 

If Not AssyDocDef Is Nothing Then
    'Testing like this will return false if object is nothing without creating an error
end if

 

Instead of using DisplayName, which can have a different value than the actual file name, use the FullFileName with the FileSystemObject from windows API:

 

dim FSO as object

set FSO = CreateObject("Scripting.FileSystemObject")

dim docName as string

docName = FSO.GetBaseName(oDoc.FullFileName) 'get file name without extension

 

I use this for my reference : https://ss64.com/vb/filesystemobject.html

 

Finally, handling Error with On Error Resume Next is ill advised, but if you need to do this. Bear in mind this will not clear your error :

 

On Error Resume Next

'something that could cause an error

if Err then
    msgbox "error message"
    err.clear
end if

 

you need to clear any errors otherwise, next time your code goes over On Error GoTo statement, it will fail right away

Lasma.smiukse
Explorer
Explorer

Hi,

 

Thank you very much for the solutions. I will try them out and hopefully it will work.

0 Likes