Solved

MS Office (Excel) Automation Across HTML Frames

Posted on 2004-11-01
410 Views
Last Modified: 2012-06-21
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?)
0
Question by:eranation
    3 Comments
     
    LVL 3

    Accepted Solution

    by:
    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
     
    LVL 6

    Expert Comment

    by:abdullah716
    0
     

    Author Comment

    by:eranation
    Thanks, I'm checking it out...  :-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now