0

I'm working on a project to detect if a user form is open and if not, close an excel program hidden in the background. The issue I'm having is the if statement in the Function Excel_Form_Close. The if statement works independently and does what it should do but when moved into the function area, the msgbox "Entered" triggers but the If statements do not. Why would the If statement be getting skipped during the load?

Sub Macro_Timer()
CATIA.Application.OnTime Now + TimeValue("00:00:10"), "Excel_Form_Close"

End Sub






Function Excel_Form_Close()

MsgBox "Entered Excel_Form_Close"

If Currentproject.Allforms("USER_FORM").IsLoaded = False Then
    Call APP_CLOSE(LIST_APP, LIST_FILE)
    MsgBox "Excel Closed"
    End
End If

If Currentproject.Allforms("USER_FORM").IsLoaded = True Then
    MsgBox "USER FORM CURRENTLY LOADED"
    Call Macro_Timer
End If


End Function


aaron
  • 81
  • 1
  • 10
  • Are the forms modal or modeless? Presumably if you're hunting down their state they're modeless, right? Not familiar with CATIA at all, but it seems the problem stems from treating all forms as global objects. The idiomatic way to track non-modal forms would be to handle their events (`Initialize`, `Activate`, `QueryClose`) and relay the information to the module that owns the form instance - but in order to do this you must have a class module that is responsible for owning a form instance (and handling its events), ...where and how are you showing the forms? – Mathieu Guindon Jul 28 '20 at 16:18
  • I'll try answering as best as I can. I'm not a computer guy and this is an engineering side project. The user form is triggered by a macro, it displays a pop up window that the user then fills in desired information. The excel file is in the background since certain drop down boxes being selected will automatically fill in other parts of the form based on material, size, etc. The user form is Modal, the excel file is hidden in the background and the only way they would know an excel file is open is if they open task manager. – aaron Jul 28 '20 at 16:41
  • *Modeless* forms are tricker, because execution continues on the next line immediately after `.Show vbModeless` runs - and if the form isn't being instantiated and instead its default instance is made stateful and is shown, then that's when you need to use the global state to track what's opened and what isn't. Modeless forms quickly get very complicated and bug-prone when implemented through global state. – Mathieu Guindon Jul 28 '20 at 16:51
  • I think I understand what you're saying and it does make sense. The code executes until the next command is given so having a stationary command that's variable and doesn't officially end will probably confuse it if it's in Modal form. – aaron Jul 28 '20 at 18:49

1 Answers1

0

maybe bcs the end function you have on the first if statement completely shutdowns your code if no necessity so ever, you could also add only one if statement in ur function after all there is only two possibilities. Here is the possible fix.

Function Excel_Form_Close()

MsgBox "Entered Excel_Form_Close"

If Currentproject.Allforms("USER_FORM").IsLoaded = False Then
    Call APP_CLOSE(LIST_APP, LIST_FILE)
    MsgBox "Excel Closed"
else
    MsgBox "USER FORM CURRENTLY LOADED"
    Call Macro_Timer
End If
Dharman
  • 30,962
  • 25
  • 85
  • 135
INGl0R1AM0R1
  • 1,532
  • 5
  • 16
  • That's what I thought but it doesn't trigger the message boxes either. – aaron Jul 28 '20 at 16:37
  • @aaron that doesn't add up, there's a `MsgBox` invoked in all execution paths. Are you running into any error (or is it being suppressed? `On Error Resume Next`?) – Mathieu Guindon Jul 28 '20 at 16:45
  • Nope, CATIA is screwy like this. It triggers the "Entered Excel_Form_Close" message box but it ignores the if. When I run the if independently and in line with the rest of the code, the if works though. – aaron Jul 28 '20 at 18:46