Default browser?

C

Charlotte E.

Does anyone know how to read/find the current users default browser via VBA?

(Internet Explorer, Google Chrome, Mozilla Firefox, Apple Safari, Opera
Browser, etc...)

I'm not talking about opening a website in the default browser, but finding
out which browser the current user is using as the default one...

I'm guessing it must be somewhere in the Registry, under HKEY_CURRENT_USER,
since all users on the same computer are allowed to have their own choice of
default browser - but where?

A search on Google didn't help, but perhaps, I shouldn't get it from the
Registry at all???

So, how to tell which browser is the default one?


TIA,

CE
 
I

isabelle

hi Charlotte,

Sub CheckWebOptions()
Dim wkbOne As Workbook
Set wkbOne = Application.Workbooks(1)
' Determine the target browser.
Select Case wkbOne.WebOptions.TargetBrowser
Case msoTargetBrowserIE6: MsgBox "The target browser is IE6."
Case msoTargetBrowserIE5: MsgBox "The target browser is IE5."
Case msoTargetBrowserIE4: MsgBox "The target browser is IE4."
Case msoTargetBrowserV4: MsgBox "Microsoft Internet Explorer 4.0, Netscape
Navigator 4.0."
Case msoTargetBrowserV3: MsgBox "Microsoft Internet Explorer 3.0, Netscape
Navigator 3.0."
Case Else: MsgBox "The target browser is not in the given list"
End Select
End Sub

isabelle

Le 2014-04-27 08:18, Charlotte E. a écrit :
 
C

Charlotte E.

Thanks, Isabelle, but it doesn't tell me, if it is Chrome, Firefox, Safari,
or another browser, but only support IE...

....and, since Windows know the default browser, it must be possible to get
it in VBA also.

(I did know about that one - as I said, I've already looked around the net
myself)

Thanks for your effort anyway :)

CE
 
A

Auric__

Charlotte said:
Does anyone know how to read/find the current users default browser via
VBA?

(Internet Explorer, Google Chrome, Mozilla Firefox, Apple Safari, Opera
Browser, etc...)

I'm not talking about opening a website in the default browser, but
finding out which browser the current user is using as the default
one...

I'm guessing it must be somewhere in the Registry, under
HKEY_CURRENT_USER, since all users on the same computer are allowed to
have their own choice of default browser - but where?

A search on Google didn't help, but perhaps, I shouldn't get it from the
Registry at all???

So, how to tell which browser is the default one?

Under Windows 7 & 8 (and probably Vista & 8.1):

HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations
\UrlAssociations\http\UserChoice\ProgID

This points to an entry under HKEY_CLASSES_ROOT -- for example, in my Win8
install, it's "IE.HTTP", while in my Win7 install it's "Opera.Protocol".

Under Windows XP (and 2000, if you need to support it):

HKEY_CURRENT_USER\Software\Classes\http\shell\open\command

There's a different key used for what's listed on XP's start menu, but I
don't remember what it is. Google should know, if you need it.

Under Win9x & NT4 (if you need to support *those*):

HKEY_CLASSES_ROOT\http\shell\open\command
 
G

GS

Here's how I check to see what MSO apps are installed...

Const sNames$ = "Internet Explorer,Firefox,Chrome"
Dim oApp As Object, sz
For Each sz In Split(sNames, ",")
Set oApp = CreateObject(sz & ".Application")
If Not oApp Is Nothing Then Beep: Exit For
Next 'sz

...but that won't tell you the default browser. I'm curious why you need
to know this because passing a URL in ShellExecute() opens in the
user's default browser. I suppose you could do this and query its
window title for the browser name, though!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
A

Auric__

GS said:
Here's how I check to see what MSO apps are installed...

Const sNames$ = "Internet Explorer,Firefox,Chrome"
Dim oApp As Object, sz
For Each sz In Split(sNames, ",")
Set oApp = CreateObject(sz & ".Application")
If Not oApp Is Nothing Then Beep: Exit For
Next 'sz

..but that won't tell you the default browser. I'm curious why you need
to know this because passing a URL in ShellExecute() opens in the
user's default browser. I suppose you could do this and query its
window title for the browser name, though!

Maybe documentation that was written differently for various browsers?
 
P

Peter T

Does anyone know how to read/find the current users default browser via

Without reading the registry can use FindExecutable but it needs a file with
an appropriate extension, simplified example

' adapt for 64bit office
Private Declare Function FindExecutable Lib "shell32" _
Alias "FindExecutableA" _
(ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal sResult As String) As Long

Sub Test()
Dim pos As Long
Dim sExt As String
Dim sFile As String
Dim sPath As String
Dim lReturn As Long
Dim sResult As String

sExt = "html"

