【问题标题】:vbscript to convert html files to docxvbscript 将 html 文件转换为 docx
【发布时间】:2018-08-28 05:23:07
【问题描述】:

我在 c:\temp\ 中有许多 html 文件,需要在同一目录中将它们转换为 .docx 文件。
我有 Office 2013。所以我想它不需要任何转换器,而只需要在后台打开 word 中的 html 并将其保存为 .docx。

我找到了一个将 doc 转换为 pdf 的脚本: vbscript to convert word doc to pdf

Const wdExportAllDocument = 0
Const wdExportOptimizeForPrint = 0
Const wdExportDocumentContent = 0
Const wdExportFormatPDF = 17
Const wdExportCreateHeadingBookmarks = 1

if  Wscript.Arguments.Count > 0 Then
    ' Get the running instance of MS Word. If Word is not running, Create it
    On Error Resume Next
    Set objWord = GetObject(, "Word.Application")
    If Err <> 0 Then
        Set objWord = CreateObject("Word.Application")
    End If
    On Error GoTo 0

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.GetFile(WScript.Arguments(0))
    Set objDoc = objWord.Documents.Open(WScript.Arguments(0),,TRUE)

    'Export to PDF using preferred settings
    pdf = objWord.ActiveDocument.ExportAsFixedFormat( _
        WScript.Arguments(1), _
        wdExportFormatPDF, False, wdExportOptimizeForPrint, _
        wdExportAllDocument,,, _
        wdExportDocumentContent, _
        False, True, _
        wdExportCreateHeadingBookmarks _
    )

    'Quit MS Word
    objWord.DisplayAlerts = False
    objWord.Quit(False)
    set objWord = nothing
    set objFSO = nothing
Else
    msgbox("You must select a file to convert")
End If

将 const wdExportFormatPDF 更改为 wdFormatDocumentDefault = 16 会引发错误。

知道如何打开并保存为 c:\temp 目录中的所有文件吗?

【问题讨论】:

  • throws an error - 请始终始终包含错误的详细信息。

标签: html vbscript converter docx


【解决方案1】:

ExportAsFixedFormat 方法将文档保存为 PDFXPS 格式..

阅读更多关于它的信息here

要将您的文件保存为docx,只需使用SaveAs method。 它适用于 Office 2013 及更高版本

objWord.ActiveDocument.SaveAs ("C:\SomeDir\yourFileName.docx")

【讨论】:

  • 对于 oFldr.Files 中的每个 oFile If LCase(oFSO.GetExtensionName(oFile.Name)) = "html" Then Set oDoc = oWord.Documents.Open(oFile.path) oWord.ActiveDocument.SaveAs oFile.path & ".docx", 12 oDoc.Close End If
  • 现在保存为:filename.html.docx。如何从文件名中删除 .html
  • @Lucas 因为您将文件的完整路径 (oFile.path) 连接到 ".docx" 后缀。只需在连接之前操纵路径。在连接之前从路径中删除最后 5 个字符(".html")的简单示例:pathWithoutHtml = Left(oFile.path, Len(oFile.path) - 5)。然后连接pathWithoutHtml &amp; ".docx"
猜你喜欢
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2020-04-18
  • 2020-06-07
  • 2012-08-05
  • 1970-01-01
  • 2014-10-08
  • 2021-04-23
相关资源
最近更新 更多