将特定范围导出为HTML

时间:2015-05-31 21:34:28

标签: excel vba excel-vba

我正在尝试将工作表Sheet1中的范围(“A1:C10”)导出为 HTML ,并将其保存在与当前相同的目录中工作簿。我还想保留源格式。

我使用以下代码,但是我收到错误,我无法将目标文件夹定义为当前工作簿所在的文件夹。

Sub Export()
Dim rng As Range

Set rng = Sheets("Sheet1").Range("A1:C10")
ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=file1, _
sheet:="Sheet1", _
Source:=rng, _
HtmlType:=xlHtmlStatic).Publish

End Sub

我也试过这个:

Sub Export()
Dim rng As Range


Set rng = Sheets("sheet1").Range("A1:C10")
ActiveWorkbook.PublishObjects.Add( _
SourceType:=xlSourceRange, _
fileName:="C:\exported.xlsx", _
Sheet:="Sheet1", _
Source:=rng, _
HtmlType:=xlHtmlStatic).Publish

End Sub

1 个答案:

答案 0 :(得分:2)

Source:=期望字符串值的地址为范围,而不是Range对象。

包含宏的工作簿的路径可以通过ThisWorkbook.Path获得。

Sub Export()
 Dim rng As Range

 file1 = ThisWorkbook.Path & "\" & "test.html"

 Set rng = Sheets("Tabelle1").Range("A1:C10")

 ActiveWorkbook.PublishObjects.Add( _
 SourceType:=xlSourceRange, _
 Filename:=file1, _
 Sheet:=rng.Worksheet.Name, _
 Source:=rng.Address, _
 HtmlType:=xlHtmlStatic).Publish

End Sub