我正在尝试将工作表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
答案 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