I am using Process.Start() to open a URL and it's great for a one-time use but if opening multiple URLs it creates either a new instance of the default browser or uses a new tab. I need to use the original tab.
Suggestions?
For Internet Explorer, you will need to reference the shdocvw.dll COM component located, by default, at c:\windows\system32\shdocvw.dll. This COM component contains a ShellWindows object that you can use to determine if there is a running instance of Internet Explorer or not, like this:
Dim internetExplorerInstances As New ShellWindows()
Dim foundIE As Boolean = False
For Each ie As InternetExplorer In internetExplorerInstances
If ie.Name = "Windows Internet Explorer" Then
ie.Navigate(ur, &H800)
foundIE = True
Exit For
End If
Next
If Not foundIE Then
' Either do nothing or use Process.Start with a new browser instance
End If
For the other browsers, you are, unfortunately,out of luck, programmatically.
Related
Here is my requirement which I intend to implement. There is one window application showing some icons which take me to different web sites. When I click on one of the icons, it should open an IE window and append a customized name to it.
So, before opening any website after I click on website icon, I want to check if there is already an IE window open with that customized name, if yes, bring that already opened window to the foreground. If not, open a new IE window.
I have checked various questions posted which are related to what I am looking to achieve, but am somehow not able to get it right. Below is my attempt.
For Each e In shellWins
If InStr(1, e.GetProperty("IEWindowName"), namedWindow, CompareMethod.Text) <> 0 Then
hWnd = e.HWND
myIE = e
End If
Next
If hWnd == -1
Dim p As New Process
Dim psi As New ProcessStartInfo(IEPath, webSiteURL)
p.StartInfo = psi 'Trying to open a new IE window
p.Start()
For Each ie In shellWins
If ie.hwdn = p.MainWindowHandle Then
ie.PutProperty("IEWindowName", namedWindow)
End If
Next
End if
Else
myIE.BringToForeground()
This sometime works and sometimes does not. Is there any better way to do it?
It doesn't work in which situation? Does there any error throw when it doesn't work? If there is, please tell us the detailed error information and in which line it occurs.
Besides, you could try to compare the url to check if the website is already open in IE like this:
Sub Main()
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
shellWins = New SHDocVw.ShellWindows
Dim SQuery As String = "https://www.example.com/"
For Each explorer In shellWins
If explorer.Application.Name = "Internet Explorer" And explorer.LocationURL.Contains(SQuery) Then
explorer.BringToForeground()
End If
Next
shellWins = Nothing
explorer = Nothing
End Sub
I've seen a lot of examples using JavaScript to run a popup window to remove address bar. My issue is I want to run a full website [that I've created in ASP.net] without the address bar visible from a Windows Form application.
I've tried:
Dim objIE As Object 'InternetExplorer 'or as object - if you want to keep it lite and don't add the reference
objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
objIE.AddressBar = False
objIE.MenuBar = False
objIE.ToolBar = False
.Navigate2(Address)
End With
objIE.Navigate = Address
but I get an error at CreateObject of Cannot create ActiveX component.
So my current working code to run the website is:
Dim process As New System.Diagnostics.Process()
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
Dim sInfo As New ProcessStartInfo(Address)
Process.Start(sInfo)
However, the address bar is visible.
Any suggestions?
That code should work fine, as I just tested it myself. I would say start a fresh VB file (console application/windows form, whichever you'd like) and try to run just these:
Dim objIE as Object
objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
End With
If it does not run from there, then your best bet is to try to "Reset" your internet explorer, which can be done through the Settings/Options. I've done this in the past and it allowed to work. The fact that you cannot create the object is the concerning part, as that's being written correctly and when I run that on my PC it works.
Give those two a shot (try a fresh file, and reset internet explorer). And let me know if that works.
I need to force my application to open few links (there are few buttons - one link under one button) in IE11 (regardless if it's set as default browser or not).
I tried multiple ways, but the result is always the same like with:
Process.Start("iexplore.exe", address)
It works perfectly for Firefox, or Chrome, but for IE - it always opens new window for each link. Regardless IE is already opened or not.
Few years ago I wrote similar thing which worked with IE7 I guess...:
Imports SHDocVw
Dim internetExplorerInstances As New ShellWindows()
Dim foundIE As Boolean = False
foundIE = False
For Each ie As InternetExplorer In internetExplorerInstances
If ie.Name = "internet explorer" Then
ie.Navigate(address, &H800)
foundIE = True
Exit For
End If
Next
If Not foundIE Then
With oPro
.StartInfo.UseShellExecute = True
.StartInfo.Arguments = address
.StartInfo.FileName = "iexplore"
.Start()
End With
End If
But today, with IE 11 it doesn't work.... I mean it still opens URLs in new windows.
Do you have any ideas how to programatically force IE11 to open link in new Tab, not in new window?
Generally above code is OK except one detail - it should be:
If IE.Name = "Internet Explorer" Then... it is case sensitive.
Finally my code looks like this:
Imports SHDocVw
Const openInTab As Object = &H800
Dim internetExplorerInstances As New ShellWindows()
Public Function IsProcessOpen(ByVal name As String) As Boolean
For Each clsProcess As Process In Process.GetProcesses
If clsProcess.ProcessName.Contains(name) Then
Return True
End If
Next
Return False
End Function
(...)
And call:
If IsProcessOpen("iexplore") Then
For Each IE As InternetExplorer In internetExplorerInstances
If IE.Name = "Internet Explorer" Then
IE.Navigate2(address, openInTab)
Exit For
End If
Next
Else
Process.Start("iexplore", address)
End If
And it works :)
I'm looking for help (or suggestions/leads) on the following issue.
This console app:
looks for an InternetExplorer object,
when found, creates a new ListenerExplorer object
in which an event handler is activated (TitleChange)
Now, when you run this application while an explorer (not IE, but a general explorer.exe) is active and you navigate to a folder (this triggers a TitleChange event) the explorer thread is blocked. It is not possible to navigate further in explorer until the VB app is closed.
This pause occurs in the TitleChange sub, on the line
Console.WriteLine("URL=" + objIE.LocationURL)
Getting the LocationURL property outside of the event works fine. Within the event, it blocks the explorer thread.
What caught my attention is that in Visual Studio's immediate window, on a breakpoint on the line above, i'm unable to access the same property:
?objIE.LocationURL
Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation.
Code:
Dim objIE As SHDocVw.InternetExplorer
Sub Main()
Dim objShellWindows As SHDocVw.ShellWindows = CreateObject(“Shell.Application”).Windows
If (objShellWindows.Count > 0) Then
objIE = objShellWindows.Item(0)
AddHandler objIE.TitleChange, AddressOf TitleChange
'wait for events until a key is pressed by the user
Do
If Not (Console.ReadKey.KeyChar = "") Then Exit Do
Loop
End If
End Sub
Private Sub TitleChange(title As String)
Console.WriteLine("TitleChange: " + title) 'works fine
Console.WriteLine("URL=" + objIE.LocationURL) 'blocks the explorer thread!
End Sub
I've Googled for days, but can't fix it... Any help would be greatly appreciated! Thanks
A console application needs a message loop.
Solved by creating a Windows Forms Application project in MS Visual Studio instead of a Console Application project.
I was searching a lot on the web and found your website very helpfull - that is why I have not posted anything yet, just reading.
However, I need your advice in the following step of my project. The porject is to automate the creation/placing of ad for selling goods.
The website is http://olx.bg/adding/
What I am failing to do through VBA is to press the blue square called "ДОБАВИ СНИМКА" which stands for "ADD PHOTO" in the form.
Once pressed a default BROWSE window opens to select the file.
Thanks in advance!
Add two references to your VBA project:
Microsoft Internet Controls
Microsoft HTML Object Library
The HTML behind the button has an id = add-files which makes it pretty easy to locate using getElementById
Here is a working example:
Sub PushAddPhotoButton()
Dim IEexp As InternetExplorer
Set IEexp = New InternetExplorer
IEexp.Visible = True
IEexp.navigate "http://olx.bg/adding/"
Do While IEexp.readyState <> 4: DoEvents: Loop
Dim photoButton As HTMLInputElement
Set photoButton = IEexp.Document.getElementById("add-files")
If (Not photoButton Is Nothing) Then
photoButton.Click
Else
MsgBox "Button not found on web page."
End If
IEexp.Quit
Set IEexp = Nothing
End Sub
If your IE security settings are on 'High' for Internet then you won't get a dialog or an error. The IE window will simply flash and then be gone.
Change to: