Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1040to1044
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
Excel Instanzen finden und Anzeigen
22.01.2009 13:58:00
chris
Hallo VBA experten,
ich habe die letzten tage viel mit mit neuen und verschiedenen meheren Excel Instanzen zu tun gehabt.
Habe aber bis jetzt noch nicht herausgefunden wie ich mir diese alle anzeigen lasse.
Deshalb habe ich einmal diesen Code unten erstellt im Makro "anlegen".
Wenn ich diesen Code Ausführe werden 5 neue Instanzen von Excel erstellt.
Zum beispiel Mappe1 Mappe2 Mappe 3 .....
Aber ich habe jetzt versucht mit einem zweiten Makro alle instanzen anzeigen zu lassen in einer msgbox
aber das klappt leider nicht :( Das habe ich versucht mit Makro "test".
Würde mich sehr freuen wenn ihr mir helfen könnt.
Danke im vorraus gruß Chris

Public Sub anlegen()
For x = 1 To 5
Dim objApp As Excel.Application
Set objApp = New Excel.Application
'  objApp.Workbooks.Open Filename:="C:\Test.xls"
objApp.Visible = True
objApp.Workbooks.Add
'Set objApp = Nothing
Next
End Sub


Sub test()
Dim wb
For Each wb In Application.Windows
MsgBox wb.Caption
Next
End Sub


4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Excel Instanzen finden und Anzeigen
22.01.2009 14:04:00
Ramses
Hallo
Application.Windows bezieht sich auf die ausführende Application.
Damit kannst du keine anderen Instanzen/Fenster anzeigen lassen.
Das ist um einiges aufwändiger.
Wozu soll das denn gut sein. Im Normalfall hast du damit nur Nachteile
Das mal als Beispiel
Option Explicit
'Listet alle aktiven Fenter / Applikation auf
Private Declare Function GetWindow Lib "User32" _
    (ByVal appWnd As Long, ByVal wCmd As Long) As Long

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

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

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

Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" _
    (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long



Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Const GWL_STYLE = (-16)
Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000


Private Function GetWindowTitle(ByVal appWnd As Long) As String
    Dim appResult As Long, appTempStr As String
    appResult = GetWindowTextLength(appWnd) + 1
    appTempStr = Space(appResult)
    appResult = GetWindowText(appWnd, appTempStr, appResult)
    GetWindowTitle = Left(appTempStr, Len(appTempStr) - 1)
End Function

Sub CheckWindow()
    Dim x As Variant
    Dim AppName As String
    Dim Qe As Integer
    'Mit Parameter 0 wird nur True oder False
    'bei der Suche nach dem Programm Namen zurückgegeben
    'Mit Parameter 1 erfolgt die Ausgabe aller
    'gefundenen Fenster in eine MsgBox
    AppName = "Excel"
    AppName = InputBox("Bitte geben Sie die Applikation ein, die gesucht werden soll", _
    "Suche nach geöffneter Applikation", AppName)
    Qe = MsgBox("Soll nur geprüft werden ob die Application: " & AppName & " geöffnet ist?", _
    vbQuestion + vbYesNo, "Prüfung")
    If Qe = vbYes Then
        MsgBox "Applikation: """ & AppName & """ ist geöffnet: " & GetWindowList(AppName, 0)
    Else
        x = GetWindowList("EXCEL", 1)
    End If
End Sub

Public Function GetWindowList(findApp As String, kindMsg As Integer) As Boolean
    'Gibt True zurück wenn die Applikation aktiv ist
    Dim app() As Long
    Dim appWnd As Long, appTitle As String, appStyle As Long, appTask_name() As String
    Dim appCount As Integer, appIndex As Integer, appFound As Boolean
    Dim msgTxt As String
    appWnd = FindWindow(ByVal 0&, ByVal 0&)
    appWnd = GetWindow(appWnd, GW_HWNDFIRST)
    '1. Initialisierung
    GetWindowList = False
    Do
        'Loop starten durch alle geöffneten Fenster
        appFound = False
        appStyle = GetWindowLong(appWnd, GWL_STYLE)
        appStyle = appStyle And (WS_VISIBLE Or WS_BORDER)
        appTitle = GetWindowTitle(appWnd)
        'Alle gefundenen Applicationen in einen Array aufnehmen
        If (appStyle = (WS_VISIBLE Or WS_BORDER)) = True Then
            If Trim(appTitle) <> "" Then
                For appIndex = 1 To appCount
                    If appTask_name(appIndex) = appTitle Then
                        appFound = True
                        Exit For
                    End If
                Next appIndex
                If Not appFound Then
                    appCount = appCount + 1
                    ReDim Preserve appTask_name(1 To appCount)
                    appTask_name(appCount) = appTitle
                    ReDim Preserve app(1 To appCount)
                    app(appCount) = appWnd
                End If
            End If
        End If
        appWnd = GetWindow(appWnd, GW_HWNDNEXT)
    Loop Until appWnd = 0
    'Durchsuchen des erstellten Arrays nach der Application
    If kindMsg = 0 Then
        For appIndex = 1 To appCount
            'Es wird nur der übergebene String in "appTask_Name" gesucht
            'Die Instanz selbst wird nicht identifiziert.
            'Dazu müsste noch der String "Microsoft" geprüft werden
            If InStr(1, appTask_name(appIndex), findApp) > 1 Then
                'Application gefunden = Ende der Schleife
                GetWindowList = True
                Exit Function
            End If
        Next appIndex
        ElseIf kindMsg = 1 Then
        For appIndex = 1 To appCount
            msgTxt = msgTxt & "Aktiv: " & appTask_name(appIndex) & Chr$(13)
        Next appIndex
        MsgBox msgTxt
        GetWindowList = True
    End If
End Function


Gruss Rainer
Anzeige
AW: Excel Instanzen finden und Anzeigen
22.01.2009 15:45:00
chris
Danke Ramses !! Sowas habe ich gesucht
Danke auch den anderen helfern Schlumpf und Roland !!!
AW: Excel Instanzen finden und Anzeigen
22.01.2009 14:07:00
Oberschlumpf
Hi Chris
Wenn es NUR um die göffneten Excel-Dateien (Workbooks) geht, dann versuch es mal so:

Sub test()
Dim wb As Workbook
For Each wb In Application.Workbooks
MsgBox wb.Name
Next
End Sub


Hilfts?
Ciao
Thorsten

AW: Excel Instanzen finden und Anzeigen
22.01.2009 15:34:00
Roland
Hallo chris b.,
wenn es Dir nur um die Anzahl der Excel-Instanzen geht, reicht das hier:

Sub test()
Dim l As Long
l = ReadProcessData("EXCEL.EXE")
MsgBox l & " Excel-Instanzen offen"
End Sub



Function ReadProcessData(ByVal Prozess As String) As Long
Dim objWMIService As Object, colProcesses As Object, sinProcess As Object
Set objWMIService = GetObject("winmgmts:")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
ReadProcessData = 0
For Each sinProcess In colProcesses
With sinProcess
If .Name = Prozess Then ReadProcessData = ReadProcessData + 1
End With
Next
Set objWMIService = Nothing
Set colProcesses = Nothing
End Function


Es grüßt
Roland Hochhäuser

Anzeige

299 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige