Is there a way to open an IE window with a user-defined name. When same URL opened, already opened IE window comes to foreground - vb.net

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

Related

vb.net open link in new IE11 tab (instead of window)

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 :)

Working with Internet Explorer Object

My goal is to create a VB application that reads and writes information to various webpages loaded in Internet explorer.
I have created a program that works exactly as I intend in VBA. I am now trying to re-implement the same programming in VB.
I have a function that looks for and returns an Internet Explorer Object where the input matches the LocationName.
Assuming the target page is loaded, I can work with it. Methods such as getElementByID() work perfectly. If the browser window is closed and reopened, and the code is run again, the results are very inconsistent. The getIE function seems to work, but when trying to use methods like document.getElementByID() a NullReferenceException is thrown.
Does anyone know if there is anything I am missing that I need to include to get the document property to update?
EDIT: I have looked over the NullReferenceException article. It hasn't helped me unfortunately. In case I was unclear in my wording, I would like to reiterate that the same bit of code yields a different result when executed the 2nd time under the same conditions (same webpage open in Internet Explorer).
On the second execution IE.locationName is retrievable but IE.Document.title is not. The problem is definitely with the Document property as far as I can tell. I am truly stumped.
Many thanks
Public Function getIE(targetTitle As String) As SHDocVw.InternetExplorer
' Create the shell application and Collection of open windows
Dim shellObj As Object = CreateObject("Shell.Application")
Dim shellWindows As SHDocVw.ShellWindows = shellObj.Windows()
getIE = Nothing
' Scan through the Collection
For I = (shellObj.Windows.Count - 1) To 0 Step -1
' If found, assign this to the function output and exit early
If InStr(shellWindows(I).LocationName, targetTitle) Then
getIE = shellWindows(I)
Debug.Print("Found: " & shellWindows(I).LocationName)
Exit For
End If
Next I
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IE As SHDocVw.InternetExplorer
IE = getIE("my site title")
If IE Is Nothing Then
Debug.Print("Site not open")
Exit Sub
End If
' The code always gets this far, and despite the page being found, starts throwing exceptions from here on if the window has been closed and reopened whilst my application has stayed running.
' Sample form data insertion code
IE.Document.getElementById("textbox1").value = "my value"
' Click the submit button
IE.Document.getElementById("submit").click()
' Wait for page to load
While IE.Busy
End While
IE.Document.getElementById("textbox2").value = "my 2nd value"
' done
IE = Nothing
End sub

How to press Upload button through VBA

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:

Reusing browser/tab in VB.net

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.

Internet Explorer 8/9 Add-On VB.Net - mshtml.HTMLDocument not working. Code enclosed

I'm trying to create a toolbar in Internet Explorer 8 & 9. The main functionality of this is to launch a in-house product when clicked on the icon and convert document accordingly. However, I'm having some difficulty using mshtml.HTMLDocument. Please take a look at my code and advise.
Thanks for helping out
' Get the hWnd value of the IE window that the macro button was clicked within
myhWnd = GetForegroundWindow()
MsgBox(myhWnd)
' for all IE window within the shell window, by the way this
' includes Windows Explorer as well as IE windows!
Dim IE As New SHDocVw.InternetExplorer
Dim SWs As New SHDocVw.ShellWindows
For Each IE In SWs
Doc = IE.Document ' Set the active document
Functions.OutputDebugString("setting active document for printing using set Doc in main")
If TypeOf IE.Document Is mshtml.HTMLDocument Then ' if the document is IE then proceed because EXPLORER window returns in the same group - (This is where I'm having trouble. It keep saying 'Document is not an HTML)
MsgBox("I'm here")
If (IE.HWND = myhWnd) Then ' ENABLE IT AFTER TESTING
Functions.OutputDebugString("JOB SUBMITTED TO QUEUE NOW. PLEASE WAIT.DO NOT CLICK BUTTON AGAIN TO INCREASE SPEED OF OPERATION...")
IE.StatusText = "JOB SUBMITTED TO QUEUE NOW. PLEASE WAIT.DO NOT CLICK BUTTON AGAIN TO INCREASE SPEED OF OPERATION..."
Functions.OutputDebugString("Verifying if the page is still downloading or not.. Do not process if its still downloading, wait in loop for 500 mili seconds")
If Not (IE.Busy = True) Then ' if IE is not currently downloading page then proceed
loopBackfromBusy:
Functions.OutputDebugString("Check in registry about exclusing access to printer")
processFlag = saveFileName(IE.LocationName, Doc.url) ' pass the document URL to give user more informative error messages.
Functions.OutputDebugString("Form_Load() - process flag = ")
If processFlag = True Then ' if no other window is printing then proceed
Functions.OutputDebugString("FILE SUBMITTED FOR PRINTING in form load")
IE.StatusText = "FILE SUBMITTED TO PRINTER FOR PRINTING. DO NOT CLICK BUTTON AGAIN AND WAIT FOR OPERATION FINISH..."
Functions.OutputDebugString("Initializing printer settings using initializeValues()")
intilizeValues(Doc.title) ' initialize printer settings and set everything ready for printer
DefaultPrinter = Printer.PrinterName ' store current default printr name
MsgBox(PrinterName)
Functions.OutputDebugString("Changing current default printer to Print Driver.. Note that this name is case sensitive and must be available on user machine or this software will not work correctly...")
SetDefaultPrinterMyWinNT(PrinterName) ' set the default printer to KC
Functions.OutputDebugString("Fired printing using default IE command")
IE.ExecWB(17, 0) '.... print the document silently
Else
MsgBox("Document is not an HTML document")
End If
The issue is here : If TypeOf IE.Document Is mshtml.HTMLDocument . It keep saying document is not an HTML document. I'll appreciate any suggestion or recommendation.
UPDATE :
Have changed the If condition to
If IE.Name = "Windows Internet Explorer" Then
If (IE.HWND = myhWnd) Then
MsgBox(IE.locationURL)
The above works fine however its not executing the code after which is
IE.StatusText = "JOB SUBMITTED..."
I decided not to check via this If TypeOf IE.Document Is mshtml.HTMLDocument. This worked If IE.Name = "Windows Internet Explorer"