How can I run an Excel Add-in on Mutliple instance

2019-08-31 02:38发布

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!

标签: excel vba
2条回答
萌系小妹纸
2楼-- · 2019-08-31 03:06

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.

查看更多
做自己的国王
3楼-- · 2019-08-31 03:10

You stop people from opening an new instance of Excel so your addin will work! - Nice!!!!

Charles idea is good, it's like global variables across instances. Likewise you could write out to a .ini file and reference that each time.

It depends on what you're doing, but are you sure you want the settings to be the same across all instances? Maybe I want to have different settings in each one anyway?

查看更多
登录 后发表回答