Show Desktop using VBA

pfarmer

Well-known Member
Joined
Jul 6, 2005
Messages
550
Is there a way via VBA to have the desktop items disappear?

The idea I have is when using an Excel form that no desktop items are visible so the user doesn't surf the net on a kiosk type of machine.

Perry
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
If you are part of a domain using group policy would be the easiest way to do this.
 
Upvote 0
If you are part of a domain using group policy would be the easiest way to do this.

I am not the administrator. One reason to simply make the desktop not visible is that you still need to keep certain things like Internet Explorer active since the form has some Web Links.

The current problem is the Kiosk is using my logon. I have multiple machines I logon to. In any case visited places can show up under my logon as it is. I figure a simple way may be to simply not show the desktop. Current I have Excel itself invisible as well as the close 'x' on the form invisible so it is hard to close the form without a little knowledge of what you are doing.

One other consideration is to force the form ontop. Surfing would not be nearly as fun in that situation.

Perry
 
Upvote 0
This code will make your form stay on top and not be closed!

So, to close the Form you need to do it by code. You can run your close code by, hot+key, trigger, button or others, but not the forms upper right close "X"


The idea is the user can only close the form through your guidance or program.

Note: Advanced users/programmer can get around this!



'*************** Must be Top of Form Module Code: Stop Form Close Option***************************
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Const SC_CLOSE As Long = &HF060
'*****************************************************************************************************************

Private Sub UserForm_Initialize()
'***************Stop Form Close Option Code Block***************************
'Do not change this code!
'Add this to any Form's Code Module
'to deactivate the Form's upper right CLOSE option!
Dim hWndForm As Long
Dim hMenu As Long

hWndForm = FindWindow("ThunderDFrame", Me.Caption)
hMenu = GetSystemMenu(hWndForm, 0)
DeleteMenu hMenu, SC_CLOSE, 0&
'************************************************************************************
End Sub



This is a sample close:

Sub myCloseForm()
'Standard module code, like: Module1.
'Close Form!

UserForm1.Hide
End Sub
 
Upvote 0
This code will make your form stay on top and not be closed!

So, to close the Form you need to do it by code. You can run your close code by, hot+key, trigger, button or others, but not the forms upper right close "X"


The idea is the user can only close the form through your guidance or program.

Note: Advanced users/programmer can get around this!



'*************** Must be Top of Form Module Code: Stop Form Close Option***************************
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Const SC_CLOSE As Long = &HF060
'*****************************************************************************************************************

Private Sub UserForm_Initialize()
'***************Stop Form Close Option Code Block***************************
'Do not change this code!
'Add this to any Form's Code Module
'to deactivate the Form's upper right CLOSE option!
Dim hWndForm As Long
Dim hMenu As Long

hWndForm = FindWindow("ThunderDFrame", Me.Caption)
hMenu = GetSystemMenu(hWndForm, 0)
DeleteMenu hMenu, SC_CLOSE, 0&
'************************************************************************************
End Sub



This is a sample close:

Sub myCloseForm()
'Standard module code, like: Module1.
'Close Form!

UserForm1.Hide
End Sub

Currently I use this in the userform, CommandButton4 has code to close the form.

I put a drop file on the Kiosk Machine which will disable the command button while allowing it to be enabled on any machine without the drop file.

Code:
Option Explicit

Private Const GWL_STYLE = (-16)
Private Const WS_SYSMENU = &H80000

