Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Detect Excel running in browser (Excel (all))

    Is there a way I can detect if my .xls file is being run from w/in a web browser? I'd add it to the workbook_open event. I have many .xls file stored on my company's internal web site and even though I have warning text in red all over the place, people STILL double-click the link and open it within the brower!! A window pops up to Open or SaveAs but they press Open instead. <img src=/S/bummer.gif border=0 alt=bummer width=15 height=15>

    VBA enhanced Excel files rarely work within a browser (except for trivial code) and I'd like to issue a warning to the user that they can't run the file in the browser - that they need to download it to their local computer.

    If people would actually read the red warning text to download the file, I could avoid several support calls complaining that the file doesn't work! *sigh* It's hopeless trying to get people to read directions, isn't it? <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

    Thnx, Deb

  2. #2
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    I opened a workbook in IE 6 and

    Activeworkbook.Container

    returned Microsoft Web Browser Control

    An error (1004) is reported if the workbook is opened in a normal excel session.


    Andrew C

  3. #3
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    Good stuff. I was trying to use Application object but couldn't find anything useful. When I tried to implement your suggestion, I couldn't get it to work:
    <pre>Private Sub Workbook_Open()
    Dim obHost

    On Error GoTo inExcel

    Set obHost = ThisWorkbook.Container
    MsgBox "Excel is running within a browser"
    Exit Sub

    inExcel:
    Err.Clear
    Set obHost = Nothing
    MsgBox "NOT inside browser"

    End Sub</pre>

    In this code I always get the NOT message even when opened in IE (so it must be getting an error forcing it to jump).
    In the help for the Container property it says it returns an object to the hosted application (there is no usage example). What do you think I'm doing wrong?

    What about reading the top title bar? It shows "Microsoft Internet Explorer" or "Microsoft Excel" so I could parse that string to see what it's running in. Nah forget that, doesn't work. I ran "application.caption" and it shows "Microsoft Excel" as the caption even when it's run in a browser. <img src=/S/bummer.gif border=0 alt=bummer width=15 height=15>

    Deb

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Detect Excel running in browser (Excel (all))

    I don't do much work in Excel, but some of the object models have interesting and obscure information like the path to the host's executable. I suspect that you could differentiate between EXE and OCX (or whatever is relevant) if you could find that property. Assuming it exists. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    Deb,

    The code you posted worked fine for me, Excel 2002 and IE 6.

    What version of XL are you using ?

    Andrew C

  6. #6
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    It worked? As of last week my company upgraded everyone to Office 2003 (forced upgrade via a network install, I can't say no, unfortunately). Jeessh, here I go again with inconsistencies in VBA for the different Excel versions. Maybe I'll have to go with the "low-tech" route and display a text message in the first page of the workbook that says it can only be run locally. But then again, that requires users to actually READ !! <img src=/S/cranky.gif border=0 alt=cranky width=18 height=25>

    Deb

  7. #7
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    Deb,

    Sorry, but when I actually tried your code in response to a Workbook_Open event, it failed in the manner you suggested. However it did work correctly in response to an OnTime trigger in the Workbook_Open event (I tried it with a 15 second delay). Earlier I had just run the code whilst the workbook was opened.

    It seems that the Container cannot be properly determined until after the workbook has been opened. Auto_Open also failed.

    Andrew

  8. #8
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    Great. Good troubleshooting <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> I'll use your suggestion then - checking .Container after a certain amount of time has passed after WB open. This actually works fine because as jscher2000 suggested, I do have an "intro" sheet that displays for 4 seconds and then jumps to a legal disclaimer sheet with Accept/Reject buttons (all other sheets are veryhidden and protected at this point). After they hit the Accept button, the code unhides whatever sub-set of sheets I want them to use.

    Another idea that just dawned on me is that whenever the workbook is displayed in the browser I get the added 'browser' tool bar. I guess I could also check whether that is visible or not, right? The downside of that though is I can't guarantee that the user doesn't have that on 'normally' anyway.

    Thnx, Deb <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15> <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

  9. #9
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    Yes, that has been my experience.

    Does not work during open , but once the workbook is opem code works.

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    Late last night, I was fooling around with the code below.
    Likely needs some addition to delay the code until the workbook is open.

    <pre>
    Private Sub Workbook_Open()
    Dim obHost As Object
    On Error Resume Next
    Set obHost = ThisWorkbook.Container
    With Err
    If .Number = 0 Then
    MsgBox obHost.Name & vbCrLf & Application.Name
    If obHost.Name = Application.Name Then
    MsgBox "Excel is NOT running within another application."
    Else
    MsgBox "Excel is running within another application."
    End If
    Else
    MsgBox .Number & vbCrLf & .Description
    MsgBox "Excel is NOT running within another application."
    .Clear
    End If
    End With
    Set obHost = Nothing
    End Sub
    </pre>


  11. #11
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    I just now tried it and it works great. I added a call to .OnTime with a 5 second delay in WB_Open and it works fine. I was testing how short I can make the delay given this wil be run on any unknown user's computer. My laptop is 1 year old and I was able to get it to work even after a 1 second delay but I think I'll add a few more seconds for safety.
    <pre>Private Sub Workbook_Open()
    Application.StatusBar = "start timer 1 sec...."
    Application.OnTime Now + TimeValue("00:00:01"), "RunInBrowser"
    End Sub</pre>

    Where RunInBrowser() is your code (slightly tweaked for warning messages) in a Module. I also automatically close the workbook after they acknowledge the warning message if run in browser.

    I'll add this little trick to my VBA nice-to-have code stash for future projects. I'll update all the .xls files I have on our web site to include this too.

    Thanks a lot <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>
    Deb

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    The code is NOT determing whether you are in a browser, just that the host app is NOT Excel.

    In practice, that may be all you need.
    If not, the code needs to be refined to explicityly check for browsers.

  13. #13
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    yes true, I agree. For my purposes I just want to know if it's being run from anything other than Excel. I can only think of "other" app being a browser but as long as I can detect any non-Excel app that works for me. I just want to make sure that my VBA will run and it only runs directly from Excel not embedded inside another application. I'll change the name of the sub RunInBrowser() to something more correct.

    Thanks for the clarification.

    Deb

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    I named the critter IsItExcel.

    Actually better woud be to make the critter a boolean function IsExcel or RunningInExcel or ... .

  15. #15
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Detect Excel running in browser (Excel (all))

    One last comment to make it perfect if I may .... <img src=/S/hailpraise.gif border=0 alt=hailpraise width=27 height=22>

    When I detect the .xls is running in some other application, I want to warn the user and automatically shutdown that application (web browser or whatever). I tried Application.Quit and Thisworkbook.Close but that didn't do anything. I suspect it's something with obHost (the .Container) but there's no documentation that I can find to help me figure it out.

    Any ideas? <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

    It's more a nice-to-have since I've already warned the user that it won't work and can continue to do so if they keep using the workbook as-is.

    Thnx,
    Deb

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •