将工作簿中的所有工作表保存为HTML文件

时间:2017-03-22 16:36:47

标签: html excel vba excel-vba export

我一直在使用以下脚本将.xlsx文件转换为HTML网页。现在我需要将此工作簿中的所有工作表保存为HTML,此功能不起作用。

Const xlHtml = 44
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open "C:\Workbook\newitems.xlsx"
objExcel.ActiveWorkbook.SaveAs "C:\Workbook\newitems.html", xlHtml
objExcel.Quit

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您需要迭代文件中的每个工作表并为其保存输出文件:

Const xlHtml = 44

Sub SaveAllTabsAsHtmlFiles()

    Set objExcel = CreateObject("Excel.Application")
    ' open that source file
    objExcel.Workbooks.Open "C:\Workbook\newitems.xlsx"
    ' iterate all sheets in source file
    For i = 1 To objExcel.ActiveWorkbook.Worksheets.Count
        ' select each sheet
        objExcel.ActiveWorkbook.Worksheets(i).Select
        ' get name of current sheet
        sheetname = objExcel.ActiveWorkbook.Worksheets(i).Name
        ' name of output file (I added underscore + sheetname)
        outputfile = "C:\Workbook\newitems_" & sheetname & ".html"
        ' save current sheet as html
        objExcel.ActiveWorkbook.SaveAs Filename:=outputfile, FileFormat:=xlHtml
    Next i
    'done
    objExcel.Quit
End Sub