sPath = Application.DefaultFilePath & "\"
sFile = DummyFile(sPath, sExt, True)
If Len(sFile) = 0 Then
' problem creating file ?
Else
sResult = Space$(260)
lReturn = FindExecutable(sFile, sPath, sResult)

If lReturn >= 32 Then
pos = InStr(sResult, Chr$(0))
MsgBox Left$(sResult, pos - 1)
Else
MsgBox "Error code: " & lReturn
End If
End If

DummyFile sPath, sExt, False

End Sub

Function DummyFile(sPath As String, sExt As String, bCreate As Boolean) As
String
Dim sFile As String
Dim ff As Long

sFile = sPath & "Dummy." & sExt
On Error Resume Next
Kill sFile
On Error GoTo errExit

ff = FreeFile
Open sFile For Append As #ff
Print #ff, "dummy file"
Close #ff

DummyFile = "Dummy." & sExt
errExit:
End Function

Regards,
Peter T
 
C

Charlotte E.

Awesome, Peter - it works :)

And, not only does it tell you the browser, but also the full path to the
browser...

And, I've tested it with all five major browsers: Chrome, Firefox, MSIE,
Opera and Safari..
And, Opera and Safari, I even tried to install on some VERY unlikely
loctions :)

And, tested on both a Danish Windows 7, an English Windows XP, and even on a
Windows Server OS :)
And, it was tested on XL2003, XL2007, XL2010 and XL2013 - and, also in both
Danish and English versions.

So, not matter the browser, the installation, the Excel, the language or the
OS - it just works :)


Thanks, Peter - you saved my day :)


CE





Peter T said:
Without reading the registry can use FindExecutable but it needs a file
with an appropriate extension, simplified example

' adapt for 64bit office
Private Declare Function FindExecutable Lib "shell32" _
Alias "FindExecutableA" _
(ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal sResult As String) As Long

Sub Test()
Dim pos As Long
Dim sExt As String
Dim sFile As String
Dim sPath As String
Dim lReturn As Long
Dim sResult As String

sExt = "html"

sPath = Application.DefaultFilePath & "\"
sFile = DummyFile(sPath, sExt, True)
If Len(sFile) = 0 Then
' problem creating file ?
Else
sResult = Space$(260)
lReturn = FindExecutable(sFile, sPath, sResult)

If lReturn >= 32 Then
pos = InStr(sResult, Chr$(0))
MsgBox Left$(sResult, pos - 1)
Else
MsgBox "Error code: " & lReturn
End If
End If

DummyFile sPath, sExt, False

End Sub

Function DummyFile(sPath As String, sExt As String, bCreate As Boolean) As
String
Dim sFile As String
Dim ff As Long

sFile = sPath & "Dummy." & sExt
On Error Resume Next
Kill sFile
On Error GoTo errExit

ff = FreeFile
Open sFile For Append As #ff
Print #ff, "dummy file"
Close #ff

DummyFile = "Dummy." & sExt
errExit:
End Function

Regards,
Peter T
 
P

Peter T

Charlotte E. said:
Awesome, Peter - it works :)


Glad it works but just seen a stupid omission in the DummyFile routine. Add
the If bCreate pair. Whole point of course being to delete the file when
done, not recreate it again!


Function DummyFile(sPath As String, sExt As String, bCreate As Boolean) As
String
Dim sFile As String
Dim ff As Long

sFile = sPath & "Dummy." & sExt
On Error Resume Next
Kill sFile
On Error GoTo errExit

If bCreate Then
ff = FreeFile
Open sFile For Append As #ff
Print #ff, "dummy file"
Close #ff
End If

DummyFile = "Dummy." & sExt

errExit:
End Function
 
G

GS

Charlotte E. said:
Glad it works but just seen a stupid omission in the DummyFile
routine. Add the If bCreate pair. Whole point of course being to
delete the file when done, not recreate it again!


Function DummyFile(sPath As String, sExt As String, bCreate As
Boolean) As String
Dim sFile As String
Dim ff As Long

sFile = sPath & "Dummy." & sExt
On Error Resume Next
Kill sFile
On Error GoTo errExit

If bCreate Then
ff = FreeFile
Open sFile For Append As #ff
Print #ff, "dummy file"
Close #ff
End If

DummyFile = "Dummy." & sExt

errExit:
End Function

I was going to suggest same, but thinking this function isn't really
necessary if you have a list of executable filenames!<g>

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

but thinking this function isn't really necessary if you have a list
of executable filenames

Uh.., I stand corrected if one doesn't already have a procedure for
creating files! Since I already have this the 'Kill' statement would be
included in the 'GetExecutable$()' function so it runs stand-alone and
callable to return the info.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
P

Peter T

GS said:
Uh.., I stand corrected if one doesn't already have a procedure for
creating files! Since I already have this the 'Kill' statement would be
included in the 'GetExecutable$()' function so it runs stand-alone and
callable to return the info.

