[RESOLVED] Open webpage in existing tab of existing browser
I have a links program that has a ton of links on it. The idea is to click a link and have it open that page in the default browser. It should cannibalize the browser window if one is already open. So far so good; this works fine:
Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpszOp As String, ByVal lpszFile As String, ByVal lpszParams As String, ByVal lpszDir As String, ByVal FsShowCmd As Long) As Long
' Launch default browser
Public Sub OpenURL(ByVal URL As String)
ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus
End Sub
The problem is that this opens a new tab in tabbed browsers. It will quickly become cumbersome to close all those open tabs, so how can I cannibalize an existing tab as well as an existing browser window?
Re: Open webpage in existing tab of existing browser
By using the ShellExecute, it will pass the lpszFile name (by associating its type to an existing registry entry), by execution the lpszOp operation. If you set the vbNull to the lpszOp, the devault operation will be executed.
Because these operations are pre-defined, to open new tabs, it can only be modified in the registry, but its not a great idea to do, because each browser have its own calling scheme, so you could easly ruin up the callings, that is may lead to, you cant use shellexecute on htm files.
It is may be better to find the browser window (if it is already open), and sending messages to the address bar (its text box), that will point on the url you want to display. And then, you have to execute the browser to start download the page. Its just a thought, but i'm sure this is the only way to re-use an existing tab.
A side note: if your code cannot find an existing instance of firefox or IE, or opera (that you can use to pass an url to their address bar), you can simply open up a new instance of any browser, by using the above example you shown. So, in either case your program will display the page, but as long as the user using a browser you have hard-coded to be compatible, the existing tabs will be used as well.
Re: Open webpage in existing tab of existing browser
Quote:
Originally Posted by Jim Davis
It is may be better to find the browser window (if it is already open), and sending messages to the address bar, that will point on the url you want to display. And then, you have to execute the browser to start download the page. Its just a thought, but i'm sure this is the only way to re-use an existing tab.
Anybody know how to do this? I wouldn't even know how to begin.
Or is there another alternative?
Re: Open webpage in existing tab of existing browser
To find a window, you can use the FindWindow api(). The later thing is a bit like blury to me. But i'm sure its possible to do, you just have to dig into the window classes. A window spy would be a nice tool, to find out what classes and objects are used on each browser. I'm also sure you have to write three different method for the three common browsers.
Code:
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
SetwindowText is may be used to fill up the textbox(address bar), you just have to get its hwnd.
Edit: Wrong:(
Quote:
MSDN: However, SetWindowText cannot change the text of a control in another application.
Re: Open webpage in existing tab of existing browser
I'm thinking this is a fairly common problem, so there should (hopefully) be a simpler solution than all that. I do not have or want any browsers other than IE on my machine, and even if I did I don't view writing code tailored to individual browsers as a particularly elegant solution.
Any other ideas? Any brainstorming is welcome.
Re: Open webpage in existing tab of existing browser
Apparently this can be handled by a browser setting.
Open links from external programs:
- In a new browser
- In a new tab
- In an existing browser tab
I'm considering this resolved.
Re: [RESOLVED] Open webpage in existing tab of existing browser
Yeah, the browser have its own settings, to how to handle a page opening, fired by an external source. I just thought you want to implement a feature to your application, that is automatically reuse any existing tab.
However, in the meantime i found that, by using FindWindow() you can find any instance of the common browsers, by their class name. Also, by using the SendMessage() api, you can send keystrokes, just like Ctrl+L (in firefox) that will set the focuson the address bar, then imitate the keystrokes, to write into the url. Sending the enter key will execute the page downloading. You may also have to set the focus on the window, but im not sure its necessarry to filling up the adressbar.
I havent completed this, but theoretically it should work. I will look forward to put any working code to the codebank as soon as i get.
Re: [RESOLVED] Open webpage in existing tab of existing browser
Quote:
Open links from external programs:
- In a new browser
- In a new tab
- In an existing browser tab
i don't believe this will work for firefox as it only has options for an new window or a new tab
Re: [RESOLVED] Open webpage in existing tab of existing browser
There is a quite advanced settings tree, if you type about:config in the address bar.
http://kb.mozillazine.org/Browser.link.open_external
This property will manage how to open a link from external calling. Setting the value to 1 should prevent to opening new tabs, but open the page on the current window/tab.
Re: [RESOLVED] Open webpage in existing tab of existing browser
So Firefox cannot ever re-use browser windows? That doesn't sound right.
EDIT: Thanks, Jim. I knew that didn't sound right.
Re: [RESOLVED] Open webpage in existing tab of existing browser
I'm think this option has been removed in FireFox 2 because of some issues, but it is fixed already. However the option is remained to be invisible, but this way you can set it, i dont see any issues so far.