detect event on IE from visio - vba

Can i have a link between a button on an IE page and a visio event ? ( for example : changing the color of a shape just by a click on a button on the IE page)

Not really very easy unless you have access to the HTML content in IE as well, but you could use a VBA class which implements a "withevents" private variable to capture a reference to a particular element on the page, and which has an event handler to respond to browser-based events. Eg. in a class "clsHTML":
Private WithEvents el As MSHTML.HTMLInputElement
Public Sub SetElement(t As MSHTML.HTMLInputElement)
Set el = t
End Sub
Private Function el_onchange() As Boolean
Debug.Print "captured change: value = " & el.Value
End Function
In other code, create an instance of the class and call "SetElement" using a reference to an element on the page in IE:
Dim objHTML As clsHTML 'global variable
Sub TestEvents()
Dim IE As Object
'set up your IE reference....
Set objHTML = New clsHTML
objHTML.SetElement IE.document.getElementById("tester2")
Debug.Print "set capture"
End Sub
In this instance you're capturing the "change" event on a textbox, but other elements will expose different events....
Edit: I tested this in Excel, but I'm assuming something similar will also work in Visio.
Edit2: you would probably be much better off creating a form in Visio to handle this than sticking with automating IE.

yes you should check the get started documentation of jquery
html :
<button id="mybutton" />
<div id="myshape">blabla</div>
javascript :
$('#mybutton').click(function() {
$('#myshape').css('background-color', '#555555');
});

Related

Using VB .net to click command button in Access

I'm trying to click a button in a form in MS Access with VB .net. However, there isn't much I can find in this area and have a bit of a long way of getting the button. Then I'm stuck - there seems to be no way to activate the click event.
Using :
Imports Microsoft.Office.Interop
I have the following to get the button:
Dim acc As New Access.Application
acc.OpenCurrentDatabase("C:\path\to\db\aDatabase.accdb")
acc.Visible = True
For i = 0 To acc.Forms.Count - 1
If acc.Forms.Item(i).Name = "formName" Then
For j = 0 To acc.Forms.Item(i).Controls.Count - 1
If acc.Forms.Item(i).Controls.Item(j).name = "btnEnter" Then
Dim btn As Access.CommandButton = acc.Forms.Item(i).Controls.Item(j)
'
' click on button??
'
End If
Next
End If
Next
I've had a guess at trying the following:
acc.Application.Run(btn.OnClickMacro)
acc.Application.Run(btn.OnClick)
btn.OnClickMacro
btn.OnClick
btn.performclick()
none of which work.
By default, the click event handler is declared in VBA as
Private Sub Command0_Click()
' do something
End Sub
You won't be able to programmatically click the button outside of Access' VBA code if it's private (unless the developer intentionally declared it public). Best practice is to make a function which is called by the handler, which can be called elsewhere as well, instead of clicking buttons through code.
Private Sub Command0_Click()
DoSomething
End Sub
' you would have a better chance calling this from .NET
Public Sub DoSomething()
' do something
End Sub
If you don't have access to the Access VBA code, outside of .NET you could use something to automate mouse clicks like this: https://autohotkey.com/
I mean, if you have to have Access intslled, open and running, then why is such code not being run from Access? So it does not make a whole lot of sense to call such code from .net.
However, simply make the private sub click as public, and then you can use this form:
MyAccessApp.Forms("main").command44_click
So you don’t have to create any external functions etc., but if you wish to call that code directly then the form MUST be opened, and with a running instance of that application from .net, then the above syntax will work.
I would suggest you just use Access here, and it nots clear why .net is involved.

VBA IE automation, To do events like displaying message,etc in excel when a link in webpage is clicked

