如何在Excel.exe的多个实例上运行Excel加载项 [英] How can I run an Excel Add-in on Mutliple instances of Excel.exe

查看:522
本文介绍了如何在Excel.exe的多个实例上运行Excel加载项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



过去,我阻止了用户打开多个Excel实例。当我的加载项使用以下代码运行时。

  Private Sub KillDuplicateProcesses()

Dim objWMIService As Object
Dim colItems As Variant
Dim objItem As Object
Dim intCount As Integer

设置objWMIService = GetObject(winmgmts:\\.\ root \cimv2)
设置colItems = objWMIService.InstancesOf(Win32_Process)
对于每个objItem在colItems
intCount = intCount + Abs(LCase(objItem.Name)=excel。 exe)
下一个

如果intCount> 1然后
MsgBoxExcel已经在运行,vbCritical
Application.Quit
如果

设置objWMIService = Nothing
设置colItems = Nothing

End Sub

然而,我想知道如果有办法安全运行一个加载项,而Excel的多个实例正在运行。



例如,如果我在VBA中执行这样的操作:

  Application.MoveAfterReturnDirection = xlDown 

应该在所有的Excel实例中同时更改CommandBar对象的任何变化,每个实例都有自己的窗口。



谢谢!

解决方案

我建议您的插件使用注册表来存储要保留的设置,并应用于所有Excel会话。
然后,addin将使用诸如workbook.activate之类的应用程序级事件来检查存储的注册表设置。


Application level events and processes in Excel are restricted to a single instance of the application.

In the past I have prevented users from opening more than one instance of Excel when my add-in is running using the following code.

Private Sub KillDuplicateProcesses()

    Dim objWMIService As Object
    Dim colItems As Variant
    Dim objItem As Object
    Dim intCount As Integer

    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.InstancesOf("Win32_Process")
    For Each objItem In colItems
        intCount = intCount + Abs(LCase(objItem.Name) = "excel.exe")
    Next

    If intCount > 1 Then
        MsgBox "Excel is already running.", vbCritical
        Application.Quit
    End If

    Set objWMIService = Nothing
    Set colItems = Nothing

End Sub

I am wondering, however, if there is a way to safely run an add-in while multiple instances of Excel are running.

For example, if I do something like this in VBA:

Application.MoveAfterReturnDirection = xlDown

This change and any changes to CommandBar objects should be reflected across all instances of Excel, each with its own window, simultaneously.

Thanks!

解决方案

I would suggest that your addin uses the registry to store settings that you want to persist and apply across all Excel sessions. Then the addin would use an application-level event such as workbook.activate to check the stored registry settings.

这篇关于如何在Excel.exe的多个实例上运行Excel加载项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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