codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Running a Javascript from VBA.  AhrenL at 16:59 on Tuesday, May 17, 2005
 

Hello,
I am trying to run a search on a vendor webpage in order to extract some surrveillance data. I can get the drop down box, and data fields setup correctly, but I can't get the actually Search function to initiate. Here's the relevant parts of the HTML (I think), and the VB code I'm trying to use.

VB
oIE.Document.frmMain.drdSearchBy.Value = 5
oIE.Document.frmMain.submit
Application.Wait (Now + TimeValue("00:00:05"))
oIE.Document.frmMain.txtSearchCriteria5Lines.Value = "14041NAC"
oIE.Document.frmMain.btnSearchMain.click <--This is the line that is supposed to click the Search button. The object is not recognized.

Html code:
</td>
<td width="35%" valign="top" align="left" id="btnSearchMain" > <img src="/gfx/pds_smarrow.gif" border="0" align="bottom"> <a href="javascript:checkQuasiOptions(false);javascript:doSearch();"><b>Search</b></a></td>
</tr> (<-- this is the button I want to click)

function doSearch()
{

if (bOkToSearch)
{
var errMsg = "";
var iTotalSelected = 0;

/* set validation message */
switch (document.frmMain.drdSearchBy.value)
{
/* THIS WAS ADDED FOR V2 */
case "5": case "6": case "8": /* ISIN AND CUSIP and DEAL #s!!!! */
if (document.frmMain.txtSearchCriteria5Lines.value == "") {errMsg = "Enter a search criteria";}
break;
default:
break;
} (<-- This is the abbv. function that will run)

Anyone have any idea?
Thanks!


  Re: Running a Javascript from VBA.  AhrenL at 13:59 on Wednesday, May 18, 2005
 

In essence. This is what I need to do. :( Not sure if its possible in VB. I managed to recreate all the actions of the VBscripts, but I have no idea with this one (since I've never even used Javascript before).

var oList9 = document.getElementById('sl1');
oList9.options[oList9.length] = new Option(sDPTName, aValue(i).replace(/\,/g,','), false, false);


sDPTName is a formatted chunck of aValue(i) which is just the website security identifier. I can replicated all the formatting used in these two variables, but I'm not sure how to basically add an option to oList9.

Here's what I unsuccesfully tried.

oIE.Document.frmMain.sli.Options(0).Value = oIE.Document.all.chkbox.Value oIE.Document.frmMain.sli.Options(0).Selected = True
oIE.Document.frmMain.subAction.Value = "CreateObject"
oIE.Document.frmMain.submit


Here's the final doCreate function that needs to run in order for the data fields I need to be available.

/* validation passed, execute create */
/* save selected deal-pool-tranches
use ~ as delimiter to split, so we only keep dpt id's */
setSelectedDPTAsString("~", ",", document.frmMain);

if (document.frmMain.ObjectType.value == 'LookthroughReport')
{
for (i=0; i<document.frmMain.sl1.length; i++) {document.frmMain.sl1.options(i).selected = true;}
}

/* pass list of selected deal-pool-tranches as comma delimited string */
document.frmMain.subAction.value = "createObject";
setScrollTop();

document.frmMain.submit();



<Added>


I was able to figure out the doSearch replication. So it's really just this last step, and I'm all set.

  Re: Running a Javascript from VBA.  AhrenL at 14:30 on Tuesday, May 24, 2005
 

I was able to figure this out as well. If anyone is interested, let me know.


  Re: Running a Javascript from VBA.  cdahrens at 21:28 on Tuesday, August 02, 2005
 

AhrenL,

This topic sounds very close to something that I have been trying for the past few weeks. I think that I am just behind your first post. Mabye you could back up a little for me.

I, like you, am trying to retreive some data from a website that uses javascript to submit search critera. And I would like to pull the results out of the html doc that is returned.


  Re: Running a Javascript from VBA.  AhrenL at 13:47 on Wednesday, August 03, 2005
 

Sure, what do you need me to explain? I've come a long way in the few months since I wrote this post, and seem to have no problem retreiving data from any website that I've wanted. Although I never finished this particular vendor extract because the website is just too slow..

  Re: Running a Javascript from VBA.  wilsonshaw at 14:07 on Monday, March 20, 2006
 

Actually I have find a Macro online that exactly do the job... Although it focuses on automating the login for a website.
However, I have other difficulties running this macro.
First, let me show you the script:


Option Explicit

Dim myIE As InternetExplorer
Dim myIEdoc As HTMLDocument
Dim theForm As HTMLFormElement

Sub OpenWebpageAndLogin(URL As String)
Dim theItm As HTMLFormElement
Dim i As Integer
Dim flg As Integer
Set myIE = New InternetExplorer
With myIE
.Navigate URL
.Visible = True
Do While .Busy: DoEvents: Loop
Do While .ReadyState <> 4: DoEvents: Loop
End With
Set myIEdoc = myIE.Document
Set theForm = findFm(myIEdoc)
With theForm
For i = 0 To .Length - 1
Select Case .Item(i).Type
Case "password"
.Item(i).Value = "YourPW"
Case "text"
.Item(i).Value = "YourLogin"
Case "submit"
flg = i
Case Else
End Select
Next
End With
If flg > 0 Then
theForm.Item(flg).Click
Else
myIE.Quit
MsgBox ("Unexpected Error, I'm quitting.")
End If
Set myIE = Nothing
End Sub

Function findFm(theDoc As HTMLDocument) As HTMLFormElement
Dim i As Integer, j As Integer
Dim theItm As HTMLFormElement
With theDoc.forms
For i = 0 To .Length - 1
Set theItm = .Item(i)
With theItm
For j = 0 To .Length - 1
If .Item(j).Type = "password" Then
Set findFm = theItm
Exit Function
End If
Next
End With
Next
End With
End Function


Sorry if it is a bit too long...

My problems are
1. In my targeted website, there is two (or more) HTMLDocument within a single webpage, and the findFm function simply cannot find the targeted forms. Is there a way to select the targeted document?? Like HTMLDocumentCollection, which I can make a For Next Statement to search through all the HTMLDocument. Or it is doomed.

2. Also, even I try working around the first problem by creating a similar webpage in my local drive, I can find the "login" and "password" field but I still cannot find the "Login" button since it is not with a "Form". I have also try using like GetbyTagnames....
but all failed. Is there another way????

Anyway, hope this macro is helpful for you guys. Thank you.

  Re: Running a Javascript from VBA.  AhrenL at 14:23 on Monday, March 20, 2006
 

Here are some things that I've been using.

For buttons, I will assign an object to all buttons in the document with the :

.getelementsbytagname("A") 'or whatever the tagname used for buttons is on your particular form

Then it's just about finding the correct button, and using a .onclick.

If you're unable to get it by tagname, as you insinuated, you can also try finding the ID and using .getelementbyID('element ID'). You can find the ID by entering debug mode after linking your internet explorer object, and then stepping through the form structure until you find it. Very tedious, but it has helped me a couple of times in a pinch.

I have one finicky vendor, who accepts input with an ENTER keypress instead of a button.

For that I use:

oIE.Document.webtraderfrm.'your textbox form'.onfocus
Call AppActivate(oIE.Application)
SendKeys ("~")

This is kind of messy because if you're trying to do something else while the code is running, it will rip focus away from you; and if you try to take it back to soon, then you've got additional issues. :0




  Re: Running a Javascript from VBA.  wilsonshaw at 14:37 on Monday, March 20, 2006
 

Thank you so much first.

I would go try the getelementbyID though I think I have tried but failed... Since as I have said, the Function simply cannot find the target document. And thus It returns nothing. (No matter what target string or target type or method... I assign for it to search...)

I have also tried the KeySends method, although it is a bit messy (With all the {Tab} {Enter} you need to input to fill out the "Text" and to click the button). Still there is a problem since the macro is scheduled to run in off-office hour. And the "Sendkey" simply do not work when the user has "locked" the computer. The only thing I can run is to call up the IE window.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: open excel file in html page
•  asp.net excel
•  Cracked software ftp download 2013
•  Latest crack software ftp download
•  Famous Software ftp download 2013
•  deleting data from databse to dropdown list
•  matrix addition
•  Re: Storing data from HTML to Excel or TXT
•  Re: function within loop problem


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2013