My requirement is i need to have a VBA code in excel which runs to do some action in excel when a link in webpage is clicked.For example when i click a link "login" in webpage my VBA code should press printscreen button in keyboard(like we use sendkeys).
I have been searching for this solution since two weeks over google but failed to get one.This is urgent project requirement, please save my day,thank you very much in advance.
Here's an edited version of an answer I gave to a similar question (http://stackoverflow.com/questions/12877872/possible-to-intercept-onchange-event/12927960#12927960).
Project references set for "Microsoft HTML object library" and "Microsoft internet controls"
In a class module "clsEvents":
Option Explicit
Public WithEvents href As MSHTML.HTMLAnchorElement
'Note that having defined "href" as "WithEvents", if you choose "href"
' from the left-hand dropdown at the top of the class code module
' then the right-hand dropdown will populate with events you can select
' from to create an event-handling procedure in the class
Private Function href_onclick() As Boolean
Debug.Print "link clicked"
href_onclick = False 'cancels the navigation
End Function
In a regular module:
Option Explicit
Dim evt As clsEvents
Sub Setup()
Dim IE As New InternetExplorer
Dim el2 As Object
Set evt = New clsEvents
IE.Visible = True
IE.navigate "http://www.csee.wvu.edu/~riggs/html/select_example.html"
Do While IE.Busy
Loop
Set el2 = IE.document.getElementsByTagName("a")(1)
If Not el2 Is Nothing Then
Debug.Print "setting event capture on link:" & el2.innerText
Set evt.href = el2
End If
End Sub
If you run the Setup sub and then click the "Javascript" link on the page in IE you should see output in the Immediate window of the VB editor.
Hope that helps get you started.
There is nothing native you can do inside Internet Explorer to enable this functionality.
You would need to write an ActiveX control (using C#, C++, VB6, etc.), which would perform the Excel automation.
A few useful links:
How to automate Excel from VB6
How to write an updatable ActiveX control in VB6

How to access to the properties of an UserControl from code side?

make my own UserControl and I can aggregate new TabPages to a TabControl and then, inside of then TabPage, I add my own UserControl using the following code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim TabX As New Windows.Forms.TabPage("Tab " & TabCount.ToString) '(ConfiguracionTabPage)
Dim MyControl As New ClientesEmpresa
MyControl.Name = "Control" & TabCount.ToString
If ClientesTabControl.TabPages.Count = 10 Then
ClientesTabControl.TabPages.RemoveAt(9)
End If
TabX.Controls.Add(MyControl)
TabX.Name = "Tab" & TabCount.ToString
TabX.Text = "Tab" & TabCount.ToString
MyControl.TitularLbl.Text = "Coca Cola"
Me.ClientesTabControl.TabPages.Insert(0, TabX)
Me.ClientesTabControl.SelectedIndex = 0
TabCount += 1
End Sub
My user control have several Labels, TextBox and TabPages(inside of a TabControl).
Now I want to change some properties dynamically from the source code, but I don't know how to access them.
The most similar theme that I found is this How to Acces of an User control in c#, but, as the title says, is in C#, how I can do it in VB.NET?
Sorry, I just notice that the Enter key post the comment. :(
Thanks for your feedback, I understand what are you saying but I missing something in the middle.
When I create the control in running time in the above code I can access easily to the properties of the created object, in this case my UserControl, but I don't understand how to reach the properties of a particular instance of that control from outside of Button_Click; ie. another button_click event(second button)
I was thinking to use something like
Dim ControlList As Windows.Forms.Control() = Me.ClientesTabControl.TabPages(0).Controls.Find("ModeloLbl", True)
or
ClientesTabControl.TabPages(0).Controls.OfType(Of AlarmasVehiculo)()
But I'm stuck here.
------------------------------------- 3th post ---------------
Thanks Steve, I was resolved using "Control.Find" and a For Each but your solution is easier.
There's any way to get the name of the selected tab or I must to create an Array when I create the New TabPage?, the idea is to update the text of the controls inside of the selected tab only when is selected by the user or every 5 seconds but just the in selected one.
Thanks.
To borrow M4N's answer from the C# question, and translate it to VB:
Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:
Public Class MyUserControl
' expose the Text of the richtext control (read-only)
Public ReadOnly Property TextOfRichTextBox As String
Get
Return richTextBox.Text
End Get
End Property
' expose the Checked Property of a checkbox (read/write)
Public Property CheckBoxProperty As Boolean
Get
Return checkBox.Checked
End Get
Set (value As Boolean)
checkBox.Checked = value
End Set
End Property
'...
End Class
In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).
Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.
To answer your second question, if you need to access your dynamically created controls, you can do so easily using their names, for instance:
Dim c As ClientesEmpresa= CType(Me.ClientesTabControl.TabPages("Tab1").Controls("Control1"), ClientesEmpresa)
c.CheckBoxProperty = True

VB.Net WebBrowser Navigate Only Working Once

Hoping someone can help me with this. I have two separate but related Forms, one of which contains a WebBrowser control. The user fills out some information on Form 1 and clicks a button with the following code:
If Form2Shown = False Then
Dim memoscreen As New Form2
Form2Ref = memoscreen
memoscreen.show()
Form2Shown = True
memoscreen.TopMost = OptionOnTop
Else
Dim memoscreen As Form2
memoscreen = Form2Ref
memoscreen.TopMost = OptionOnTop
memoscreen.QuickRefresh()
End If
The QuickRefresh sub in Form2 is the method that navigates. It is called both when the form is loaded as well as manually in the code above:
Public Sub QuickRefresh()
Dim HM As Form1
HM = Form1Ref
Me.Text = "retrieving information..."
Me.AxWebBrowser1.Navigate("SomeValidURL")
HM.Focus()
HM.SetHugoFocus()
End Sub
The problem I'm having is that the first time QuickRefresh is called (i.e. when Form2 is loaded) the navigation is successful and the page displays fine. If I then click the button on Form1 again, the page does not change. The Text attribute and window focus does change however, so I know the method is firing.
Some things I've tried/checked:
AllowNavigation is set to True on the WebBrowser control
Have tried looping while the browser is busy while calling Application.DoEvents()
Any suggestions would be appreciated. Thanks.
From your "Internet Options dialog > General Tab > Settings Button > Check for newer version of stored page" change that option to *Every Time I visit the webpage". That setting impacts how the webbrowser control deals with the refreshing.
Use the method refresh.
browser.Navigate("http://www.google.com") : browser.Refresh()

VB.NET Programmatically add code between tags

I have a custom control that does some work for me on async postbacks. Normally, I'd call the control directly from the presentation side using the following code on the ASPX page:
<mytag:CustomControl runat="server">
html (or other text) goes here
</mytag:CustomControl>
However, in my current application, I need to dymanically create the control from the codebehind, using code similar to the following:
Dim myControl As myClass.CustomControl = New myClass.CustomControl
myControl.ID = "someID"
myControl.?????? = "html (or other text) goes here"
Me.Controls.Add(myControl)
When adding the control to the page dynamically, how do I add info that would normally be between the start and end tags if the control were added the normal, non-dynamic way?
Thanks
Here's the actual control:
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim scriptmanagerPage As ScriptManager = ScriptManager.GetCurrent(Page)
If scriptmanagerPage Is Nothing Then
'Do nothing
Else
'See if we are in a postback
If scriptmanagerPage.IsInAsyncPostBack Then
'We are in a postback; register the script
Dim stringbuilderWorking As New StringBuilder()
MyBase.Render(New HtmlTextWriter(New StringWriter(stringbuilderWorking)))
Dim stringScript As String = stringbuilderWorking.ToString()
ScriptManager.RegisterStartupScript(Me, GetType(ScanWorkXAJAX), UniqueID, stringScript, False)
Else
'Not in a postback
MyBase.Render(writer)
End If 'In an async postback
End If 'Scriptmanager present
End Sub
What do you mean by data? More controls?
You can use
myControl.Controls.Add(childControlHere);
EDIT After question was clarified:
Add a literal control. i.e.
myControl.Controls.Add(new LiteralControl("<b>hello world</b><script type='text/javascript'>alert('hi');</script>"));
Do you mean FIND and append data to controls inside your dynamic control?
You can use the WebControl.FindControl method to find your control embedded in your custom control and then you can add data via its properties.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.findcontrol.aspx
That depends on the properties of the custom control you are using. You may have to bind to a template to display so that events will be fired, it really just depends on your specific control.
Was able to accomplish this using a literal control.
Code above plus:
Dim myLiteral As LiteralControl = New LiteralControl
myLiteral.ID = "myLiteral"
myLiteral.Text = "html (or some other text) goes here"
myControl.Controls.Add(myLiteral)