将 Excel 发布为网页
Publishing Excel as Webpage
我正在尝试使用 VB 脚本将 excel 另存为网页。我面临的问题是 excel 中的 sheet 名称(例如 Sheet1)也保存在网页中。我不希望 sheet 名称出现在网页中。
我找到的一个解决方案是,如果 excel 发布为网页(而不是保存为网页),那么它将不包含 sheet 名称。但是我在尝试使用 VB 脚本时遇到了问题。下面是代码:
Dim objApp, objBook, objRange, objSheet
Set objApp = CreateObject("Excel.Application")
objApp.Visible = False
Set objBook = objApp.Workbooks.Open("C:\Dashboard\excel_files\Account Executive.xlsx")
Set objSheet = objBook.worksheets("Account Executive")
Set objRange = objSheet.Range("C5:I30")
With objBook.PublishObjects.Add(xlSourceRange, "C:\EXCEL_TO_HTML_RANGE.htm", "Account Executive", objRange.Address, xlHtmlStatic, "Title of the page").Publish(True)
End With
objBook.Close
objApp.Quit
Set objSheet = Nothing
Set objRange = Nothing
Set objApp = Nothing
此代码给出了一个未知的 运行 时间错误,代码为 800A03EC。
是否有更正此代码或其他方法从网页中删除 sheet 名称?
提前致谢。 :)
感谢@AxelRichter
excel 2010 的最终工作代码:
Dim objApp, objBook, objRange, objSheet
Set objApp = CreateObject("Excel.Application")
objApp.Visible = False
Set objBook = objApp.Workbooks.Open("C:\Dashboard\excel_files\OU Dashboard.xlsx")
Set objSheet = objBook.worksheets("OU Dashboard")
Set objRange = objSheet.Range("C5:J30")
Dim oPOs, oPO
Set oPOs = objBook.PublishObjects
Set oPO = oPOs.Add(4, "C:\Dashboard\EXCEL_TO_HTML_RANGE.htm", "OU Dashboard", "$C:$J", 0, "TableDIVID", "Title of the page")
oPO.Publish (True)
set oPO = Nothing
set oPOs = Nothing
Set objSheet = Nothing
Set objRange = Nothing
objBook.Close False
set objBook = Nothing
objApp.Quit
Set objApp = Nothing
您的代码存在三个问题。
第一:常量xlSourceRange
和xlHtmlStatic
可能未定义。因此,请改用它们的值。请参阅 https://msdn.microsoft.com/en-us/library/office/ff823068.aspx 了解 XlSourceType 和 XlHTMLType。
第二:With
需要 Add
返回的对象而不是 Publish
的响应。
第三:你把 DivID
和 Title
弄乱了。
Dim objApp, objBook, objRange, objSheet
Set objApp = CreateObject("Excel.Application")
objApp.Visible = False
Set objBook = objApp.Workbooks.Open("C:\Dashboard\excel_files\Account Executive.xlsx")
Set objSheet = objBook.worksheets("Account Executive")
Set objRange = objSheet.Range("C5:I30")
With objBook.PublishObjects.Add(4, "C:\EXCEL_TO_HTML_RANGE.htm", "Account Executive", objRange.Address, 0, "TableDIVID", "Title of the page")
.Publish(True)
End With
objBook.Close False
objApp.Quit
Set objSheet = Nothing
Set objRange = Nothing
Set objApp = Nothing
可以将With .. End With
替换成下面的代码进行调试:
...
Dim oPOs, oPO
Set oPOs = objBook.PublishObjects
Set oPO = oPOs.Add(4, "C:\EXCEL_TO_HTML_RANGE.htm", "Account Executive", "$C:$I", 0, "TableDIVID", "Title of the page")
oPO.Publish (True)
...
我正在尝试使用 VB 脚本将 excel 另存为网页。我面临的问题是 excel 中的 sheet 名称(例如 Sheet1)也保存在网页中。我不希望 sheet 名称出现在网页中。
我找到的一个解决方案是,如果 excel 发布为网页(而不是保存为网页),那么它将不包含 sheet 名称。但是我在尝试使用 VB 脚本时遇到了问题。下面是代码:
Dim objApp, objBook, objRange, objSheet
Set objApp = CreateObject("Excel.Application")
objApp.Visible = False
Set objBook = objApp.Workbooks.Open("C:\Dashboard\excel_files\Account Executive.xlsx")
Set objSheet = objBook.worksheets("Account Executive")
Set objRange = objSheet.Range("C5:I30")
With objBook.PublishObjects.Add(xlSourceRange, "C:\EXCEL_TO_HTML_RANGE.htm", "Account Executive", objRange.Address, xlHtmlStatic, "Title of the page").Publish(True)
End With
objBook.Close
objApp.Quit
Set objSheet = Nothing
Set objRange = Nothing
Set objApp = Nothing
此代码给出了一个未知的 运行 时间错误,代码为 800A03EC。 是否有更正此代码或其他方法从网页中删除 sheet 名称?
提前致谢。 :)
感谢@AxelRichter excel 2010 的最终工作代码:
Dim objApp, objBook, objRange, objSheet
Set objApp = CreateObject("Excel.Application")
objApp.Visible = False
Set objBook = objApp.Workbooks.Open("C:\Dashboard\excel_files\OU Dashboard.xlsx")
Set objSheet = objBook.worksheets("OU Dashboard")
Set objRange = objSheet.Range("C5:J30")
Dim oPOs, oPO
Set oPOs = objBook.PublishObjects
Set oPO = oPOs.Add(4, "C:\Dashboard\EXCEL_TO_HTML_RANGE.htm", "OU Dashboard", "$C:$J", 0, "TableDIVID", "Title of the page")
oPO.Publish (True)
set oPO = Nothing
set oPOs = Nothing
Set objSheet = Nothing
Set objRange = Nothing
objBook.Close False
set objBook = Nothing
objApp.Quit
Set objApp = Nothing
您的代码存在三个问题。
第一:常量xlSourceRange
和xlHtmlStatic
可能未定义。因此,请改用它们的值。请参阅 https://msdn.microsoft.com/en-us/library/office/ff823068.aspx 了解 XlSourceType 和 XlHTMLType。
第二:With
需要 Add
返回的对象而不是 Publish
的响应。
第三:你把 DivID
和 Title
弄乱了。
Dim objApp, objBook, objRange, objSheet
Set objApp = CreateObject("Excel.Application")
objApp.Visible = False
Set objBook = objApp.Workbooks.Open("C:\Dashboard\excel_files\Account Executive.xlsx")
Set objSheet = objBook.worksheets("Account Executive")
Set objRange = objSheet.Range("C5:I30")
With objBook.PublishObjects.Add(4, "C:\EXCEL_TO_HTML_RANGE.htm", "Account Executive", objRange.Address, 0, "TableDIVID", "Title of the page")
.Publish(True)
End With
objBook.Close False
objApp.Quit
Set objSheet = Nothing
Set objRange = Nothing
Set objApp = Nothing
可以将With .. End With
替换成下面的代码进行调试:
...
Dim oPOs, oPO
Set oPOs = objBook.PublishObjects
Set oPO = oPOs.Add(4, "C:\EXCEL_TO_HTML_RANGE.htm", "Account Executive", "$C:$I", 0, "TableDIVID", "Title of the page")
oPO.Publish (True)
...