Link to home
Start Free TrialLog in
Avatar of remerick
remerick

asked on

Using execScript to execute JavaScript in Web Browser

This question has come up before as recently as April 2004 and I have followed the thread in the title solution "JavaScript manipulation with VBA (Excel)" as an example.  Unfortunately I am getting the same automation error that was showing up in the original posting and I can't seem to get around this.  I don't know if it is related to scope of objects or the window I am trying to execute from, but here is a segment of the HTML/Script that I am trying to interact with:

<script language='JavaScript'>
var  totalTimeoutMilliseconds = 2400000;
var  warningTimeoutMilliseconds = 2160000;
var timeOutURL = 'http://xxx.xxx.xxx.com/psp/ps/EMPLOYEE/PSFT_EP/?cmd=expire';
var timeoutWarningPageURL = 'http://xxx.xxx.xxx.com/psc/ps/EMPLOYEE/PSFT_EP/s/WEBLIB_TIMEOUT.PT_TIMEOUT-WARNING.FieldFormula.IScript_TIMEOUTWARNING';
</script>

<link rel='stylesheet' type='text/css' href='/cs/ps/cache/PTSTYLEDEF_DARKBLUE_1.css' />
<title>ETC Analysis Start</title>
<script language='JavaScript'>
var baseKey_win0 = "";
var altKey_win0 = "05678\xbc\xbe\xbf\xde";
var ctrlKey_win0 = "JK";
var bTabOverTB_win0 = false;
var bTabOverPg_win0 = false;
var bTabOverNonPS_win0 = false;
</script>
<script language='javascript' src='/cs/ps/cache/PT_SCRIPTIE550_win0_1.js'>
</script>
<SCRIPT LANGUAGE='JavaScript'>
document.domain = "xxx.xxx.xxx.com";
</SCRIPT>
<script language='javascript' src='/cs/ps/cache/PT_PAGESCRIPT_win0_1.js'>
</script>
<script language='javascript' src='/cs/ps/cache/PT_ISCROSSDOMAIN_win0_1.js'>
</script>
<script language='javascript' src='/cs/ps/cache/PT_SAVEWARNINGSCRIPT_win0_1.js'>
</script>
<script language='JavaScript'>
var nResubmit=0;
setupTimeout();
function submitAction_win0(form, name)
{
form.ICAction.value=name;
form.ICXPos.value=getScrollX();
form.ICYPos.value=getScrollY();
processing_win0(1,3000);
form.ICResubmit.value=nResubmit;
form.submit();
nResubmit++;
}
</script>

********************************************
And here is my VB code:

Dim IE As Object
Dim Wait As Integer
Dim test As String

Set IE = CreateObject("InternetExplorer.application")

IE.Navigate2 "http://xxx.xxx.xxx.com/psp/ps/EMPLOYEE/PSFT_EP/c/CREATE_PROJECTS.PC_XX_ETC_START.GBL"

While IE.busy
    DoEvents
Wend

Call IE.Document.parentWindow.execScript("submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');", "JavaScript")

**************************************************

When it gets to the execScript call, it generates an automation error # 80020101.