Private Declare Function _
FindWindow Lib "User32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function _
GetWindowLong Lib "User32" Alias "GetWindowLongA" ( _
ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function _
SetWindowLong Lib "User32" Alias "SetWindowLongA" ( _
ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Declare Function _
DrawMenuBar Lib "User32" (ByVal hWnd As Long) As Long




Private Sub UserForm_Initialize()


Dim KioskDetection As String
Dim lStyle

Dim xl_hwnd

KioskDetection = ThisWorkbook.Worksheets("Setup").Range("c19")

If Dir(KioskDetection) <> "" Then

CommandButton4.Visible = False

Else

CommandButton4.Visible = True

End If


xl_hwnd = FindWindow(vbNullString, Me.Caption)
If xl_hwnd <> 0 Then
lStyle = GetWindowLong(xl_hwnd, GWL_STYLE)
lStyle = SetWindowLong(xl_hwnd, GWL_STYLE, lStyle And Not WS_SYSMENU)
DrawMenuBar xl_hwnd
End If

Then in the workbook open event I have this to launch the form and make Excel disappear:

Code:
Private Sub Workbook_Open()

Dim KioskDetection As String


KioskDetection = ThisWorkbook.Worksheets("Setup").Range("c19")

If Dir(KioskDetection) <> "" Then

Application.Visible = False

End If

UserForm1.Show vbModeless

End Sub

I then have two hidden labels that initially are set to visible = false and then are set to visible with the background color over another label making them invisible to the user. This allows one to make Excel visible if needed or when you want to close Excel in a clean fashion. I toggle these between visible and invisible as they sit in a region of the sheet which is used depending on the state of a couple of checkboxes. If a yes checkbox is true they are invisible, if false visible:

Code:
Private Sub Label63_Click()

Application.Visible = True

End Sub

Private Sub Label64_Click()

Application.Visible = False

End Sub
 
Upvote 0
Yes, I have used it that way, with: Application.Visible = False, but not with the two labels, I have used the CheckBox, label, hot-spot, Caption and button methods of doing the same.

So, did this work for you or do you still need help?
 
Upvote 0
Yes, I have used it that way, with: Application.Visible = False, but not with the two labels, I have used the CheckBox, label, hot-spot, Caption and button methods of doing the same.

So, did this work for you or do you still need help?

I may have been a little unclear. Basically I don't really want the desktop to disappear, I want the desktop icons to disappear. The idea is to keep users from manually running Internet explorer to surf the net while allowing some web links to exist in the userform. The form has links to various procedures that are provided on the Internet by a 3rd party which by license I believe would keep me from making local *.mht types of links.

Perry
 
Upvote 0
Set all connections to Cancel unless the URL is the one(s) indicated:


Private Sub object_BeforeNavigate( _
ByVal pDisp As Object, _
ByRef url As Variant, _
ByRef Flags As Variant, _
ByRef TargetFrameName As Variant, _
ByRef PostData As Variant, _
ByRef Headers As Variant, _
ByRef Cancel As Boolean)
 
Upvote 0
Is there a way via VBA to have the desktop items disappear?

The idea I have is when using an Excel form that no desktop items are visible so the user doesn't surf the net on a kiosk type of machine.

Perry

Ok,here is another way....But first let me remind you that if you try the following code do make sure you have all your data saved first in case for some reason you can't restore the hidden windows after the userform is closed !!

Be even more careful if you try editing the code for experimentation.!!

If things go wrong you will have to reboot the computer either by bringing the TaskBar Manager (Alt+Ctrl+Del) or by pressing the Windows Hot Key.

Here is a Workbook Demo : http://media3.uploadjar.com/file.php?file=uploads/clearscreen_1.xls

Proceedings:

-Create a userform with 3 Buttons, accept the default names and place this in its module :

( one button is for closing the Form, one is for hiding all windows and the last one if for recovering the hidden windows)


Code:
Private Sub CommandButton1_Click()
    Call Restore_AllWindows
End Sub

Private Sub CommandButton2_Click()
   Call Hide_AllWindows
End Sub

Private Sub CommandButton3_Click()
        If blnWndsHidden Then Call Restore_AllWindows
        Unload Me
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If blnWndsHidden Then Call Restore_AllWindows
End Sub

-Place this in Standard Module and Run the TEST Procedure :

Code:
Option Base 1
Option Explicit
 
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long

Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Declare Function SetActiveWindow Lib "user32.dll" (ByVal hwnd As Long) As Long

Const SW_HIDE = 0
Const SW_SHOW = 5

Public blnWndsHidden As Boolean
Dim objUF As UserForm1
Dim Arr() As Variant
Dim intCounter As Integer


Sub Hide_AllWindows()
    'don't hide wndws more than once
    If Not blnWndsHidden Then
        'initiate counter & Arr and cycle thru all Top level Windows
        intCounter = 1
        Erase Arr()
        EnumWindows AddressOf EnumWindowsProc, ByVal 0
        'reset flag
        blnWndsHidden = True
     Else
        MsgBox "Windows Already Hidden !  ", vbInformation
    End If
End Sub


Sub Restore_AllWindows()
    Dim i As Integer
    If blnWndsHidden Then
        'retrieve all window handles from array
        'and dislay them
        For i = LBound(Arr) To UBound(Arr)
            ShowWindow Arr(i), SW_SHOW
        Next
        'reset flag
        blnWndsHidden = False
        SetActiveWindow Application.hwnd
    Else
        MsgBox "Windows Already Restored !  ", vbInformation
    End If
End Sub


Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
    'don't hide Userform!
    If hwnd <> FindWindow(vbNullString, objUF.Caption) Then
        'look for visible windows only, store their HWNDs
        'and then hide them
        If IsWindowVisible(hwnd) Then
            ReDim Preserve Arr(intCounter)
            Arr(intCounter) = hwnd
            intCounter = intCounter + 1
            ShowWindow hwnd, SW_HIDE
        End If
    End If
    'next call
    EnumWindowsProc = True
End Function



Sub Test()
    Set objUF = New UserForm1
    objUF.SHOW
    Set objUF = Nothing
End Sub

Basically, the code just hides each and every single item on the screen, leaving the user with the UserForm Only.

Regards.
 
Upvote 0
Is there a way via VBA to have the desktop items disappear?

The idea I have is when using an Excel form that no desktop items are visible so the user doesn't surf the net on a kiosk type of machine.

Perry

Ok,here is another way....But first let me remind you that if you try the following code do make sure you have all your data saved first in case for some reason you can't restore the hidden windows after the userform is closed !!
................

Basically, the code just hides each and every single item on the screen, leaving the user with the UserForm Only.

Regards.

This was pretty much exactly what I was after!

I got it working but to the Public Function EnumWindowsProc I had to add a set statement or it errored. Not sure why since the example book works fine, only when I add the code to mine does it fail. I can leave out the set statement and just specifiy Userform1.Caption:

Code:
Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Set objUF = UserForm1
    'don't hide Userform!
    If hwnd <> FindWindow(vbNullString, objUF.Caption) Then
        'look for visible windows only, store their HWNDs
        'and then hide them
        If IsWindowVisible(hwnd) Then
            ReDim Preserve Arr(intCounter)
            Arr(intCounter) = hwnd
            intCounter = intCounter + 1
            ShowWindow hwnd, SW_HIDE
        End If
    End If
    'next call
    EnumWindowsProc = True
End Function

Perry

[/code]
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,693
Members
449,117
Latest member
Aaagu

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