'Repeating Creating Object until it gets executed

I would like to repeat Creating Object until it gets created (opened).

In my case the problem looks like this. Application is opened via COM and access is controlled via licensing service. While all licenses are in use, you eventually will get an error as shown in attached image. But when free license will appear, you will be able to launch the application.

Dim ApplicationName As Object
Set ApplicationName = CreateObject("AppNameToLaunchViaCOM")

Is there any way to do it? And maybe inform the user how many times it failed to open etc.

Server Execution Failed window



Solution 1:[1]

You could try the following approach

Option Explicit
Public Declare PtrSafe Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As LongPtr)

Function getObj() As Object
    On Error GoTo EH
    
    Dim ApplicationName As Object
    Set ApplicationName = CreateObject("AppNameToLaunchViaCOM")
    
    Set getObj = ApplicationName
    
    Exit Function
    
EH:
    
End Function

Sub TryOut()
    Const MAX = 10
    
    Dim i As Long
    Dim myObj As Object

    Do
    
        Set myObj = getObj
        If Not myObj Is Nothing Then
            Exit Do
        End If
        
        ' Code to wait
        Sleep 1000
        
        ' exit loop in case more than max tries
        i = i + 1
    Loop Until i > MAX
    
    If myObj Is Nothing Then
        Debug.Print "No license"
        ' furher code
    Else
        Debug.Print "Tries", i
        ' furher code
    End If

End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Storax