4

I need to open a url in the default web browser using Excel VBA. It should work both in PC as well as Mac.

I also need to get hold of the browser instance so I can access its document object and read some of the html elements or anything from the page rendered in the browser.

I know how to open InternetExplorer using CreateObject which gives you back the browser instance. Maybe in Mac I can open the Safari browser the same way. But I want to open the default web browser and also get the browser instance.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • 1
    one way to go about is to do an initial test in VBA to see if you are on a MAC or PC (`Application.PathSeparator` is one such way to tell), then call different subs based on which the user is on. AFAIK, VBA on a PC is only capable of manipulating IE, so the defauit browser idea may not be achievable. [Here is some code](http://www.ozgrid.com/forum/showthread.php?t=164655) that can get you started with a Mac (uses VBA with AppleScript) – Scott Holtzman Dec 24 '15 at 15:56
  • 1
    launch the url from the command line and let the computer figure out the default browser. `CreateObject("WScript.Shell").Run "www.google.com"` on Windows. I don't know about [mac](http://stackoverflow.com/a/12320294/5090771). – WhiteHat Dec 24 '15 at 21:51
  • Can you use this to open the webpage and set a proxy as well as the user agent? Looks like you can set the user agent in the header ([see link](https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.followhyperlink)) but just not sure of the syntax to do so. – PeterB May 03 '19 at 03:44

1 Answers1

1

I'd personally let Excel figure it out:

ActiveWorkbook.FollowHyperlink "https://www.google.com"

I can't test whether this works on a Mac (but I don't see any reason why it wouldn't).

Note that if you want to use the default browser, you won't be able to ensure that you can even get an object instance (i.e. Chrome, Firefox, Opera, etc., etc.). I'd display for the user in the browser, then grab the html from the same link concurrently with some other method.

Comintern
  • 21,855
  • 5
  • 33
  • 80