Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1072to1076
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Fenster aktivieren Appactivate oder anders

Fenster aktivieren Appactivate oder anders
05.05.2009 12:09:02
chris
Hallo Forumsbesucher und Excelexperten,
ich habe eine frage und würde mich sehr über Hilfe freuen.
Ich habe momentan vor über Excel ein fenster in den Vordergrund zu holen.
Das schaffe ich auch mit einem Makro...
Was ich nicht schaffe ist es das Fenster zu aktivieren so das der Cursor in dem Programm blinkt.
habe es MIt AppActivate ("fenstername) versucht aber es kommt immer ein fehler.
Obwohl ich genau den richtigen Fensternamen eingebe:(
Es handelt sich in dem Fall um ein SAP Fenster.
Bei vielen anderen geht es aber bei diesem nicht.
Auch wenn ich mit dem anderen Programm das fenster in den Vordergrund hole hat das fenster z.b den Namen "sap_fenstername" aber mit AppActivate gehts nicht :(
Um das fenster in den Vordergrund zu holen verwende ich so einen Code.
Hier ein teil davon.
Declare

Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare 

Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As  _
Long) As Long
Declare 

Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As  _
Long
Declare 

Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString  _
As String, ByVal cch As Long) As Long
Declare 

Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal  _
lpWindowName As String) As Long
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


Sub vordergrund()
cb = Cells(1, 1)
whandle = FindWindow(vbNullString, cb)
Call SetWindowPos(whandle, -1, 0, 0, 0, 0, 3)
'bis hier hin funktioniert es.
Sobald abeer AppActivate das fenster aktivieren soll kommt der Fehler.
AppActivate (cb)
End Sub


Würde mich sehr über Hilfe freuen.
Vielleicht weiß jemand eine möglichkeit über API ein fenster zu aktivieren.
Vielen Dank dafür im vorraus
gruß Christian

8
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Fenster aktivieren Appactivate oder anders
05.05.2009 12:21:23
Tino
Hallo,
hier mal ein Beispiel für den Internetexplorer.
Option Explicit

Private Declare Function SetForegroundWindow _
  Lib "user32" ( _
  ByVal hwnd As Long) As Long
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 ShowWindow Lib "user32" ( _
  ByVal hwnd As Long, _
  ByVal nCmdShow 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

Const iNormal& = 1
Const iMinimized& = 2
Const iMaximized& = 3

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


Sub Maximieren_Internet_Explorer()
Dim hwnd As Long
Dim STitel As String

STitel = "Internet Explorer" 'hier einen Teil vom Titel angeben 

  hwnd = GetDesktopWindow()
  hwnd = GetWindow(hwnd, GW_CHILD)
  
  '2. Param. Fenstertitel (nur ein Teil erforderlich) 
  '3. Param. optional True nur Sichtbare, False alle 
  GetWindowInfo hwnd, "Internet Explorer", True

Do While hwnd <> 0
    hwnd = GetWindow(hwnd, GW_HWNDNEXT)
   If GetWindowInfo(hwnd, STitel, True) = hwnd Then
    ShowWindow hwnd, iMaximized 'maximieren 
    SetForegroundWindow hwnd 'aktivieren 
   End If
Loop

End Sub


Gruß Tino

Anzeige
AW: Fenster aktivieren Appactivate oder anders
05.05.2009 12:37:55
Tino
Hallo,
war mal für was anderes gedacht, habe es nur erweitert.
die Zeile muss noch angepasst werden
mach aus
GetWindowInfo hwnd, "Internet Explorer", True
diese
GetWindowInfo hwnd, STitel, True
Gruß Tino
AW: Fenster aktivieren Appactivate oder anders
05.05.2009 13:44:22
chris
Perfekt.
Ist ja total super !
Danke Dir und schönen tag wünsche ich !!!
gruß Chris
AW: zusatzfrage
05.05.2009 14:36:37
chris
Hallo tino,
dein Code hat mir schon super weiter geholfen.
Jetzt aber eine frage vielleicht hast du da auch noch eine Idee ?
hier ist ein teil deines Codes.
Wenn das zu aktivierende fenster nicht gestartet ist beendet dein Makro einfach.
Gibt es eine möglichkeit zu sagen das dein Programm ich sag einfach einmal 20sekunden warten soll und immer wieder prüfen soll ob das fenster geöffnet wird und wenn nicht solll es abbruchen bevor es weiter macht. ?
Geht das ?
Do While hwnd 0
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
If GetWindowInfo(hwnd, STitel, True) = hwnd Then
'ShowWindow hwnd, iMaximized 'maximieren
SetForegroundWindow hwnd 'aktivieren
End If
Loop
'hier erst weiter machen wenn Fenster !!erfolgreich!! aktiviert wurde
call neues_makro
Würde mich freuen wenn Du oder jemand anderes mir noch einmal helfen könntest.
Vielen Dank
Anzeige
bin auf der arbeit, erst heute Abend. oT.
05.05.2009 15:24:30
Tino
AW: bin auf der arbeit, erst heute Abend. oT.
05.05.2009 16:37:42
chris
Wäre super.
Vielen Dank Tino !
teste mal
05.05.2009 23:16:18
Tino
Hallo,
ok. habe mal was zusammengebastelt. (siehe Sub Beispiel())
Option Explicit

Private Declare Function SetForegroundWindow _
  Lib "user32" ( _
  ByVal hwnd As Long) As Long
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 ShowWindow Lib "user32" ( _
  ByVal hwnd As Long, _
  ByVal nCmdShow 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

Const iNormal& = 1
Const iMinimized& = 2
Const iMaximized& = 3

Dim booAktiv As Boolean

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


Sub ActiviereAnwendung(STitel As String, WarteZeitSek As Integer, Optional i As Integer = 0)
Dim hwnd As Long

  hwnd = GetDesktopWindow()
  hwnd = GetWindow(hwnd, GW_CHILD)
  
  '2. Param. Fenstertitel (nur ein Teil erforderlich) 
  '3. Param. optional True nur Sichtbare, False alle 
    GetWindowInfo hwnd, STitel, True

    Do While hwnd <> 0
        hwnd = GetWindow(hwnd, GW_HWNDNEXT)
       If GetWindowInfo(hwnd, STitel, True) = hwnd Then
        ShowWindow hwnd, iMaximized 'maximieren 
        SetForegroundWindow hwnd 'aktivieren 
        booAktiv = (hwnd > 0)
        If booAktiv Then Exit Do
       End If
    Loop
  
    If (i < WarteZeitSek - 1) And Not booAktiv Then
     DoEvents
     Application.Wait Now + TimeSerial(0, 0, 1)
     i = i + 1
     ActiviereAnwendung STitel, WarteZeitSek, i
    End If


End Sub

Sub Beispiel()
 booAktiv = False
 
 '1. Param Programmtitel, 2. Param Wartezeit in Sekunden 
 ActiviereAnwendung "Internet Explorer", 20
 
 If Not booAktiv Then
  MsgBox "Anwendung nicht gefunden!", vbCritical
  Exit Sub
 End If

End Sub


Gruß Tino

Anzeige
AW: teste mal
06.05.2009 07:32:11
chris
Super,Klasse,Perfekt.
Vielen vielen Dank Tino !!!!!!!!!!!!!!!!!!!!!!!!
Schönen Tag wünsche ich Dir !

15 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige