Detect if Excel file is opened in a web browser (1 Viewer)

ghudson

Registered User.
Local time
Today, 17:31
Joined
Jun 8, 2002
Messages
6,195
How can I detect if the current Excel 2003 file is opened within an Internet Explorer web browser and not within the Excel.exe program?

I have an Excel file that is opened from a SharePoint site and all but a few of the 50+ users have no problem with the VBA in the file because their computer is opening the Excel file within the Excel.exe program. A few users so far [more users will be added going forward] have problems with the VBA that creates copies of the files worksheets and saves their output files back to the SharePoint site. Once the user’s computers XLS Excel Worksheet File Types are reset to not open the file with the Browse In Same Window option all is ok but I need to be proactive and run some VBA when the Excel file is opened to detect if the xls file is opened within an Internet Explorer web browser or within the Excel.exe program. It is an Excel 2003 file and most users have Excel 2003 but some are using Excel 2007 with Windows XP.

Anybody know how I can do this? Thanks in advance for your help!
 

HiTechCoach

Well-known member
Local time
Today, 16:31
Joined
Mar 6, 2006
Messages
4,357
How can I detect if the current Excel 2003 file is opened within an Internet Explorer web browser and not within the Excel.exe program?

...

Once the user’s computers XLS Excel Worksheet File Types are reset to not open the file with the Browse In Same Window option all is ok ...

If it were me, instead for trying to detect what has opened the file, I would use a logon or start up script that runs a registry update to insure that "XLS Excel Worksheet File Types are reset to not open the file with the Browse In Same Window"

It should be easy to do once you determine the registry keys to set. I currently do it to set Access 2003 Macro Security level to low or add trusted locations for 2007.
 

ghudson

Registered User.
Local time
Today, 17:31
Joined
Jun 8, 2002
Messages
6,195
If it were me, instead for trying to detect what has opened the file, I would use a logon or start up script that runs a registry update to insure that "XLS Excel Worksheet File Types are reset to not open the file with the Browse In Same Window"

It should be easy to do once you determine the registry keys to set. I currently do it to set Access 2003 Macro Security level to low or add trusted locations for 2007.


Thanks for the suggestion. There are show stopper problems if the user has opened the Excel file within a web browser. I need to detect if the Excel file was opened within a web browser so that I can stop the user from trying to run the functions I have programmed into the Excel application. Updating their registry would only help the next time they open the application. I prefer not to mess with their registry since some users are running Office 2003, Office 2007 or both. All users should have Windows XP and Internet Explorer 6 but I cannot guarantee which OS and browser they are using since they are remote users who can do their own upgrades which makes it even more difficult.

Anybody know how I can detect when the user has opened the Excel file from a SharePoint site if the Excel file is opened within a web browser or in the Excel.exe program?

Thanks in advance for your help!
 

chergh

blah
Local time
Today, 22:31
Joined
Jun 15, 2004
Messages
1,414
I haven't used sharepoint but couldn't you use getobject and then parse the url of any browser objects returned?
 

ghudson

Registered User.
Local time
Today, 17:31
Joined
Jun 8, 2002
Messages
6,195
I haven't used sharepoint but couldn't you use getobject and then parse the url of any browser objects returned?

It is an Excel file that acts like any Excel file except that the file cannot create a copy of a worksheet and move it into a new workbook when the Excel file is opened in a web browser. I can trap for the runtime error but I prefer to alert the user [and disable the custom functions programmed into the Excel file] as soon as they open the Excel file if it is opened within a web browser.
 

chergh

blah
Local time
Today, 22:31
Joined
Jun 15, 2004
Messages
1,414
If it is opened in a browser I assume the path to the file and file name is displayed as the url. You should be able to determine from the path if it is open in a web browser or not.

Code:
Public Declare Function ShowWindow& Lib "user32" ( _
ByVal hwnd As Long, _
ByVal nCmdShow As Long)

Private Sub Workbook_Open()

Dim oShApp As Object
Dim oWin As Object
Dim IE As Object
Dim WScript As Object

Set oShApp = CreateObject("Shell.Application")

For Each oWin In oShApp.Windows
   If TypeName(oWin.Document) = "HTMLDocument" Then
      Set IE = oWin
      if IE.LocationURL = "Browser path to excel sharepoint file here" then

          'code to disable desired functions here

      end if
      
   End If
Next

end sub
 

ghudson

Registered User.
Local time
Today, 17:31
Joined
Jun 8, 2002
Messages
6,195
Thanks for the code. I use this to get the location of the active Excel file... Range("Location").Value = ThisWorkbook.FullName

The file path with be a URL http://blahblah/ and it is supposed to be since the file is opened from a SharePoint site. My quick fix this morning was to trap for the runtime error # 9 and alert the user on how to adjust their computers File Type settings for XLS Worksheets for the Open action.
 

HiTechCoach

Well-known member
Local time
Today, 16:31
Joined
Mar 6, 2006
Messages
4,357
Thanks for the suggestion. There are show stopper problems if the user has opened the Excel file within a web browser. I need to detect if the Excel file was opened within a web browser so that I can stop the user from trying to run the functions I have programmed into the Excel application. Updating their registry would only help the next time they open the application. I prefer not to mess with their registry since some users are running Office 2003, Office 2007 or both. All users should have Windows XP and Internet Explorer 6 but I cannot guarantee which OS and browser they are using since they are remote users who can do their own upgrades which makes it even more difficult.

