MS Office (Excel) Automation Across HTML Frames

Hi

I know how to automate Excel from an HTML
i.e.

      var oXL = new ActiveXObject("Excel.Application");
      oXL.Visible = true;

what it does, is open an excel workbook and show it outside of the HTML page (just a simple standalone Excel window)

1) what is the code to open that ActiveXObject inside an HTML frame?

Question Explanation:
what I want to do, is open that exact dynamically created  
Excel Object *inside another HTML frame* (let say left frame has a button with the above script , right frame should contain the Excel window)

2) What is the code to control an Existing XLS file that was loaded as a source of an HTML frame

Question Explanation:
I know that if I give the frame's src property the path to the excel object I can see it opened INSIDE the frame.

however, I dont know how to control it from the other frame
( I know to get as far as parent.frames.xlFrame. <- what to put after the frame to get the Excel object?)
eranationAsked:
Who is Participating?
 
FaircoCommented:
Answer to question 1:

First off all, add an ID attribute to the FRAME tag within the FRAMESET, that contains the Excel. E.g.:

  <frame name="xlFrame" id="xlFrame">

(Since you mentioned xlFrame in your example, I'll use this name for that frame.)
Once an ID attribute is issued, you can use the following script line to load/change the contents from the other frame:

 parent.document.all.xlFrame.src = "myExcel.xls";

Here's an example for loading XLS:
The frameset file:
---------------------
<HTML>
<head>
   <title>test</title>
</head>
<frameset cols="20%,*" bordercolor=#154120>
<frame src="test2.html" name="menu" noresize scrollleft=0 scrolling=no>
<frame name="xlFrame" id="xlFrame">
</frameset>
</html>

The test2.html (left frame) file:
------------------------------------
<html>
<body>
<button onclick="loadXLS('test.xls')">Load TEST.XLS</button><br>
<button onclick="loadXLS('test2.xls')">Load TEST2.XLS</button><br>

<script language="JavaScript">

function loadXLS(filename)
{
 parent.document.all.xlFrame.src = filename;
}
</script>
</body>
</html>

Pay attention, that Excel being embedded into Internet Explorer renders both programs very unstable. For example, if you try to load the same file twice (clicking twice on either button), you will get an empty frame. This may be only an issue on my computer, but this is a warning of instability anyway. This isn't as annoying as in the following part.

Answer to question 2:
To get hold on the embedded Excel, you can use automation. To get the embedded Excel object, use this script:

  var xlsobj = new ActiveXObject("Excel.Application");
  xlsobj.Application.Workbooks(parent.document.all.xlFrame.src).Activate;
  xlsobj.Application.Range("A1:C3").Select;  // Some demostration: Select 3x3 cells

The problem is, that Internet Explorer does not wait for the commands to complete, therefore produces numerous errors during execution. This problem applies especially, if you combine the two answers into one script. (First it will work, but things go bad after loading the second Excel file.) You have to make the script wait for at least 500 ms after loading an Excel file, otherwise IE tries to access the non-existing Excel object and generates an error. Here's the combined script that I have made:

test2.html (file for the left frame - should be loaded by the frameset)
-------------------------------------------------------------------------------
<html>
<body>
<button onclick="loadXLS('test.xls')">Load TEST.XLS</button><br>
<button onclick="loadXLS('test2.xls')">Load TEST2.XLS</button><br>
<button onclick="selectCells()">Select 3x3 cells</button>

<script language="JavaScript">

function loadXLS(filename)
{
 parent.document.all.xlFrame.src = filename;
 window.setTimeout("activateWB()", 500);
}

function activateWB()
{
 xlsobj.Application.Workbooks(parent.document.all.xlFrame.src).Activate;
}

var xlsobj = new ActiveXObject("Excel.Application");
function selectCells()
{
 xlsobj.Application.Range("A1:C3").Select;
}

</script>
</body>
</html>

Hope I could help!
0
 
abdullah716Commented:
0
 
eranationAuthor Commented:
Thanks, I'm checking it out...  :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.