XL2001: How to Programmatically Save a Worksheet as HTML (274785)



The information in this article applies to:

  • Microsoft Excel 2001 for Mac

This article was previously published under Q274785

SUMMARY

This article shows you how to save a worksheet in the Hypertext Markup Language (HTML) format programmatically.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
The following example uses the HTMLconvert method of the Internet Assistant Wizard to create a Web page.
  1. Close and save any open workbooks, and then create a new workbook.
  2. Enter the following values on Sheet1:
          A1 : 2
          A2 : 4
          A3 : 6
          A4 : 8
          A5 : 10
          A6 :
          A7 : This
          A8 : is
          A9 : a
          A10: test
    					
  3. Select cells A1:A5, and then click Chart on the Insert menu. In the Chart Wizard - Step 1 of 4 - Chart Type dialog box, click Finish.

    The result is a worksheet that contains two data ranges and one chart.
  4. Start the Visual Basic Editor (press OPTION+F11).
  5. To use the Htmlconvert method of the Internet Assistant Wizard add-in, create a reference to this add-in. To create the reference, follow these steps:
    1. Click References on the Tools Menu.
    2. Click to select the HTML check box, and then click OK.

      If you do not see HTML in the Available References list, continue with step c. Otherwise, go to step 6.
    3. If you do not see HTML in the Available References list, click Browse.
    4. Open the folder that contains the updated Html.xla file. By default, the Internet Assistant Wizard file is installed in the following folder:

      Microsoft Office 2001:Office:Excel Add-ins

    5. Click Microsoft Excel Files in the Show list.
    6. Click Internet Assistant Wizard in the list of files, and then click Open.
  6. Click Module on the Insert menu to insert a Visual Basic module.
  7. Enter the following code into the module sheet:
    Sub CreateWebPage()
    
       Dim ObjToConvert(2) As Variant
       Dim Result As Integer
    
       ' Populate the ObjToConvert array with the ranges and chart
       ' that you want to export.
       Set ObjToConvert(0) = Sheets("sheet1").Range("a1:a5")
       Set ObjToConvert(1) = Sheets("sheet1").ChartObjects("Chart 1")
       Set ObjToConvert(2) = Sheets("sheet1").Range("a7:a10")
    
       ' Create the HTML page.
       Result = htmlconvert(rangeandcharttoconvert:=ObjToConvert, _
                            useexistingfile:=False, _
                            usefrontpageforexistingfile:=False, _
                            addtofrontpageweb:=False, codepage:=1252, _
                            htmlfilepath:="Test.htm", _
                            headerfullpage:="Test Page", _
                            linebeforetablefullpage:=True, _
                            namefullpage:="MyName")
    
       ' If the conversion is successful, the code htmlconvert_success
       ' is returned.
       If Result = htmlconvert_success Then
           MsgBox "Web Page Created Successfully"
       Else
           MsgBox "Error Creating Web Page"
       End If
    End Sub
    					
  8. Run the CreateWebPage macro.
When you run the macro, a message box appears that indicates that the Web page was created successfully. You have two additional files in the current folder: Test.htm and Test0.gif. Test.htm is the HTML file, and Test0.gif is the chart that you converted.

If you run this macro several times, it always overwrites the Test.htm file, and creates a new .gif file each time. If, for example, you run the CreateWebPage macro six times, it creates six .gif files. The Test.htm file points to Test5.gif, which is the latest .gif file.

Modification Type:MajorLast Reviewed:6/17/2005
Keywords:kbhowto KB274785