webbrowser control on page load or refresh - vb.net

I have a WebBrowser control on my Vb.net winform. Now i need to find a way to detect if the page reloads or refreshed.
Similar to WebBrowser1_DocumentCompleted.
Example:
WebBrowser1_DocumentOnLoad, OR WebBrowser1_DocumentOnRefreshed

dim wb as new webbrowser
wb.navigate(url)
do while wb.ReadyState <> WebBrowserReadyState.Complete
application.doevents()
loop
wb.refresh()
do while wb.ReadyState <> WebBrowserReadyState.Complete
application.doevents()
loop
msgbox("refreshed")

You need to intercept the BeforeNavigate2 event, and if it is directed to the exact same URL that is currently loaded in the WB control (BN2 event provides a URL argument for you to read from and test, and your WB.LocationURL or WB.Document.URL can tell you the current URL of your WB control so you can compare it to.
So, when you intercept the BeforeNavigate2 event, you can do your comparison there, you can also do it during DocumentComplete, whatever you prefer. There is no OnRefresh event unfortunately (that I know of) so give my method a try and let me know how you get along with it, and if you need any further help.
Cheers.

Related

vb.net - showdialog() sends main window to background

I have a modal window popup so a user of my application can edit some stuff, then they save it and close the window. When they close the popup window, my parent (main) window gets sent to the back of all other applications on my desktop, and then it immediately gets sent back to the front.
Any idea why this would happen?
In your main form:
Dim frmDlg as New FormDialogToShow
frmDlg.ShowDialog(Me)
The main form should not get sent to the back. The child dialog will display on top of the parent. Without the owner reference, the mainform can sometimes get sent to the back. When you dont specify an owner form and that happens:
Dim frmDlg as New FormDialogToShow
frmDlg.ShowDialog()
Me.BringToFront
(the answer is the same as the first time)
Does your modal form somehow hide itself before ShowDialog line ends? This happened to me and was able to solve it by removing the Hide call from the modal form.
I think I read somewhere here in SO that this happens because Windows does not have an enabled window to send focus to in the active app so it sends focus to the next app instead.
This code seems to solve the problem:
' When closing the subform
' ------------------------
sub_form.close()
main.focus()
sub_form.dispose()
When doing this, my main form does not get sent to the back even when the sub form is modal window.
I was searching desperately to find the answer to a similar problem. I found this to be particularly useful:
Private Sub Frm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Prompting = False
Frm = Nothing
FrmPrompt.Close()
FrmPrompt.Dispose()
FMain.Activate()
End Sub
Activate allowed my main form to not be sent behind anything else I had open.

Powerpoint Event handling - Prevent New Presentation

I am creating a Powerpoint Add-in. I would like to restrict user from either:
Create New Presentation
Open an existing Presentation
I have used this tutorial/overview to trap the NewPresentation and PresentationOpen events. I would like to close any presentation initialized through these events before the user can interact with it. When I try to close it using .Close method, I receive an error (screenshot below).
In my class module, I have the following to trap the NewPresentation event. This works fine, I receive the message box and Pres is a valid Presentation object that can be passed to the CloseNewPres routine.
Private Sub PPTEvent_NewPresentation(ByVal Pres As Presentation)
MsgBox "You cannot use this Charting tool with multiple presentations.", vbInformation
CloseNewPres Pres
End Sub
In a standard module, I have the CloseNewPres routine, which I expect to close the "New" presentation:
Sub CloseNewPres(Pres As Presentation)
Application.Presentations(Pres.Name).Close
'Pres.Close '<~~ This also fails.'
End Sub
I receive the following error.
Any thoughts on why this is happening? Or what I can do to close these presentations?
In my opinion you need to use another event which is quite similar to one you used:
Private Sub PPApp_AfterNewPresentation(ByVal Pres As Presentation)
If I set Pres.Close within proposed one it really closes new created presentation.
The best explanation I have (and this is my own interpretation) is that it's barking because you're trying to remove an object (the presentation) while the event handler is dealing with it. Kind of asking the event handler to pull the rug out from under its own feet.
By the way, it won't work to call another routine from within the event handler and have IT do the deed because the event handler's still active.
One way around this:
Have the event handler load a form modelessly.
When you do that, the remaining code in the event handler runs to completion.
The form's initialization code can close the presentation if your conditions are met.
The form needn't ever become visible for this to work.
Yep. Seems bizarre. But it works.
In C#.Net you can do by this way,
using pp = Microsoft.Office.Interop.PowerPoint;
pp.Application app = Globals.ThisAddIn.Application;
app.AfterNewPresentation += Event_PresNew;
public void Event_PresNew(pp.Presentation pres)
{
//.....your code here....
}
Also you can see the list of all the events you can use in powerpoint here below,
https://learn.microsoft.com/en-us/office/vba/api/powerpoint.application.newpresentation(even)

using ppDisp to take control of popup windows in userform

I have setup a userform1 to browse an intranet using web browser controls in VBA/Excel. The problem I am having is that when the user initiates a popup through the web browser control, that popup runs in IE by default, outside of the scope of the web browser control, and therefore doesnt contain the correct session data in the popup. This popup initiates from a dropdown box, onchange command and then inserts the input from the popup back into a web form on the page. The code below intercepts the popup event and lets you handle it, by transferring it to say, userform2
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
Dim frm As UserForm2
'Dim ppDisp As Object
Set frm = New UserForm2
Set ppDisp = frm.WebBrowser1.Application
frm.Show
End Sub
Problem is, it gets stuck right now on frm.show, when I pause, and doesnt seem to be actively transferring over the web page correctly into userform2. I'm not sure where my logic is wrong here, any advice would be helpful. Most guides have shown:
Set ppDisp = frm.WebBrowser1.object
But I cant find that in the object browser anywhere, and doing .object bombs out, as error 438: object doesnt support this property or method. But everything I could find on this so far shows using .object.
for anyone reading this, VBA wont support this method as the above says. It works perfectly in .net and I would assume then, vb6

Making a form halt other program activity like a messagebox (in VB.NET)

I want to create a custom form (in visual basic .NET) that will stop other process responsiveness until the form is acknowledged. It would be a nice bonus if I can add a beep when trying to access the main program UI while this form is displayed as well (like how a messagebox does).
My initial idea was to create another thread for the messagebox-type form and have the main thread sleep until the messagebox-type form is responded too, however I think this would create a bug-like appearance on the main program as it simply wouldnt respond or update its UI (also worth noting, I have little experience working with multithreading, so this may be incorrect).
I really don't have much idea of how to proceed with this, any ideas and/or guidance are greatly appreciated. thank you! :)
I think that this type of behavior is that for which there is ShowDialog()
this sample is on the MSDN page for ShowDialog()
Public Sub ShowMyDialogBox()
Dim testDialog As New Form2()
' Show testDialog as a modal dialog and determine if DialogResult = OK.
If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
' Read the contents of testDialog's TextBox.
txtResult.Text = testDialog.TextBox1.Text
Else
txtResult.Text = "Cancelled"
End If
testDialog.Dispose()
End Sub 'ShowMyDialogBox
When the code calls ShowDialog() the program continue on the testDialog and exits only when your user press OK, Cancel, Close or whatever method, which, in the called dialog, set the property DialogResult to any value different from DialogResult.None.

Operating on a web page using the Document Object

I have a VB.Net app that needs to print a bunch of pages from a secure intranet site; the app was made and was working for a non-secure site but now the app needs to login before accessing the web pages necessary to print.
I get the error "Object Reference not set to an instance of an object" when I use my new Sub which follows:
Private Sub SiteLogin()
WebBrowser1.Navigate("http://url/login/")
WebBrowser1.Document.GetElementById("user").SetAttribute("value", "username")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "mypassword")
WebBrowser1.Document.GetElementById("submit").InvokeMember("click")
End Sub
I got the code from this VB tutorial, but I seem to be missing something in how I'm referencing the WebBrowser.Document object. I've never used VB.Net before so I don't know much about this problem, I have the following sub which DOES work from the original program however:
Private Sub WebPrint()
WebBrowser1.Navigate(strUrl)
WebDocLoaded = False
'lblMessage.Text = "Waiting for report page to load"
Do Until WebDocLoaded
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
This Sub prints the given page in IE, so I figured I could use the same WebBrowser1 object for the login Sub, as I don't know how to or whether I should declare a separate object for the SiteLogin Sub. Both of these functions are called after the WebBrowser1 object is defined however.
I just need to be able to submit a simple login form to a set URL, and this info needs to all be hardcoded (there's no option to set the location or login info nor should there be). If there's another way to log into this site via VB.Net or if I need to give more information let me know.
.Navigate returns before the page load is complete so the DOM is not ready & .Document is not accessible.
See; How to wait until WebBrowser is completely loaded in VB.NET?