Alltid ha Excel-fönstret aktiverat


Här visas en Windows API-baserad lösning för att förhindra att aktivera andra program än Excel, dvs att alltid ha Excel aktiverat.

För att få till en fullständig lösning krävs också att vi tar bort systemknapparna för Excelfönstret, dvs förhindra att användarna via dessa växlar aktivt programfönster eller avslutar Excel.

 

Option Explicit  

Declare Function SetWindowPos Lib "user32" _

      (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _

      ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _

      ByVal cy As Long, ByVal uFlags As Long) As Long

 

Private Declare Function FindWindow Lib "user32" _

      Alias "FindWindowA" (ByVal lpClassName As String, _

      ByVal lpWindowName As String) As Long

 

Const HWND_TOPMOST = -1

Const HWND_NOTOPMOST = -2

 

Sub XL_Aktiverat()

   Dim lnhWnd As Long

   Dim lnRes As Long

 

   lnhWnd = FindWindow("XLMAIN", vbNullString)

   lnRes = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, vbNull)

 End Sub

 

Sub Aterstalla()

   Dim lnhWnd As Long

   Dim lnRes As Long

 

   lnhWnd = FindWindow("XLMAIN", vbNullString)

   lnRes = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, vbNull)

 End Sub

 

För att förhindra att användarna använder sig av systemknapparna för att avsluta Excel eller minimera
Excel-fönstret används följande procedurer:

Option Explicit  

Private Declare Function FindWindowA Lib "user32" (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 MF_BYPOSITION As Long = &H400

Private Const MF_BYCOMMAND As Long = &H0

Private Const mlNUM_SYS_MENU_ITEMS As Long = 9

 

 Sub Ta_Bort_Systemmenyer()

   Dim lnHandle As Long

   Dim lnCount As Long

 

   On Error Resume Next  

   lnHandle = FindWindowA(vbNullString, Application.Caption)

 

   If lnHandle <> 0 Then

      For lnCount = 1 To mlNUM_SYS_MENU_ITEMS

         DeleteMenu GetSystemMenu(lHandle, False), 0, MF_BYPOSITION

      Next lnCount

   End If

 End Sub

 

Sub Aterstalla_Systemmenyer()

   Dim lnHandle As Long

    On Error Resume Next

    lnHandle = FindWindowA(vbNullString, Application.Caption)

    GetSystemMenu lHandle, True

End Sub