Gary, not sure I follow but what I posted was only intended as example,
adapt as required, typically as a function to receive an extension and
return the exe name possibly with its path. If you know the location of a
file with the extension no need to make a dummy, or maybe adapt to create a
dummy optionally if a known *.ext doesn't exist (and kill it of course!).
All in the one function or two as I posted is only style.

Charlotte, although it's worked 100% in your testing it can fail, not least
if there is no file association (though unlikely not to have a default
browser). Return values 1 to 31 are error codes to inform what the problem
was and there's a list out there somewhere if you search FindExecutable.
Don't forget to adapt for 64bit if necessary including the return var as
LongPtr.

You didn't say what the objective was but if to launch the default browser
with a given file simply use ShellExecute.

Regards,
Peter T
 
G

GS

Gary, not sure I follow but what I posted was only intended as
example, adapt as required, typically as a function to receive an
extension and return the exe name possibly with its path. If you know
the location of a file with the extension no need to make a dummy, or
maybe adapt to create a dummy optionally if a known *.ext doesn't
exist (and kill it of course!). All in the one function or two as I
posted is only style.

Thanks, Peter! I got that after realizing not everyone has file I/O
wrappers as I have. In the interest of keeping the procedure 'generic',
though, it's clearly better to use a temp dummy file as you exampled,
IMO.

Nice utility to have in my "mFunctions.bas". Thanks for sharing it!!!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
C

Charlotte E.

Hi Peter,
although it's worked 100% in your testing it can fail, if there is no file
association

I know, and I've already adapted the function to take this into
consideration :)

The purpose for the function is in connection with working with IE
Automation, if you use IE as hidden during the process.
Sometimes, if the IE automation fails, IE might not get closed properly, and
you can end up having a number of IE process running in the background, with
no purpose, taking up resources.
So, I've made a little macro, which kills all running IE processes.
BUT! I don't want to kill all running IE processes, if IE is the default
browser, because then I might kill an open IE window, which the user has
opended on purpose, and want to keep open - thus, the need to know the
default browser of the system.

PS: And, the function also works with Avant Browser :)

Once, angin, thank you for your help and effort...

CE



Peter T said:
Gary, not sure I follow but what I posted was only intended as example,
adapt as required, typically as a function to receive an extension and
return the exe name possibly with its path. If you know the location of a
file with the extension no need to make a dummy, or maybe adapt to create
a dummy optionally if a known *.ext doesn't exist (and kill it of
course!). All in the one function or two as I posted is only style.

Charlotte, although it's worked 100% in your testing it can fail, not
least if there is no file association (though unlikely not to have a
default browser). Return values 1 to 31 are error codes to inform what the
problem was and there's a list out there somewhere if you search
FindExecutable. Don't forget to adapt for 64bit if necessary including the
return var as LongPtr.

You didn't say what the objective was but if to launch the default browser
with a given file simply use ShellExecute.

Regards,
Peter T
 
G

GS

You can set a fully qualified object ref to your CreateObject()
instance so you never have to worry about other running instances. You
can even make it run 'like' it's an in-process app extension so it
closes automatically when your app closes. I do this for my apps that
use a html.exe as a userguide. My app tracks any running instance it
creates via its hwnd to ensure there's only 1 instance being used
whether it's already running or not. Very easy to do with any EXE,
really!<g>

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
P

Peter T

GS said:
You can set a fully qualified object ref to your CreateObject() instance
so you never have to worry about other running instances. You can even
make it run 'like' it's an in-process app extension so it closes
automatically when your app closes. I do this for my apps that use a
html.exe as a userguide. My app tracks any running instance it creates via
its hwnd to ensure there's only 1 instance being used whether it's already
running or not. Very easy to do with any EXE, really!<g>

For html.exe probably best to close it (if open) with the help api, first
HH_CLOSE_ALL and then HH_UNINITIALIZE (the latter should be done anyway even
if not currently open but if earlier initialized)

Regards,
Peter T
 
G

GS

For html.exe probably best to close it (if open) with the help api,
first HH_CLOSE_ALL and then HH_UNINITIALIZE (the latter should be
done anyway even if not currently open but if earlier initialized)

Not sure you understand me. I have created a pure VB wrapper that
allows me to manage my stand-alone EXE user guide 'as though' it was a
CHM running 'in-process'. It does not use the help API, but does use
the following API functions...

from user32.dll:
EnumWindows
GetClassName
GetWindowText
ShowWindow
SetForegroundWindow
IsIconic
IsWindowVisible
SendMessage

from shell32.dll:
ShellExecute

...to keep track of the EXE it's managing. All that's required to use it
is the full filename of the EXE!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top