Anybody know how I can detect when the user has opened the Excel file from a SharePoint site if the Excel file is opened within a web browser or in the Excel.exe program?

Thanks in advance for your help!

if you arerunning a start up/logon script to insure the registry is set properly it is down before any apps are launch. So why would it have to be the next time. <confused>

If you app requires the reg setting then why are you not wanting to set it.

IMHO, if you are not wanting to make registry settings to insure your app run correctly then I would find a different method.
 

chergh

blah
Local time
Today, 22:31
Joined
Jun 15, 2004
Messages
1,414
Actually this is quite simple now I've done some digging. Use:

Code:
debug.print thisworkbook.container.parent.name

That should give you what you want.
 

ghudson

Registered User.
Local time
Today, 17:31
Joined
Jun 8, 2002
Messages
6,195
if you arerunning a start up/logon script to insure the registry is set properly it is down before any apps are launch. So why would it have to be the next time. <confused>
There is no startup scripts to load the Excel application. The users click a link on the SharePoint site to open the file from the SharePoint site.

IMHO, if you are not wanting to make registry settings to insure your app run correctly then I would find a different method.
I am trying to find a different method. ;) I am trying to detect if my Excel application is open within a web browser.

Thanks!
 

ghudson

Registered User.
Local time
Today, 17:31
Joined
Jun 8, 2002
Messages
6,195
Actually this is quite simple now I've done some digging. Use:

Code:
debug.print thisworkbook.container.parent.name

That should give you what you want.

Awesome! Such a simple command to do what I wanted. It gives runtime Run-time error # 1004 Method ‘Container’ of object’ _Workbook failed if run in Excel but gives the name of the browser if run when the file is opened in a web browser. Brilliant. Thank you chergh!

I am not the first to search for this answer. I just did not use the right words when googling for I found this once you directed me to use the thisworkbook.container.parent.name command. How can Excel vba tell if it is being executed under Internet Explorer?

BTW, both of these commands gives the same answer "Microsoft Internet Explorer" when the Excel file is running in a web browser...

MsgBox ThisWorkbook.Container.Parent.Name
MsgBox ThisWorkbook.Container.Name
 

PetRose

New member
Local time
Today, 23:31
Joined
Aug 3, 2023
Messages
2
Hi there, happy to find some peers that are facing the exact same issue like myself :)
Even this thread is quite old, its still has actuality.

My issue, is that I need a trigger in my Excel Macro enabled code whether the instance is opened in Browser mode rather than Excel natively (in a Excel365) environment of corporate OnPremise SharePoint as well as OneDrive (Business).
In my view: The problem is not to detect the property of ThisWorkbook.Container.Name is 1) available (ie Browser is running), or 2) not (catching Err 1014, ie Excel is running Natively).
In situation 1)
  • the Excel Open Event cannot do anything but Exiting, since you do not have any means of telling/giving a message back to User.
  • one cannot debug the Open Event, as the MS VBA debugger replies 'Can't execute code in break mode', but of course you write proper code that works from day one ;)
So I am curious to hear if you @ghudson, found a solution that are more kind to end users. And also, if you are sure the Open Event is triggered when opened by Browser ?
Cheers
 

sonic8

AWF VIP
Local time
Today, 23:31
Joined
Oct 27, 2015
Messages
998
Hi there, happy to find some peers that are facing the exact same issue like myself :)
Even this thread is quite old, its still has actuality.
Actually, I think you are mistaken about that.
If I understand it correctly, this 13 year old thread is about an Excel workbook being opened in Internet Explorer by hosting an local Excel.exe as OLE/ActiveX instance inside the IE process/window.

I assume you rather want to tell whether a workbook is opened in Excel.exe or in the Excel Online JavaScript application in a web browser (any web browser, not just IE). - The latter is something entirely different than hosting an Excel.exe instance in Internet Explorer.

So I am curious to hear if you @ghudson, found a solution that are more kind to end users. And also, if you are sure the Open Event is triggered when opened by Browser ?
No VBA code is executed in an Excel workbook when it is opened in the Excel Online web application. See: https://support.microsoft.com/en-us...-the-web-98784ad0-898c-43aa-a1da-4f0fb5014343
 

PetRose

New member
Local time
Today, 23:31
Joined
Aug 3, 2023
Messages
2
Actually, I think you are mistaken about that.
If I understand it correctly, this 13 year old thread is about an Excel workbook being opened in Internet Explorer by hosting an local Excel.exe as OLE/ActiveX instance inside the IE process/window.
Well, true its not the exact same I was looking for, but the more abstract UseCase of workbook being opened in 'some' Browser' as you indicate.
No VBA code is executed in an Excel workbook when it is opened in the Excel Online web application.
Its sad to hear. How can MS do this ? All the business rules that exists and written in VBA, will be discarded in this way.
I will have to chase the trail you provided.
Thanks so far.
 

Users who are viewing this thread

Top Bottom