using ppDisp to take control of popup windows in userform - vba

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

Related

How to hide or cover a MS Access ActiveX Web Browser control?

I have a database in which users enter a value into Textbox1 on a form; the form also has an ActiveX Web Browser control (acxWebBrowser1), plus another blank Textbox2, and finally a command button. After the user enters text into Textbox1 and clicks the command button, the VBA code will navigate to a specific web page in acxWebBrowser1, enter the Textbox1 value into the web page form, and then obtain a result in the web page in acxWebBrowser1. The result is then grabbed by the VBA code and entered into Textbox2 on the database form.
What I need to be able to do is hide the ActiveX Web Browser control to (a) prevent the user from messing with the web page, and (b) to prevent the clutter and distraction of the ActiveX Web Browser since the VBA code does all the necessary interactions with the web page behind the scenes.
Problem is, I cannot get the acxWebBrowser1 to stay hidden behind a rectangle box control on the form because the browser automatically moves itself to the top of the stack of controls. If I reduce the size of the acxWebBrowser1 control to be very tiny, the web page does not function properly. If I make the web browser control not visible, then the code cannot grab the needed values. I cannot use a POST approach to avoid using the web browser. (I might be wrong about not being able to use POST--maybe someone can point me to how to do that if that is the way to go.)
How can I hide or put something on top of the web browser control?
Three hours later, it dawned on me how to accomplish hiding the ActiveX Web Browser but still get it to be accessible to the VBA code. Simple: Don't update the screen when running the code to access the ActiveX Web Browser and use the .Visible property to "show" the Browser Control when VBA needs to access it (but it won't be visible to the user if screen updating is off) and then hide the Browser Control when VBA is done accessing it.
Here's the code I used to accomplish that:
Application.Echo False 'turn off screen updating
Me.acxWebBrowser1.Visible = True 'make web browser control "visible" to VBA code
{do stuff, like: process web page}
Me.btnClickMe.SetFocus 'set focus on the button so we can hide the web browser control
Me.acxWebBrowser1.Visible = False 'make web browser invisible so user is not distracted
Application.Echo True 'turn screen updating back on
Me.Refresh 'refresh the screen; this is probably not necessary
Just be
absolutely sure to turn screen updating (echo) back on
You should send any error handling to code that sets Application.Echo True otherwise you will not be able to see or do anything if the code crashes after setting echo to False.

Application fail to exit when a runtime created object is used

I'm using vb.net 2013, and I have configured Shutdown mode to "When last form is closed".
On my main form, I have a menu item which has this code to close the application:
Application.Exit
Everything is working fine, except one case:
When I open a specific form, where a Combobox is created on runtime and I've used Addhandler to subscribe to several events.
The combobox is created when pressing a button.
When I open this form and I don't create the combobox, everything is working ok. If the combobox is created, when I close this form and try to close the application using the menu item, nothing happens. The application is not closed and no error message is displayed. (the same situation occurs when I try to close the main form with "x" button)
On the form's (where I have the combobox) close event , I tried to put a line of code:
MyCombobox.dispose()
But the situation is the same.
What can I do? Thank you!
What I do from my little experience is
1.) remove the MyCombobox from its parent control (I'm think this is in your combobox close event).
2.) set the MyCombobox to Nothing
3.) Dispose() it.
MyCombobox = Nothing
MyCombobox.Dispose()
It would be useful to see some part of your code for the close event so we can help you check. More power to you!
Update based on OP's comment:
I have read the following from MSDN: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing(v=vs.110).aspx
From this, it is important to note that:
The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.

Enable Tab Key in my Web Browser Control

I have a web browser control in a custom task pane control (User Control) and I open it as a sidebar as soon as my Outlook opens (I have created it as an Outlook Addin using Visual Studio 2013). The web browser control has a login form inside it and I would like to place focus on an input control in my web browser control as soon as Outlook opens. I have tried several tricks like placing focus on the custom user control and then placing focus on the input control after the form has loaded but it doesn't place focus on it. Also I have been trying to allow the use of Tab and Delete keys to work inside the web browser control so that I can tab to other controls and play with it like we would do it on a normal browser window. Kindly let me know how I can achieve this as I am out of ideas.
Cheers.
Try to use the Excel WebBrowser Control instead of the System.Windows.Forms WebBrowser; it handles special key forwarding as TAB, DEL, CTRL+V, etc.
For that change the WebBrowser contructor from
new System.Windows.Forms.WebBrowser();
to
new Microsoft.Office.Tools.Excel.Controls.WebBrowser();
You would need to add references to your project:
Project/Add Referernce/Extensions select
Microsoft.Tools.Outlook & Microsoft.Tools.Outlook.v4.0.Utilities
Doc: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx
You can only do that if you install a Windows hook (SetWindowsHookExW(WH_GETMESSAGE, YourHookProc, 0, GetCurrentThreadId()) and in the hook proc detect that messages are supposed to go to your browser, and redirect them accordingly.
It works partially this way but not 100%. First I had to set the TabStop property to True for my webbrowser control and then just set the focus once the document was loaded and that was it. The tab, delete, backspace keys all ran properly.
Private Sub CustomTaskPane_Load(sender As Object, e As EventArgs) Handles Me.Load
TestWebBrowser.Size = New Drawing.Size(Me.Width, Me.Height)
Me.Focus()
TestWebBrowser.Navigate(url)
WaitForPageLoad()
TestWebBrowser.TabIndex = 0
TestWebBrowser.TabStop = True
End Sub
Private Sub TestWebBrowser_DocumentCompleted(sender As Object, e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles TestWebBrowser.DocumentCompleted
' Now set the focus on the Web Browser Control
TestWebBrowser.Focus()
End Sub

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.

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?