tags:

views:

56

answers:

2

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!

+1  A: 

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.

Charles Williams
Good suggestion. Thanks. That's actually what Excel does with application level settings. The most recent saved setting gets saved to and grabbed from the registry when the application initializes. Unfortunately, that doesn't populate the changes across all instances in "real-time," because Workbook.Activate for add-ins only fires when the application initializes. For example, if you have two instances and you minimize the ribbon in one, the ribbon is still maximized in the other. That's a very simple example of how things can get out of synch and confuse the user.
Kuyenda
I did not mean workbook.Activate for the addin - I meant use an Application level event that fires whenever any workbook gets focus - you would have to test which event is most suitable for your situation.
Charles Williams
http://www.cpearson.com/excel/AppEvent.aspxThat's a good rundown of application level events. Your suggestion is a good one, but it might get messy depending on much needs to be synchronized every time a user changes the focus between applications. I am going to hold out for now and force users to stick with one instance, but thanks!
Kuyenda
+1  A: 

Hi,

You stop people from open 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 deterrent settings in each one anyway?

Ross
Before I figured out how to block multiple instances of Excel running concurrently my users used to get confused because they would make changes in Excel, switch to another window, and their change wasn't reflected there. They didn't realize that the other window was another instance of Excel. This was for standard Excel functionality, not just my add-in functionality. So, yeah, I definitely want to keep things "consistent" if I allow multiple instances. Otherwise, I am going to be fielding questions like, "Why did the ribbon reappear after I minimized it?" Thanks!
Kuyenda
Interesting: I have never had that problem with users of my addins.
Charles Williams