Posts by Ace.McCloud

    The Javascript code shown below, changes the "class" attribute of an <li> element to "active" .



    Before Javascript execution, the <ul> doesnt contain any <li> :


    Code
    1. <ul class="dropdown">
    2. </ul>


    After Javascript execution, a dropdown list of <li> elements is added to the <ul>, which can be seen below :


    Code
    1. <ul class="hidden">
    2. [COLOR=#006400][B] <li class="active">Graphite India Ltd</li>
    3. <li>Graphite India Ltd (Merged)</li>[/B][/COLOR]
    4. </ul>


    <li> elements are referenced using a IHTMLElementCollection, like so :


    Quote

    <ul class="dropdown">
    <li class="active">Graphite India Ltd</li>
    <li >Graphite India Ltd (Merged)</li>
    </ul>



    The class attribute of <ul> changes from "dropdown" to hidden". So, there must be javascript running in the background.


    Quote

    <ul class="hidden">
    <li class="active">Graphite India Ltd</li>
    <li>Graphite India Ltd (Merged)</li>


    </ul>

    Question : I'm trying to remove rows from a html table, if the 'tr' elements have 'th' elements as children. The code below, removes any such rows based on the aforementioned condition but only for Page 1. How can I keep macro listen whenever the Page changes and trigger the code, if that happens? The macro should stop only when a user closes the webpage.


    Update: Read an interesting article on how to trap events, the summary of which is mentioned below (along with the link) . Will give this a try and see if it works!


    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx


    Applications based on Visual Basic can contain WebBrowser objects, which can contain other objects—such as HTML document objects. WebBrowser objects, however, do not provide a way to trap all of the events that can occur in the objects they can contain. The WebBrowsercontrol, for example, does not provide a way to trap the onclick event of a button element in an HTML document object the control contains.


    Events in HTML document objects are forwarded up the document object's hierarchy until the top of the hierarchy is reached (or until event forwarding is canceled). This forwarding is referred to in Understanding the Event Model as bubbling. But events do not bubble to the documentobject's container. The top of an HTML document object's hierarchy is the document object. The WebBrowser control does not provide a way to trap the onclick event of a button element because the event does not bubble to the WebBrowser control.



    "Event sinks can be used in Visual Basic-based applications to create customized ways of responding to events that are triggered by Microsoft ActiveX objects such as WebBrowser objects. This approach often uses WithEvents variables to handle events triggered by the ActiveX objects. But if the events don't bubble to the host applications, you can't trap the events. If you can't trap the events, you can't write procedures to handle them."

    Usage: I want to add a list of Companies to an Excel spreadsheet. The Companies are obtained from Screener.in. I want to trap LEFT Click (so that when I LEFT Click on a Company, it will be added to my Excel Spreadsheet)


    Note: I've already posted the same question on stackoverflow. Here's the link:


    https://stackoverflow.com/questions/...24324_47968379


    I've written a macro which navigates to a webpage. How can I let the macro wait i.e Idle State until the user enters a 'value' into the search box? I found an example online but I dont know how to modify it as the user has to enter multiple keystrokes. Any insight would be really helpful. Thanks!


    [SIZE=12px]

    Code
    1. #If VBA7 Then 'declare virtual key event listener Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Long) As Integer#Else 'declare virtual key event listener Private Declare Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Long) As Integer#End IfPrivate Const VK_F9 = &H78


    Code
    1. Sub WaitUntilF9Key()Do Until GetAsyncKeyState(VK_F9) DoEventsLoopMsgBox "Hello World"End Sub

    [/SIZE]
    [SIZE=12px]

    Code
    1. Sub GetDataFromMoneyControl() Dim SW As SHDocVw.ShellWindows Dim IE As SHDocVw.InternetExplorer Dim Sh As Worksheet Dim Url As String Dim TUrl As MSHTML.HTMLDocument Url = "http://www.moneycontrol.com/" Set Sh = ActiveWorkbook.ActiveSheet Set SW = New SHDocVw.ShellWindows Set IE = New SHDocVw.InternetExplorerMedium IE.Visible = True IE.Navigate Url ' Wait till the Internet Explorer has finished loading Do While IE.ReadyState <> READYSTATE_COMPLETE Loop ' Stores a reference to the HTML Document in that variable Set TUrl = IE.Document ' Wait for document to load If TUrl.ReadyState = "complete" Then ' [B]Search box where I need to enter data (this is where I need to tell the macro to wait)[/B] TUrl.getElementsByTagName("input")(4).Value End Sub

    [/SIZE]

    Solved!


    [TABLE="border: 1, cellpadding: 1, width: 500"]

    [tr]


    [td]

    String1

    [/td]


    [td]

    String2

    [/td]


    [td]

    LevenshteinValue

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Sun Pharma

    [/td]


    [td]

    18

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Aurobindo Pharma

    [/td]


    [td]

    18

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Lupin

    [/td]


    [td]

    20

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Piramal Enterp

    [/td]


    [td]

    15

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Glenmark Pharma

    [/td]


    [td]

    18

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Dr Reddy's Labs

    [/td]


    [td]

    18

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Cadila Health

    [/td]


    [td]

    17

    [/td]


    [/tr]


    [tr]


    [td]

    Vardhaman Laboratories Ltd

    [/td]


    [td]

    Vardhaman Labs

    [/td]


    [td]

    8

    [/td]


    [/tr]


    [/TABLE]

    I need to a search a webpage using String1 but to use it on another, I need to convert it into String2.


    Note: I could use 'InStr' functon, if one of them is a sub string of the other but here the spellings are different. There's no 'a' in String2. Any insight would be really helpful.
    Also, String2 is not known at the time of manipulation. [TABLE="border: 1, cellpadding: 1, width: 500"]

    [tr]


    [td]

    String1

    [/td]


    [td]

    String2

    [/td]


    [/tr]


    [tr]


    [td]

    [h=1][SIZE=14px]Vardhaman Laboratories Ltd[/SIZE][/h]

    [/td]


    [td]

    [h=1][SIZE=14px]Vardhman Lab[/SIZE][/h]

    [/td]


    [/tr]


    [/TABLE]

    Solution : (^[^.]+)


    Original String String to Match
    --------------------- -----------------------
    Gun.Metals.Labs. -> Gun
    Gun.Metals.Labs -> Gun
    Gun Metals.Labs. -> Gun Metals
    Gun.Metals Labs. -> Gun
    Gun.Metals Labs -> Gun
    Gun Metals.Labs -> Gun Metals
    Gun Metals Labs. -> Gun Metals Labs
    Gun Metals Labs -> Gun Metals Labs
    Gun Metals Labs -> Gun Metals Labs (Here, its a Tab between words, but not a space)
    Gun Metals Labs -> Gun Metals Labs (Space bet'n first two words, Tab bet'n last two)
    Gun Metals Labs. -> Gun Metals Labs (Tab bet'n first two words, space bet'n last two)
    Gun Meta.ls Labs. -> Gun Met
    Gun Metals.La.bs -> Gun Metals

    The error message that I am seeing indicates that you are using Objects that Excel VBA does not recognise.


    Thanks for your response. It seems to work on one webpage. But the code which I posted above, refers to the second webpage. Should I inspect the webpage for any version or something before I can code it again? Btw, I'm using Office 2016.

    Because of the same error that I told you about at the weekend, with a link to the explanation.


    Is it because the 'cells' property is returning a collection? If that were the case, then how about this (It still gives me the same error) :


    Quote

    Set BSMCRowNameCellObj = FIUrl.getElementsByTagName("table").Item(4).getElementsByTagName("tr").Item(d1).getElementsByTagName("td").Item (0)

    [ATTACH=JSON]{"alt":"Click image for larger version Name:\timage_70851.png Views:\t1 Size:\t53.2 KB ID:\t1194862","data-align":"none","data-attachmentid":"1194862","data-size":"full"}[/ATTACH][ATTACH=JSON]{"alt":"Click image for larger version Name:\tError Line.PNG Views:\t1 Size:\t15.6 KB ID:\t1194863","data-align":"none","data-attachmentid":"1194863","data-size":"full"}[/ATTACH]