PDA

View Full Version : Solved: Excel running inside Internet Explorer



dlh
03-22-2010, 10:51 AM
How can I tell if my Excel VBA code is running in shell-mode within Internet Explorer?

I have a custom menu that's created in the Workbook_Activate procedure, but when an Excel file is opened inside a browser window, the Activate event seems to be triggered before the menu objects are available. At least that's my guess of what's happening. The symptom is the Controls.Add method fails.

So I'm trying to figure out some logic to avoid calling my LoadCustomMenu subroutine if the user is not running stand-alone Excel. I don't really need the menu in this situation, but I need the VBA code to not abort.

I tried testing the values of Application.Name and Application.Parent.Name, but they equal "Microsoft Excel" all the time. Any expert advice would be very much appreciated. My existing code is below.



Private Sub Workbook_Activate()
Call LoadCustomMenu
End Sub

Public Sub LoadCustomMenu()
Dim CustomMenu As CommandBarPopup
Set CustomMenu = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, temporary:=True)
'... etc.
End Sub

SamT
03-22-2010, 09:59 PM
Window.Parent?

idunno

dlh
03-23-2010, 11:08 AM
Nope.

The following also don't work:



Debug.Print Application.Windows.Parent.Name
'Microsoft Excel
Debug.Print ActiveWindow.Caption
'MyFilename
Debug.Print ActiveWindow.Creator = xlCreatorCode
'always True
Debug.Print ActiveWindow.Parent.Name
'MyFilename

SamT
03-23-2010, 12:58 PM
From:
http://www.eggheadcafe.com/software/aspnet/28441158/i-dont-have-any-experien.aspx
and
http://groups.google.com/group/microsoft.public.excel.programming/msg/d0ae1958a648bd13

John Michl posted on Saturday, October 28, 2006 7:54 AM

Here's my solution.
In ThisWorkbook
Dim bDone As Boolean
Private Sub Workbook_Activate()
If Not bDone Then bDone = True
Call Application.OnTime(Now + TimeSerial(0, 0, 1), "TimedMsg")
End If
End Sub

In Module1:

Sub TimedMsg()
On Error GoTo NativeExcel
CName = ThisWorkbook.Container.Name ' will error if no container(Native Excel = no container)
MsgBox "Warning! You are running in a browser window."
Exit Sub
NativeExcel:
MsgBox "You're in Excel."
End Sub


End Paste John Mitchl

there has to be a timer after workbook activate so IE can do its thing.
SamT

dlh
03-23-2010, 05:45 PM
Thanks for finding that, SamT. It works as advertised. The Container property and OnTime methods were new to me.

Interestingly, scheduling the call to Controls.Add with OnTime still does not work, even with a 10 second delay.

However something changes the instant the debugger is activated which does enable Controls.Add.

Now I have to decide whether I want to use this delay solution or just:


Public Sub LoadCustomMenu()
On Error Resume Next

SamT
03-24-2010, 11:31 AM
My unqualified guess is that while the VBA compiles OK, something associated with the Container (Browser) causes the mix to throw an error.

I'd go with On Error Resume Next as long as it passes beta with all browsers.