使用 Word-VBA 在保存文档时自动另存为 .html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24871616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:56:08  来源:igfitidea点击:

Using Word-VBA to automatically save as .html when you save a document

vbams-wordword-vbamsdn

提问by Brandon

I work in an entirely offlinefacility; it has an intranet site, and is VERY limited to programs available (MS Word is the onlyapplication available).

我在一个完全离线的设施中工作;它有一个内部网站,并且非常受限于可用的程序(MS Word 是唯一可用的应用程序)。

I need to be able to open a Microsoft Word document, edit it, and then, when you save the document, it saves the file, and then saves it in .html format as well.

我需要能够打开 Microsoft Word 文档,对其进行编辑,然后在您保存文档时,它会保存文件,然后也将其保存为 .html 格式。

To elaborate, I work in a military Facility that has a network that we cannot have anything installed. On this network is a series of computers. On these computers we keep a log. We want to be able to display this log to anyone who wants to see it. The log is in a .doc format. I want someone to open the log.doc then save it. Once saved it will save an .html file. That file will be displayed for ANYONE on the network to see it.

详细地说,我在一个军事设施工作,该设施的网络无法安装任何东西。在这个网络上有一系列计算机。在这些计算机上,我们保留日志。我们希望能够向任何想要查看它的人显示此日志。日志采用 .doc 格式。我希望有人打开 log.doc 然后保存它。保存后,它将保存一个 .html 文件。该文件将显示给网络上的任何人查看。

回答by Steve Rindsberg

This is a simplified version of what Word 2010's macro recorder produces when you save a file as MHT (single-file html).

这是将文件另存为 MHT(单文件 html)时 Word 2010 的宏记录器生成的简化版本。

ActiveDocument.SaveAs2 FileName:="c:\temp\test.mht", FileFormat:= _
    wdFormatWebArchive

Adapt as needed.

根据需要进行调整。

I'm not sure if Word 2013 supports saving as HTML any longer. Not all of the office apps do.

我不确定 Word 2013 是否不再支持另存为 HTML。并非所有的办公应用程序都可以。

回答by TylerH

The following is adapted slightly from Microsoft's Developer Network. Save this as a Macro in the copy of MS Word used by the client. This Macro will:

以下内容稍微改编自微软的开发者网络。将此保存为客户端使用的 MS Word 副本中的宏。这个宏将:

Save the document as a .doc(x) file, then as a .HTML file.

将文档另存为 .doc(x) 文件,然后另存为 .HTML 文件。

Sub AutoOpen()
    Saver
End Sub

Sub Saver()
    ActiveDocument.Save
        Document_Save
End Sub

Sub Document_Save()
    Dim strDocName As String
    Dim intPos As Integer

    'Find position of extension in filename
    strDocName = ActiveDocument.Name
    intPos = InStrRev(strDocName, ".")

    If intPos = 0 Then

        'If the document has not yet been saved
        'Ask the user to provide a filename
        strDocName = InputBox("Please enter the name " & _
        "of your document.")
    Else

        'Strip off extension and add ".html" extension
        strDocName = Left(strDocName, intPos - 1)
        strDocName = strDocName & ".html"

    End If

    'Save file with new extension
    ActiveDocument.SaveAs FileName:=strDocName, _
    FileFormat:=wdFormatHTML

End Sub