Kann man eine MSG Box positionieren, so das sie nicht mehr mitten im Bildschirm, sondern rechts mittig auftaucht?
Habe nichts weiter gefunden, wo man so was einstellen könnte.
Grüße Andi
Option Explicit
' 1.Teil - alles in ein Standart-Modul:
' MsgBOx an bestimmte Position
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As String) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, _
_
ByVal nIndex As Long) As Long
Public Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As _
Long
Public 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 wFlags As Long) As Long
Public Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Const GWL_HINSTANCE = (-6)
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5
Public hHook As Long
Public MsgBoxPosX As Integer
Public MsgBoxPosY As Integer
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Function WinProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
SetWindowPos wParam, 0, MsgBoxPosX, MsgBoxPosY, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or _
SWP_NOACTIVATE
UnhookWindowsHookEx hHook
End If
WinProc = False
End Function
Function msg(msgText As String, msgButton As Long, msgTitel As String, xPos As Long, yPos As _
Long)
Dim hInst As Long
Dim XLInst As Long
Dim Thread As Long
MsgBoxPosX = xPos
MsgBoxPosY = yPos
XLInst = FindWindow("xlmain", vbNullString)
hInst = GetWindowLong(XLInst, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, hInst, Thread)
MsgBox msgText, msgButton, msgTitel, 0, 0
End Function
' 2.Teil - Aufruf von überall:
Sub Aufruf_MsgBox()
Dim Dummy As Byte
'ButtonTypen:
'0 = OK
'1 = OK , Cancel
'2 = Abort , Ignore, Retry
'3 = Yes , No, Cancel
'4 = Yes , No
'5 = Retry , Cancel
'IconTypen:
'16 = Critical
'32 = Question
'48 = Exclamation
'64 = Information
'Syntax: msg(Text,Button(siehe oben),Titel,Position X-Achse,Position Y-Achse)
'Variablen: msg(as string, as Long, as String, as Long, as Long)
Dummy = msg("Ich bin Positioniert :-)", 4, "Achtung!", 5, 50)
End Sub
' Von Der Zugvogel (Mischa Reichelt mIsCHa)
Benutze eine UserForm ist einfacher.Um eine MSG Box in Excel zu positionieren, kannst du den folgenden VBA-Code verwenden. Dieser ermöglicht es dir, die Position der MSG Box individuell festzulegen.
Öffne den VBA-Editor: Drücke ALT + F11
in Excel.
Füge ein neues Modul hinzu: Klicke mit der rechten Maustaste auf einen der Einträge im Projekt-Explorer und wähle Einfügen
> Modul
.
Kopiere den folgenden Code in das Modul:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, ByVal _
lpWindowName As String) As Long
Public 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 wFlags As Long) As Long
Public MsgBoxPosX As Integer
Public MsgBoxPosY As Integer
Function msg(msgText As String, msgButton As Long, msgTitel As String, xPos As Long, yPos As Long)
MsgBoxPosX = xPos
MsgBoxPosY = yPos
MsgBox msgText, msgButton, msgTitel
SetWindowPos FindWindow(vbNullString, msgTitel), 0, MsgBoxPosX, MsgBoxPosY, 0, 0, 0
End Function
Sub Aufruf_MsgBox()
msg "Ich bin positioniert :-)", vbInformation, "Achtung!", 100, 200
End Sub
Führe das Makro aus: Drücke F5
, während du in der Sub Aufruf_MsgBox
bist, um die MSG Box an der definierten Position (in diesem Beispiel bei X=100 und Y=200) anzuzeigen.
Fehler: MSG Box erscheint nicht an der gewünschten Position
xPos
und yPos
im Aufruf_MsgBox
-Sub korrekt eingegeben sind. Überprüfe auch, ob der Code im richtigen Modul eingefügt wurde.Fehler: Fehler beim Ausführen des VBA-Codes
Datei
> Optionen
> Trust Center
> Einstellungen für das Trust Center
und aktiviere die Makros.Eine einfachere Möglichkeit, MSG Boxen zu positionieren, ist die Verwendung einer UserForm. Du kannst eine UserForm erstellen, die die gewünschte Positionierung und Gestaltung ermöglicht. Hierbei kannst du Steuerelemente wie Buttons und Textfelder hinzufügen, um eine benutzerfreundliche Oberfläche zu gestalten.
Hier ist ein Beispiel, wie du die MSG Box an der rechten Mitte des Bildschirms positionieren kannst:
Sub Beispiel_RechtsMitte()
Dim xPos As Long
Dim yPos As Long
xPos = Application.Width - 200 ' 200 Pixel von der rechten Seite
yPos = (Application.Height / 2) - 100 ' Mitte des Bildschirms
msg "Ich bin an der rechten Mitte positioniert :-)", vbInformation, "Information", xPos, yPos
End Sub
xPos
und yPos
, um die MSG Box genau dort zu positionieren, wo du sie möchtest.xPos
und yPos
dynamisch berechnen, um sie an verschiedene Bildschirmauflösungen anzupassen.Debug.Print
-Funktion, um die Werte von xPos
und yPos
während der Ausführung zu überwachen.1. Frage
Kann ich die MSG Box auch in Excel Online positionieren?
Antwort
Leider unterstützt Excel Online keine VBA-Makros, und somit ist das Positionieren von MSG Boxen nicht möglich.
2. Frage
Was ist der Unterschied zwischen einer MSG Box und einer UserForm?
Antwort
Eine MSG Box ist ein einfaches Dialogfeld, das Informationen anzeigt oder Benutzeraktionen abfragt, während eine UserForm eine umfassendere, anpassbare Oberfläche bietet, die mehrere Steuerelemente enthalten kann.
Die erweiterte Suchfunktion hilft dir, gezielt die besten Antworten zu finden
Suche nach den besten AntwortenEntdecke unsere meistgeklickten Beiträge in der Google Suche
Top 100 Threads jetzt ansehen