Code not working on Citrix

MUKESHY12390

Well-known Member
Joined
Sep 18, 2012
Messages
901
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
Hi All,

I have this code which works fine on "Local system Excel" but when I run same code with Citrix Excel ( open excel through Citrix App) it doesn't work, at end I get "Out of Memory"

anyone knows why this happen.????

Code:
Option Explicit
Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Long, ByVal fInherit As Long, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Const DESKTOP_SWITCHDESKTOP As Long = &H100
Public p_lngHwnd As Variant
Public p_lngRtn As Variant
Public p_lngErr As Variant
Public system As Variant


Function Check_If_Locked() As String
'    Dim p_lngHwnd As Long
'    Dim p_lngRtn As Long
'    Dim p_lngErr As Long
'    Dim system As String
    p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)
    If p_lngHwnd = 0 Then
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        p_lngErr = Err.LastDllError


        If p_lngRtn = 0 Then
            If p_lngErr = 0 Then
                system = "Away"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
    End If
    Check_If_Locked = system
End Function


Private Sub Form_Timer()
Dim i As Long


For i = 1 To 5
   Sheet1.Cells(i, 1) = Check_If_Locked
Next i


End Sub
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Q1: What version of Excel is running on the Citrix host?

If it is using an older version then it might have a smaller memory allocation than your local version.

Q2: Are you able to see the condition of the Citrix Host system? Is it actually running out of memory?

If it is not actually running out of memory and the memory limit of excel has not been reached then you might be seeing a rare error. There are cases where an error will cause Excel to report that it is out of memory when it is not. This is the last thing we will check for after you have answered the first two questions.

Read this article "Excel specifications and limits":
https://support.office.com/en-us/ar...d-limits-1672b34d-7043-467e-8e27-269d656771c3
 
Last edited:
Upvote 0
thanks for quick reply!

Q1 : MS Excel 2010/14.0 both Excel (local and citrix)
Q2 :Are you able to see the condition of the Citrix Host system? I'm not sure about this

the file which I use has only above peace of code that's it. no other things

Out of Memory comes - when macro execute "
End Sub" line.
 
Last edited:
Upvote 0
Strange. Your code is pretty simple. The one thing I would change is those public variants. I can see that they used to be declared properly within the function "Check_If_Locked". I would switch it back. That would save you a little memory.

Does it run like this?

Code:
Option Explicit
Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Long, ByVal fInherit As Long, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Const DESKTOP_SWITCHDESKTOP As Long = &H100
'Public p_lngHwnd As Variant
'Public p_lngRtn As Variant
'Public p_lngErr As Variant
'Public system As Variant

Function Check_If_Locked() As String
    Dim p_lngHwnd As Long
    Dim p_lngRtn As Long
    Dim p_lngErr As Long
    Dim system As String
    
    p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)
    If p_lngHwnd = 0 Then
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        p_lngErr = Err.LastDllError


        If p_lngRtn = 0 Then
            If p_lngErr = 0 Then
                system = "Away"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
    End If
    Check_If_Locked = system
End Function

Private Sub Form_Timer()
Dim i As Long
    For i = 1 To 5
        Sheet1.Cells(i, 1) = Check_If_Locked
    Next i
End Sub

The next thing I would do is check or updates. Follow this guide:

https://support.office.com/en-us/ar...46de917611c5?ui=en-US&rs=en-US&ad=US&fromAR=1
 
Upvote 0
Upon closer inspection I added an error handler on your close function. This will attempt to close the handle for 30 seconds if it did not close. This will catch if you are leaving a handle open before moving on to the next attempt:

It does run on my computer.

Code:
Option Explicit
Private Declare Function SwitchDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Declare Function OpenDesktop Lib "user32" Alias "OpenDesktopA" (ByVal lpszDesktop As String, ByVal dwFlags As Long, ByVal fInherit As Long, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseDesktop Lib "user32" (ByVal hDesktop As Long) As Long
Private Const DESKTOP_SWITCHDESKTOP As Long = &H100
'Public p_lngHwnd As Variant
'Public p_lngRtn As Variant
'Public p_lngErr As Variant
'Public system As Variant

Function Check_If_Locked() As String
    Dim p_lngHwnd As Long
    Dim p_lngRtn As Long
    Dim p_lngErr As Long
    Dim system As String
    
    p_lngHwnd = OpenDesktop(lpszDesktop:="Default", dwFlags:=0, fInherit:=False, dwDesiredAccess:=DESKTOP_SWITCHDESKTOP)
    If p_lngHwnd = 0 Then
        system = "Error"
    Else
        p_lngRtn = SwitchDesktop(hDesktop:=p_lngHwnd)
        
        If p_lngRtn = 0 Then
            p_lngErr = Err.LastDllError
            If p_lngErr = 0 Then
                system = "Away"
            Else
                system = "Error"
            End If
        Else
            system = "Unlocked"
        End If
        p_lngHwnd = CloseDesktop(p_lngHwnd)
        
        If p_lngHwnd = 0 Then
            p_lngErr = Err.LastDllError
            MsgBox "Failed to release Desktop handle" & vbNewLine & p_lngErr, vbCritical, "ERROR"
            
            Dim waitTill As Date
            waitTill = Now() + TimeValue("00:30:00")
            While p_lngHwnd = 0
                DoEvents
                p_lngHwnd = CloseDesktop(p_lngHwnd)
                If Now() > waitTill Then p_lngHwnd = 54 'Timeout error and end the loop
            Wend
        End If
    End If
    Check_If_Locked = system
    
End Function

Private Sub Form_Timer()
Dim i As Long
    For i = 1 To 5
        Sheet1.Cells(i, 1) = Check_If_Locked
    Next i
End Sub
 
Upvote 0
thanks for reply,

still having same problem code works when I use local system's Excel or Access but on citrix it shows only "Unlocked" in it doesn't matter if system is locked.

any other suggestion.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top