Preventing excel program from running if computer is running another instance of excel or word

unluckyuser

New Member
Joined
Jan 12, 2025
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
Hello. Working in an environment where we have memory crashes when multiple spreadsheet and word processing documents are opened. Is there a way I can prevent my excel automatic macro from executing if there's another instance of excel or word running? So if they're going to run my excel macro, that can be the only thing that's opened?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Paste the following into the ThisWorkbook module :

VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Dim xlApp As Object
    Dim wdApp As Object
    Dim otherExcelInstance As Boolean

    On Error Resume Next
    ' Check for other instances of Excel, excluding this workbook
    Set xlApp = GetObject(, "Excel.Application")
    If Not xlApp Is Nothing Then
        If xlApp.Workbooks.Count > 1 Then
            otherExcelInstance = True
        End If
    End If

    ' Check if another instance of Word is running
    Set wdApp = GetObject(, "Word.Application")
    On Error GoTo 0

    If otherExcelInstance Or Not wdApp Is Nothing Then
        MsgBox "Another instance of Excel or Word is already running. Close those running applications then re-open this workbook.", vbCritical, "Application Restriction"
        
      

        ' Close the master workbook
        Application.DisplayAlerts = False
        ThisWorkbook.Close SaveChanges:=False
        Application.Quit
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,225,739
Messages
6,186,741
Members
453,370
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top