If you are part of a domain using group policy would be the easiest way to do this.
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
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
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
Private Sub Label63_Click()
Application.Visible = True
End Sub
Private Sub Label64_Click()
Application.Visible = False
End Sub
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?
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
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
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
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.
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