gpt4 book ai didi

ruby - 当通过 OLE 从 Ruby 或 VBS 调用时,Word Document.SaveAs 忽略编码

转载 作者:数据小太阳 更新时间:2023-10-29 08:13:01 25 4
gpt4 key购买 nike

我有一个脚本,VBS 或 Ruby,将 Word 文档保存为“过滤后的 HTML”,但编码参数被忽略。 HTML 文件始终以 Windows-1252 编码。我在 Windows 7 SP1 上使用 Word 2007 SP3。

ruby 示例:

require 'win32ole'
word = WIN32OLE.new('Word.Application')
word.visible = false
word_document = word.documents.open('C:\whatever.doc')
word_document.saveas({'FileName' => 'C:\whatever.html', 'FileFormat' => 10, 'Encoding' => 65001})
word_document.close()
word.quit

VBS 示例:

Option Explicit
Dim MyWord
Dim MyDoc
Set MyWord = CreateObject("Word.Application")
MyWord.Visible = False
Set MyDoc = MyWord.Documents.Open("C:\whatever.doc")
MyDoc.SaveAs "C:\whatever2.html", 10, , , , , , , , , , 65001
MyDoc.Close
MyWord.Quit
Set MyDoc = Nothing
Set MyWord = Nothing

文档:

文档.另存为:http://msdn.microsoft.com/en-us/library/bb221597.aspx

msoEncoding 值:http://msdn.microsoft.com/en-us/library/office/aa432511(v=office.12).aspx

有什么建议,如何让 Word 以 UTF-8 格式保存 HTML 文件?

最佳答案

嗨,Bo Frederiksen 和 kardeiz,

今天我的“Word 2003 (11.8411.8202) SP3”版本也遇到了“Word Document.SaveAs ignores encoding”的问题。

幸运的是,我设法使 msoEncodingUTF8(即 65001)在 VBA 代码中工作。但是,我必须先更改 Word 文档的设置。步骤是:

1) 从 Word 的“工具”菜单中,选择“选项”。

2) 然后点击“常规”。

3) 按“Web 选项”按钮。

4) 在弹出的“Web 选项”对话框中,单击“编码”。

5) 你可以找到一个组合框,现在你可以更改编码,例如,从 'GB2312' 到 'Unicode (UTF-8)'。

6) 保存更改并尝试重新运行 VBA 代码。

希望我的回答能帮到你。下面是我的代码。

Public Sub convert2html()
With ActiveDocument.WebOptions
.Encoding = msoEncodingUTF8
End With

ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & "file_name.html", FileFormat:=wdFormatFilteredHTML, Encoding:=msoEncodingUTF8

End Sub

关于ruby - 当通过 OLE 从 Ruby 或 VBS 调用时,Word Document.SaveAs 忽略编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com