在多个Excel实例运行期间检索所有工作簿名称 [英] Retrieve all workbook names during more than one excel instances are running

查看:71
本文介绍了在多个Excel实例运行期间检索所有工作簿名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基本上是关于循环所有excel实例中的所有工作簿!

This question is basically regarding to loop all workbooks in all excel instances!

推荐答案

您面临的主要问题是您没有使用遇到的任何过程.因此,您将无法获得任何东西.在该进程的循环内部,然后创建一个ExcelApplication的新实例,然后尝试遍历Workbooks.默认情况下,当您执行此操作时,当时只有 1 ,因此为什么只有 1 Workbook以及为什么只看到 1 Workbook.

Your main issue you are facing is you are not using any of the process's you come across. Therefore, you will not get anything that way. Inside of the loop for the process's you then create a new instance of ExcelApplication and then try to loop through the Workbooks. By default when you do this there is only 1 at that time, hence why you get only 1 Workbook and also why you will only ever see 1 Workbook.

解决方案(经过测试)

您需要查看 Windows API 调用以获取所需的内容.其中一些是:

You need to look into Windows API calls to get what you need. A few of them are:

  1. GetDesktopWindow()
  2. EnumChildWindows()
  3. GetClassName()
  4. EnumWindowsProc()
  5. AccessibleObjectFromWindow()

  1. GetDesktopWindow()
  2. EnumChildWindows()
  3. GetClassName()
  4. EnumWindowsProc()
  5. AccessibleObjectFromWindow()

Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Public Class Form1

Private Declare Function GetDesktopWindow Lib "user32" () As Integer
Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowsProc, ByVal lParam As IntPtr) As Boolean
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
Private Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" (ByVal Hwnd As Int32, ByVal dwId As Int32, ByRef riid As Guid, <MarshalAs(UnmanagedType.IUnknown)> ByRef ppvObject As Object) As Int32

Private Const OBJID_NATIVE = &HFFFFFFF0

'Required to show the workbooks. Used in function to add to.
Private lstWorkBooks As New List(Of String)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    lstWorkBooks.Clear()
    GetExcelOpenWorkBooks()
End Sub


Private Sub GetExcelOpenWorkBooks()
    Try
        'Get handle to desktop
        Dim WindowHandle As IntPtr = GetDesktopWindow()

        'Enumerate through the windows (objects) that are open
        EnumChildWindows(WindowHandle, AddressOf GetExcelWindows, 0)

        'List the workbooks out if we have something
        If lstWorkBooks.Count > 0 Then MsgBox(String.Join(Environment.NewLine, lstWorkBooks))

    Catch ex As Exception
    End Try

End Sub

Public Function GetExcelWindows(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Boolean

    Dim Ret As Integer = 0
    Dim className As String = Space(255) 'Return the string with some padding...

    Ret = GetClassName(hwnd, className, 255)
    className = className.Substring(0, Ret)

    If className = "EXCEL7" Then
        Dim ExcelApplication As Excel.Application
        Dim ExcelObject As Object = Nothing
        Dim IDispatch As Guid

        AccessibleObjectFromWindow(hwnd, OBJID_NATIVE, IDispatch, ExcelObject)

        'Did we get anything?
        If ExcelObject IsNot Nothing Then
            ExcelApplication = ExcelObject.Application
            'Make sure we have the instance...
            If ExcelApplication IsNot Nothing Then
                'Go through the workbooks...
                For Each wrk As Excel.Workbook In ExcelApplication.Workbooks
                    'If workbook ins't in the list then add it...
                    If Not lstWorkBooks.Contains(wrk.Name) Then
                        lstWorkBooks.Add(wrk.Name)
                    End If
                Next
            End If

        End If
    End If

    Return True

End Function

End Class

这篇关于在多个Excel实例运行期间检索所有工作簿名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