AW: Kleine Nachfrage
06.04.2023 14:22:13
volti
Hallo Wolfgang,
zugegebener Weise habe ich kaum Ahnung von Word, weil ich außer Schreiben fast nichts damit mache. Trotzdem habe ich mich aus Eigeninteresse mal daran gemacht Deinen Wunsch umzusetzen.
Leider läuft das nicht so wie bei Excel, weil
- Application.hwnd fehlt und damit das Word-Handle anderweitig ermittelt werden muss
- Es kein Event BeforeClose gibt, und damit kein Abbruch über Cancel gemacht werden kann.
Also habe ich es über eine Klassenprogrammierung probiert und bei mir klappt es jetzt. Probiere es einfach mal aus.
Falls jemand anderes Tipps zur Wordklassenprogrammierung für diesen Fall hat, =>her damit.
Code:
' ##### in ThisDocument #####
Dim X As New clsWord
Sub Document_Open()
Set X.appWord = Word.Application
End Sub
' ##### In ein Klassenmodul mit Namen clsWord #####
Public WithEvents appWord As Word.Application
Private Declare PtrSafe Function GetWindowRect Lib "user32" ( _
ByVal hWnd As LongPtr, lpRect As RECT) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function ScreenToClient Lib "user32" ( _
ByVal hWnd As LongPtr, lpPoint As POINTAPI) As Long
Private Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
y As Long
End Type
Private Sub appWord_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
Dim PT As POINTAPI, R As RECT
GetCursorPos PT ' Mausposition holen
ScreenToClient GetActiveWindow, PT ' Koordinaten umrechnen auf Fenster
GetWindowRect GetActiveWindow, R ' Maße des Fensters holen
If PT.X < R.Right And PT.X > (R.Right - 68) And _
PT.y > R.Top And PT.y < R.Top + 50 Then
MsgBox "Bitte nicht über das Systemkreuz beenden!", vbExclamation
Cancel = True: Exit Sub
End If
End Sub
_________________________
viele Grüße aus Freigericht 😊
Karl-Heinz