HOWTO: Automating Excel From Client-Side VBScript

ID: Q198703


The information in this article applies to:


SUMMARY

This article walks you through building a Web page that launches and automates Microsoft Excel 97 when a user clicks a button. To use this article you need to have the following:


MORE INFORMATION

Use the following steps:

  1. Start Microsoft Visual InterDev version 6.0.


  2. In the New Project dialog box, type "MyVIProj" in the Name edit box, then double-click the New Web Project icon.


  3. Type or select your Web server name in the Web Project Wizard dialog box that opens. Leave the working mode as Master, click Next, then click Finish in the following dialog box.


  4. After Visual InterDev finishes creating your project, from the Project menu, choose Add Web Item\HTML Page...


  5. In the Add Item dialog box, type MyPage as the name of your page, and then click Open.


  6. The HTML page you added opens in Design view by default. Click inside the page while in Design view and type in the following text, as if you were typing it into a Microsoft Word document:


  7. 
          Welcome to my web page! This page demonstrates the use of client-side
          script by automating Microsoft Excel 97. Click the button below
          to start the demonstration... 


  8. Click the Source tab at the bottom of your HTML page.


  9. Add the following HTML just before the </BODY> tag...:



  10. 
    
        &lt;INPUT id=button1 name=button1 type=button value=Button&gt;&lt;/P&gt;
        &lt;SCRIPT LANGUAGE="VBScript"&gt; 

    
        sub button1_onclick()
           ' Launch Excel.
           dim app
           set app = createobject("Excel.Application")
    
           ' Make it visible.
           app.Visible = true
    
           ' Add a new workbook.
           dim wb
           set wb = app.workbooks.add
    
           ' Fill array of values first...
           dim arr(19,9) ' Note: VBScript is zero-based
           for i = 1 to 20
              for j = 1 to 10
                 arr(i-1,j-1) = i*j
              next
           next
    
           ' Declare a range object to hold the data.
           dim rng
           set rng = wb.Activesheet.Range("A1").Resize(20,10)
    
           ' Now assign them all in one shot...
           rng.value = arr
    
           ' Add a new chart based on the data.
           wb.Charts.Add
           wb.ActiveChart.ChartType = 70 'xl3dPieExploded
           wb.ActiveChart.SetSourceData rng, 2 ' xlColumns
           wb.ActiveChart.Location 2, "Sheet1" 'xlLocationAsObject
    
           ' Rotate it around...
           for i = 1 to 360 step 30
              wb.activechart.rotation = i
           next
        end sub
        &lt;/SCRIPT&gt; 

  11. From the File menu, choose Save All (or select CTRL+SHIFT+S).


  12. Open Microsoft Internet Explorer (either locally on your server or remotely), and navigate to your page. For example, in http://<myservername>/MyVIProj/MyPage.htm substitute your server's name for <myservername>.


  13. Assuming your Web server is configured properly, you should see your Web page. Now click the button to execute the client-side script. You should see Excel launch, a new workbook get created, data filled into Sheet1, and a chart get created and rotated. You may receive the following error after clicking the button on the page:
    ActiveX component can't create object 'createobject'.
    If you get the error, you need to change the security settings on your client-side browser. The instructions for making the change are in the article listed in the REFERENCES section.


REFERENCES

For additional information, please see the following article(s) in the Microsoft Knowledge Base:

Q195826 PRB: CreateObject Fails from Client-Side Scripts

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Joe Crump, Microsoft Corporation

Additional query words: kbDSupport


Keywords          : kbASP kbExcel kbIE kbVisID 
Version           : WINDOWS:6.0,97; winnt:4.0
Platform          : WINDOWS winnt 
Issue type        : kbhowto 

Last Reviewed: January 16, 1999