I also tried calling the execScript from the frame level (not sure if there even is a frame level.  When try to identify the Document and parentWindow, I get back:

HTMLDocument
and
HTMLWindow2

prior to making the execScript call.  It looks like they are referencing form win0 in the HTML, but I am not sure if that is affecting what I am trying to do.

Any help will be greatly appreciated!  I am assigning 500 points due to urgency.

Bob

-
Avatar of AzraSound
AzraSound
Flag of United States of America image

>>I also tried calling the execScript from the frame level

Are you sure its from the correct frame?


Just to be sure, what if you move the code to call the script to a button or something, and click it when you know the document has fully loaded??
Avatar of remerick
remerick

ASKER

>>Are you sure its from the correct frame?

Thanks for the feedback AzraSound.  I am not certain that I am on the correct frame and not sure how to identify this.  Any ideas on how to validate and specify where I am at the that time would be helpful.

>>Just to be sure, what if you move the code to call the script to a button or something, and click it when you know the document has fully loaded??

I am instantiating IE as part of the process.  If you can tell me how to hook into an instance of an existing IE with the page already loaded, I can try out your suggestion.

Thanks,
Bob

>>Any ideas on how to validate and specify where I am at the that time would be helpful

We would need to see the html page source.  If there are frames at all, would be the first question.


>>If you can tell me how to hook into an instance of an existing IE with the page already loaded, I can try out your suggestion.

What I meant was, instead of what you have above, do something like:


Dim IE As Object    <~~ DECLARE AT TOP OF MODULE

Set IE = CreateObject("InternetExplorer.application")

IE.Navigate2 "http://xxx.xxx.xxx.com/psp/ps/EMPLOYEE/PSFT_EP/c/CREATE_PROJECTS.PC_XX_ETC_START.GBL"



Wait a minute...go to the restroom or grab some coffee (you get the idea) just to make sure the page has fully loaded.  Then have a button on a form that says:

Private Sub Command1_Click()
    Call IE.Document.parentWindow.execScript("submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');", "JavaScript")
End Sub


BTW, do you ever make the browser visible, or is this all done with it hidden?
I moved the code around so that the execScript fired after the page was loaded.  Still get the same error.  
Yes, I do make the browser visible also.

Thanks again!

Here is the entire source from the page:

<html lang='en'>
<!-- Copyright (c) 2000-2003 PeopleSoft, Inc.  All Rights Reserved. -->
<!-- IE/5.5/WINNT; ToolsRel=8.43.10; Page=PC_XX_ETC_START; Component=PC_XX_ETC_START; Menu=CREATE_PROJECTS; User=111111; DB=PFTST/ORACLE; AppServ=//10.10.10.10:9000; -->
<head>
<script language='JavaScript'>
var  totalTimeoutMilliseconds = 2400000;
var  warningTimeoutMilliseconds = 2160000;
var timeOutURL = 'http://xxx.corp.xxx.com/psp/ps/EMPLOYEE/PSFT_EP/?cmd=expire';
var timeoutWarningPageURL = 'http://xxx.corp.xxx.com/psc/ps/EMPLOYEE/PSFT_EP/s/WEBLIB_TIMEOUT.PT_TIMEOUT-WARNING.FieldFormula.IScript_TIMEOUTWARNING';
</script>

<link rel='stylesheet' type='text/css' href='/cs/ps/cache/PTSTYLEDEF_DARKBLUE_1.css' />
<title>ETC Analysis Start</title>
<script language='JavaScript'>
var baseKey_win0 = "";
var altKey_win0 = "05678\xbc\xbe\xbf\xde";
var ctrlKey_win0 = "JK";
var bTabOverTB_win0 = false;
var bTabOverPg_win0 = false;
var bTabOverNonPS_win0 = false;
</script>
<script language='javascript' src='/cs/ps/cache/PT_SCRIPTIE550_win0_1.js'>
</script>
<SCRIPT LANGUAGE='JavaScript'>
document.domain = "corp.xxx.com";
</SCRIPT>
<script language='javascript' src='/cs/ps/cache/PT_PAGESCRIPT_win0_1.js'>
</script>
<script language='javascript' src='/cs/ps/cache/PT_ISCROSSDOMAIN_win0_1.js'>
</script>
<script language='javascript' src='/cs/ps/cache/PT_SAVEWARNINGSCRIPT_win0_1.js'>
</script>
<script language='JavaScript'>
var nResubmit=0;
setupTimeout();
function submitAction_win0(form, name)
{
form.ICAction.value=name;
form.ICXPos.value=getScrollX();
form.ICYPos.value=getScrollY();
processing_win0(1,3000);
form.ICResubmit.value=nResubmit;
form.submit();
nResubmit++;
}
</script>
<script language='javascript' src='/cs/ps/cache/PT_EDITSCRIPT_win0_1.js'>
</script>
</head>
<body class='PSPAGE'  onload="
setFocus_win0('ETC_START_WRK_PB_ADD',-1);
setEventHandlers_win0('ICFirstAnchor_win0', 'ICLastAnchor_win0', false);
processing_win0(0,3000);
setKeyEventHandler_win0();
nResubmit=0;
">
<div id="WAIT_win0" style="position:absolute;right:0;display:block;">
<img align='right' src='/cs/ps/cache/PT_PROCESSING_1.gif' class='PSPROCESSING' alt='Processing... please wait' />
</div>
<a name='ICFirstAnchor_win0'></a>
<form name='win0' method='post' action="../../../EMPLOYEE/PSFT_EP/c/CREATE_PROJECTS.PC_XX_ETC_START.GBL"  autocomplete='off'>
<input type='hidden' name='ICType' value='Panel' />
<input type='hidden' name='ICElementNum' value='0' />
<input type='hidden' name='ICStateNum' value='1' />
<input type='hidden' name='ICAction' value='None' />
<input type='hidden' name='ICXPos' value='0' />
<input type='hidden' name='ICYPos' value='0' />
<input type='hidden' name='ICFocus' value='' />
<input type='hidden' name='ICChanged' value='-1' />
<input type='hidden' name='ICResubmit' value='0' />
<table cols='3' width='100%' cellpadding='0' cellspacing='0' hspace='0' vspace='0'>
<tr>
<td width='80%'></td><td width='10%' nowrap='nowrap' align='right'></td>
<td width='10%' nowrap='nowrap' align='right'><a href="javascript:void window.open('http://xxx.corp.xxx.com/psp/ps_newwin/EMPLOYEE/PSFT_EP/c/CREATE_PROJECTS.PC_XX_ETC_START.GBL');" accesskey='9' tabindex='1' class='PSHYPERLINK'>New Window</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://xxx.corp.xxx.com/PSOL/htmldoc/f1search.htm?ContextID=PC_XX_ETC_START&LangCD=ENG" target='help' tabindex='2' class='PSHYPERLINK'>Help</a></td></tr>
</table>
<br />
<table border='0' cellpadding='0' cellspacing='0'>
<tr>
<td><img src='/cs/ps/cache/PT_TAB1LAX_1.gif' alt='' /></td>
<td><img src='/cs/ps/cache/PT_TAB1MAX_1.gif' width='100%' height='1' alt='' /></td>
<td><img src='/cs/ps/cache/PT_TAB1RAX_1.gif' alt='' /></td>
<td rowspan='2'></td>
</tr>
<tr>
<td><img src='/cs/ps/cache/PT_TAB2LAX4A598C_1.gif' alt='' /></td>
<td nowrap='nowrap' align='center' class='PSACTIVETAB'>&nbsp;&nbsp;ETC Analysis&nbsp;&nbsp;</td>
<td><img src='/cs/ps/cache/PT_TAB2RAX4A598C_1.gif' alt='' /></td>
</tr>
<tr>
<td><img src='/cs/ps/cache/PT_TAB3LAX4A598C_1.gif' alt='' /></td>
<td class='PSACTIVETAB'></td>
<td><img src='/cs/ps/cache/PT_TAB3RAX4A598C_1.gif' alt='' /></td>
<td><img src='/cs/ps/cache/PT_TB_BLN_1.gif' height='2' width='427' alt='' /></td>
</tr>
</table>
<table border='0' cellpadding='0' cellspacing='0' cols='7' width='569'>
<tr>
<td width='8' height='8'></td>
<td width='4'></td>
<td width='96'></td>
<td width='36'></td>
<td width='96'></td>
<td width='116'></td>
<td width='213'></td>
</tr>
<tr>
<td height='67'></td>
<td colspan='5'  valign='top' align='LEFT'>
<span class='PSTEXT' >ETC Analysis</span>
</td>
</tr>
<tr>
<td height='54' colspan='2'></td>
<td colspan='2' nowrap='nowrap'  valign='top' align='LEFT'>
<a name='ETC_START_WRK_PB_ADD' id='ETC_START_WRK_PB_ADD' tabindex='14' href="javascript:submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');"><img src='/cs/ps/cache/PS_ADD_DOCUMENT_ICN_1.gif' name='ETC_START_WRK_PB_ADD$IMG' alt='Add' border='0' /></a>
</td>
<td colspan='3' nowrap='nowrap'  valign='top' align='LEFT'>
<a name='ETC_START_WRK_PB_VIEW' id='ETC_START_WRK_PB_VIEW' tabindex='16' href="javascript:submitAction_win0(document.win0,'ETC_START_WRK_PB_VIEW');"><img src='/cs/ps/cache/PS_REVIEW_DOCUMENT_ICN_1.gif' name='ETC_START_WRK_PB_VIEW$IMG' alt='View' border='0' /></a>
</td>
</tr>
<tr>
<td height='19' colspan='2'></td>
<td colspan='2' nowrap='nowrap'  valign='top' align='LEFT'>
<span class='PSHYPERLINK' >
<a name='ETC_START_WRK_PB_ADD$2$' id='ETC_START_WRK_PB_ADD$2$' tabindex='15' href="javascript:submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD$2$');" class='PSHYPERLINK'  title="Add" >Create/Update</a></span>
</td>
<td colspan='3' nowrap='nowrap'  valign='top' align='LEFT'>
<span class='PSHYPERLINK' >
<a name='ETC_START_WRK_PB_VIEW$5$' id='ETC_START_WRK_PB_VIEW$5$' tabindex='17' href="javascript:submitAction_win0(document.win0,'ETC_START_WRK_PB_VIEW$5$');" class='PSHYPERLINK' >View</a></span>
</td>
</tr>
<tr>
<td height='182' colspan='2'></td>
<td  valign='top' align='LEFT'>
<span class='PSTEXT' >Create/Update ETC Analysis data for further review or to submit.</span>
</td>
<td></td>
<td  valign='top' align='LEFT'>
<span class='PSTEXT' >View previously submitted ETC Analysis sheets</span>
</td>
</tr>
</table>
</form>
<a name='ICLastAnchor_win0'></a>
</body>
</html>
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MsgBox IE.Document.frames.length

returns a count of 3.  

I used :

Call IE.Document.Frames(2).execScript("submitAction_win0(win0,'ETC_START_WRK_PB_VIEW');", "JavaScript")

and it is working now.  Thanks AzraSound!  I will accept your answer.

Bob
Aha!  So, this page source you posted is actually within one of these frames.  Which one, we don't know, so give each a try:

Call IE.Document.frames(0).execScript("submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');", "JavaScript")
Call IE.Document.frames(1).execScript("submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');", "JavaScript")
Call IE.Document.frames(2).execScript("submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');", "JavaScript")
I dont know if i am late. In any case let me post my findings
The reason is the document you are trying to download in still under process while your VB code passes the code.
modify your code to
"
Dim HTMLDoc as HTMLDocument
Set HTMLDOC=IE.Document
Loop Until HTMLDoc.readyState = "complete"

Call IE.Document.parentWindow.execScript("submitAction_win0(document.win0,'ETC_START_WRK_PB_ADD');", "JavaScript")

"

Hope ot will help you