Microsoft Excel

Herbers Excel/VBA-Archiv

Fensterausrichtung mit VBA | Herbers Excel-Forum


Betrifft: Fensterausrichtung mit VBA von: marspoki
Geschrieben am: 18.11.2009 10:35:28

Hallo Profis,

Ich möchte gerne mit Hilfe von VBA ein Programmfenster z.B. Outlook an eine ganz bestimmt Stelle am Bildschirm verschieben.
Mit dem Editor funktioniert der Unten aufgeführte Code wunderbar. Warum nicht mit Outlook oder anderen Programmen?
Habe natürlich die Zeilen verändert gehabt.

Vielen Dank

beste grüße

Masrpoki

Option Explicit

Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Private Declare Function MoveWindow Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal bRepaint As Long) As Long
Private Declare Function GetWindowRect Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByRef lpRect As RECT) As Long
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" ( _
ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Const GC_CLASSNAMENOTEPAD = "Notepad"

Public Sub test()

Dim hWnd As Long, lngReturn As Long
Dim udtRECT As RECT
Dim strClassName As String

AppActivate "Unbenannt - Editor", True

hWnd = GetForegroundWindow

If hWnd <> 0 Then

strClassName = Space(256)
lngReturn = GetClassName(hWnd, strClassName, 256)

If Left$(strClassName, lngReturn) = GC_CLASSNAMENOTEPAD Then
Call GetWindowRect(hWnd, udtRECT)
With udtRECT
Call MoveWindow(hWnd, 0, 0, .Right - .Left, .Bottom - .Top, -1)
End With
End If
End If

End Sub

  

Betrifft: versuche es mal hiermit. von: Tino
Geschrieben am: 21.11.2009 06:52:40

Hallo,

kommt als Code in Modul1

Option Explicit 
 
Private Declare Function SetForegroundWindow Lib "user32" ( _
  ByVal hwnd As Long) As Long 
   
Private Declare Function ShowWindow Lib "user32" ( _
  ByVal hwnd As Long, ByVal nCmdShow As Long) As Long 
   
Private Declare Function MoveWindow Lib "user32.dll" ( _
ByVal hwnd As Long, ByVal X As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long 
 
Private Declare Function GetWindowRect Lib "user32.dll" ( _
ByVal hwnd As Long, ByRef lpRect As RECT) As Long 
 
Private Type RECT 
    Left As Long 
    Top As Long 
    Right As Long 
    Bottom As Long 
End Type 
 
Const iNormal& = 1 
Const iMinimized& = 2 
Const iMaximized& = 3 
 
Sub Test() 
Dim LHwnd As Long 
Dim udtRECT As RECT 
 
 
LHwnd = Hwnd_Fenster("Microsoft Outlook") 
 
If LHwnd > 0 Then 
 
    SetForegroundWindow LHwnd 
    ShowWindow LHwnd, iNormal 
     
    GetWindowRect LHwnd, udtRECT 
     
    With udtRECT 
        MoveWindow LHwnd, 0, 0, .Right - .Left, .Bottom - .Top, -1 
    End With 
 
End If 
End Sub 
 
kommt als Code in Modul2
Private Declare Function GetDesktopWindow Lib "user32" () As Long 
 
Private Declare Function GetWindowTextLength Lib "user32" _
        Alias "GetWindowTextLengthA" (ByVal hwnd As Long) _
        As Long 
         
Private Declare Function GetWindowText Lib "user32" _
        Alias "GetWindowTextA" _
        (ByVal hwnd As Long, ByVal lpString As String, _
        ByVal cch As Long) As Long 
 
Private Declare Function GetWindowLong Lib "user32" Alias _
        "GetWindowLongA" (ByVal hwnd As Long, ByVal wIndx As _
        Long) As Long 
          
Private Declare Function GetWindow Lib "user32" _
       (ByVal hwnd As Long, ByVal wCmd As Long) As Long 
 
Const GWL_STYLE& = (-16) 
Const WS_VISIBLE = &H10000000 
Const WS_BORDER = &H800000 
Const GW_HWNDNEXT& = 2 
Const GW_CHILD& = 5 
 
Private Function GetWindowInfo(ByVal hwnd&, STitel$, Optional booVisible As Boolean = True) As Long 
Dim Result&, Style&, Title$ 
   
    'Darstellung des Fensters 
    Style = GetWindowLong(hwnd, GWL_STYLE) 
    Style = Style And (WS_VISIBLE Or WS_BORDER) 
     
    'Fensetrtitel ermitteln 
    Result = GetWindowTextLength(hwnd) + 1 
    Title = Space$(Result) 
    Result = GetWindowText(hwnd, Title, Result) 
    Title = Left$(Title, Len(Title) - 1) 
     
'prüfen ob Fenster Sichtbar 
If (Style = (WS_VISIBLE Or WS_BORDER)) Or booVisible = False Then 
      If Title Like "*" & STitel & "*" Then 
       GetWindowInfo = hwnd 
       Exit Function 
      End If 
End If 
GetWindowInfo = 0 
End Function 
 
 
Function Hwnd_Fenster(strTitel$) As Long 
Dim hwnd As Long 
 
  hwnd = GetDesktopWindow() 
  hwnd = GetWindow(hwnd, GW_CHILD) 
 
  GetWindowInfo hwnd, strTitel, True 
 
Do While hwnd <> 0 
    hwnd = GetWindow(hwnd, GW_HWNDNEXT) 
   If GetWindowInfo(hwnd, strTitel, True) = hwnd Then 
    Hwnd_Fenster = hwnd 
    Exit Function 
   End If 
Loop 
 
End Function 
 
Gruß Tino