| ©«¤l23 ¥DÃD0 ºëµØ0 ¿n¤À73 ÂI¦W265  §@·~¨t²ÎXP ³nÅ骩¥»Office 2003 ¾\ŪÅv20 µù¥U®É¶¡2012-4-21 ³Ì«áµn¿ý2025-10-30 
            
 | 
                
| no3-taco«e½ú¡AÁÂÁ§AªºÀ°¦£¡C
 §Ú¦³¬d¨ìºô¸ô¤WªºÃþ¦üªº°ÝÃD¡C§Ú§â¥N½X©ñ¦b©³¤U¡]¤W¤@«Êªº¥N½X¬O§Ú¨Ì ...
 justintoolbox µoªí©ó 2015-7-25 17:37
 
 ¸Õ¸Õ¬Ý(´ú¸ÕÀô¹Ò : Win7 + Office2013)
 ½Æ»s¥N½XOption Explicit
Option Base 1
#If Win64 Then
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
        (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, _
         ByVal lpsz2 As String) As LongPtr
Private Declare PtrSafe Function IIDFromString Lib "ole32" _
        (ByVal lpsz As LongPtr, ByRef lpiid As GUID) As LongPtr
Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "oleacc" _
        (ByVal hWnd As LongPtr, ByVal dwId As LongPtr, ByRef riid As GUID, _
         ByRef ppvObject As Object) As Long
#Else
Private Declare Function FindWindowEx Lib "User32" Alias "FindWindowExA" _
        (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
         ByVal lpsz2 As String) As Long
Private Declare Function IIDFromString Lib "ole32" _
        (ByVal lpsz As Long, ByRef lpiid As GUID) As Long
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" _
        (ByVal hWnd As Long, ByVal dwId As Long, ByRef riid As GUID, _
         ByRef ppvObject As Object) As Long
#End If
         
Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(7) As Byte
End Type
Private Const S_OK As Long = &H0
Private Const IID_IDispatch As String = "{00020400-0000-0000-C000-000000000046}"
Private Const OBJID_NATIVEOM As Long = &HFFFFFFF0
Private Function GetXLapp(hWinXL, xlApp As Object) As Boolean
    Dim hWinDesk, hWin7, obj As Object
    Dim iid As GUID
    Call IIDFromString(StrPtr(IID_IDispatch), iid)
    hWinDesk = FindWindowEx(hWinXL, 0&, "XLDESK", vbNullString)
    hWin7 = FindWindowEx(hWinDesk, 0&, "EXCEL7", vbNullString)
    If AccessibleObjectFromWindow(hWin7, OBJID_NATIVEOM, iid, obj) = S_OK Then
        Set xlApp = obj.Application
        GetXLapp = True
    End If
End Function
Private Function IsCollectionExists(ByVal oCol As Collection, ByVal vKey As Variant) As Boolean
    On Error Resume Next
    oCol.Item vKey
    IsCollectionExists = (Err.Number = 0)
    Err.Clear
    On Error GoTo 0
End Function
Public Function GetXLInstanceInfo(ByRef col As Object) As Long
    Dim hWndXL, i As Long
    Dim xlApp As Object, wb As Object
    Set col = Nothing
    Set col = New Collection
    hWndXL = FindWindowEx(0&, 0&, "XLMAIN", vbNullString)
    While hWndXL > 0
        If GetXLapp(hWndXL, xlApp) Then
            For Each wb In xlApp.Workbooks
                If Not IsCollectionExists(col, wb.Name) Then
                     col.Add Array(hWndXL, xlApp, wb.Name, wb.Path), wb.Name
                End If
            Next
        End If
        hWndXL = FindWindowEx(0, hWndXL, "XLMAIN", vbNullString)
    Wend
    GetXLInstanceInfo = col.Count
    
End Function
Sub Ex()
    Dim col As Collection
    Dim i As Long
    Dim xlApp As Excel.Application, AR As Variant
    
    i = GetXLInstanceInfo(col)
    Debug.Print "¿W¥ßEXCEL¦³¡G" & i & "Ó"
    
    '´ú¸Õ : ¦b©Ò¦³ExcelÀɲÄ1Ó¤u§@ªíÀx¦s®æA1¤W¼g¤J¦Û¤vªºÀɦW
    For i = 1 To col.Count
        AR = col(i)    ' AR(1): HWND (µøµ¡±±¨î¥N½X)
                       ' AR(2): xlApp (ExcelÀÉ©ÒÄݪº¤÷¶µ Excel.Application)
                       ' AR(3): ÀɦW
                       ' AR(4): Àɮ׸ô®|
        Set xlApp = AR(2)
        xlApp.Workbooks(AR(3)).Sheets(1).Range("A1").Value = "ÀɦW:" & AR(3)
    Next
    
    Set xlApp = Nothing
    Set col = Nothing
    
End Sub
 | 
 |