Determine if a workbook was opened by VBA code or by user

KeepTrying

Active Member
Joined
Aug 19, 2012
Messages
276
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi Folks,

It sounds simple but I couldn't find a proper way how to check if a workbook was opened by macro or by a user. To cut a long story short: I run a macro (it displays a userform) which opens several workbooks while running. How can I determine if a workbook is opened by a user during running? I want to prevent users to open a new/existing workbook while my macro is running.
Thanks in advance for your effort and time.

Kind Regards,
 
Dear Jaafar,
Thanks a lot for dropping by :) I wouldn't have thought this determination can be so complex.


Workbooks - which are opened by userform - are not always the same but if you can create a solution for fixed workbooks I'd be really grateful.

Thank you.

Hi KeepTrying,

I took a look at the WH_CALLWNDPROC hook approach , unfortunately it only works with Top-level windows ... Workbook windows are child windows so it won't work to trap the opening of workbooks.

I am assuming that the userform that is running is Modeless - Can you confirm that ?
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi Jaafar,


Yes, I confirm that.

Thank you.

Following on mikerickson's steps, here is an alternative that should also prevent the user from opening files in other excel instances while the userform is running.

Code in the UserForm module :
Code:
Option Explicit

Private WithEvents app As Application
Private bIgnoreRemoteRequests As Boolean


Private Sub UserForm_Initialize()
    bIgnoreRemoteRequests = Application.IgnoreRemoteRequests
    Application.IgnoreRemoteRequests = False
    Set app = Application
End Sub


Private Sub UserForm_Terminate()
    Application.IgnoreRemoteRequests = bIgnoreRemoteRequests
End Sub


Private Sub app_WorkbookOpen(ByVal Wb As Workbook)
    Call app_NewWorkbook(Wb)
End Sub


Private Sub app_NewWorkbook(ByVal Wb As Workbook)
        If Me.Tag <> "AllowOpening" Then
            Wb.Close SaveChanges:=False
            MsgBox "You cannot open or create a workbook at this time."
        End If
End Sub


[COLOR=#008000]'Example of a workbook that is allowed to be opened by your code.[/COLOR]
[COLOR=#008000]'Set the Tag value before the opening and reset afterwards.[/COLOR]
Private Sub CommandButton1_Click()
    Me.Tag = "AllowOpening"
        Workbooks.Open "C:\Users\Info-Hp\Desktop\Foo.xlsm"
    Me.Tag = ""
End Sub

The last CommandButton1_Click routine shows you a way of allowing the opening of a file from your userform via VBA while blocking the user.

Note:
As mikerickson pointed out, the code would fail (or rather be interrupted) if the user opens a macro-enabled workbook and is prompted with the Enable-Macros dialog.. The same if the opened workbook displays a modal window upon opening such as a MsgBox.

Edit:
Also, may I ask you, - Is it necessary for the userform to be modeless ? If you use a Modal userform, the user won't be able to open any workbooks via the excel interface.
 
Last edited:
Upvote 0
Dear Jaafar,
Thanks for you effort. As you pointed out code works except if....
Userform shouldn't be necessarily modeless. I changed it to Modal but it still allows to open e.g. .xlsm file (even if .xlsm file does not contain macro).
I start to run userform then open an .xslm file from Windows Explorer. After that I get usual message about "PERSONAL.XLSB is locked for editing" then .xlsm file opens.
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

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