word文档批量转换为html格式

我不是女神ヾ 2023-10-07 10:22 78阅读 0赞

有时需要将doc/docx格式的文档批量转换为html格式的网页文件,可以使用以下VBA脚本执行批量转换的操作,需要在安装了宏功能的Word中执行。

以下脚本会搜索指定目录中的doc文档,并逐个进行处理,执行以下操作:

  • 将doc文档的标题属性修改为文件名,以使生成的网页文件显示的标题为文件名;
  • 对图片大小进行统一按比例缩放,防止图片过大影响展示;
  • 将生成的html文件保存在指定的目录中。

    Private Declare Function GetTickCount Lib “kernel32” () As Long

    ‘’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’
    Private num_file As Integer
    Private FileType_htm As String
    Private FileType_doc As String
    Private path_src_start As String ‘记录源路径
    Private path_des_start As String ‘记录目标路径
    Private LogFile As String ‘日志文件路径

    Private Sub CommandButton1_Click()

    1. '从指定目录开始处理,下面的路径最后不带“\”
    2. path_src_start = "D:\word" '保存.doc文件的目录
    3. path_des_start = path_src_start '保存需要生成.htm文件的目录
    4. '开始执行
    5. start

    End Sub

    Sub start()

    1. num_file = 0
    2. FileType_htm = ".htm"
    3. FileType_doc = ".doc"
    4. Dim time_start, time_end, second_spend, minute_spend, minute_tail As Long
    5. MsgBox ("需要处理的目录路径:" & path_src_start & vbCrLf _
    6. & "保存处理结果的目录路径:" & path_des_start) 'vbCrLf为回车换行
    7. '记录开始时间
    8. time_start = GetTickCount()
    9. '开始搜索
    10. LogFile = Chr(34) & path_des_start & "\转换记录.txt" & Chr(34) 'chr(34)为",echo >>时,若文件路径中存在空格,需要用""包含
    11. Open path_des_start & "\转换记录.txt" For Output As #1
    12. search path_src_start, path_des_start
    13. '完成搜索
    14. time_end = GetTickCount()
    15. writeLog ("处理文件数量:" & num_file)
    16. '获得花费时间
    17. time_spend = time_end - time_start
    18. second_spend = time_spend \ 1000
    19. minute_spend = second_spend \ 60
    20. minute_tail = second_spend - minute_spend * 60
    21. output = "处理用时:" & minute_spend & "" & minute_tail & ""
    22. writeLog (output)
    23. Close #1
    24. MsgBox ("处理完毕" & vbCrLf _
    25. & "处理文件数量:" & num_file & vbCrLf _
    26. & output)

    End Sub

    ‘搜索文件,path_src为源文件路径,path_des为需要保存.doc文件的路径
    Sub search(path_src, path_des)

    1. Dim fs, fold, fls, fl
    2. Set fs = CreateObject("Scripting.FileSystemObject")
    3. Set fold = fs.getfolder(path_src)
    4. '首先对当前目录的指定文件进行处理
    5. If fold.Files.Count <> 0 Then
    6. Set fls = fold.Files
    7. For Each fl In fls
    8. '判断是否为.htm文件,需判断文件名中是否出现了指定的后缀,且处于最后(防止.html文件匹配.htm
    9. judge = InStrRev(fl.Name, FileType_doc)
    10. If judge <> 0 And judge + Len(FileType_doc) - 1 = Len(fl.Name) Then
    11. '处理.doc文件
    12. operDocFile path_src, fl.Name, path_src
    13. End If
    14. Next
    15. End If
    16. '再对子目录进行递归
    17. If fold.SubFolders.Count <> 0 Then
    18. Set fls = fold.SubFolders
    19. For Each fl In fls
    20. search path_src & "\" & fl.Name, path_des & "\" & fl.Name
    21. Next
    22. End If

    End Sub

    ‘处理.doc文件,path_src为.doc文件所在目录路径,FileName为.doc文件名,path_des为.htm文件需要保存的目录路径
    Sub operDocFile(path_src, FileName, path_des)

    1. filePath_doc = path_src & "\" & FileName
    2. FileName_Only = Left(FileName, InStrRev(FileName, ".") - 1) '去掉后缀的文件名,开头没有'\'
    3. filePath_htm = path_des & "\" & FileName_Only & FileType_htm '对应.htm文件的路径
    4. '判断对应的.htm是否已生成
    5. If Dir(filePath_htm) = "" Then
    6. writeLog ("--开始处理文件:" & filePath_doc)
    7. Documents.Open (filePath_doc) '打开答案.mht文件
    8. modifyTitle (FileName_Only) '修改.doc文档的标题属性为文件名
    9. operPics '修改图片大小
    10. saveDoc2Htm (filePath_htm) '保存为.htm文件
    11. ActiveDocument.Close '关闭打开的文件
    12. writeLog ("--处理文件成功:" & filePath_htm)
    13. num_file = num_file + 1 '已处理文件数加1
    14. End If

    End Sub

    ‘将.doc文件通过word保存为.htm文件,destPath为.htm文件完整路径
    Sub saveDoc2Htm(destPath)

    1. '另存为.htm文件
    2. ActiveDocument.SaveAs FileName:=destPath, FileFormat:= _
    3. wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles:= _
    4. False, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:= _
    5. False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
    6. SaveAsAOCELetter:=False

    End Sub

    ‘修改.doc文档的标题属性
    Sub modifyTitle(title)

    1. ActiveDocument.BuiltInDocumentProperties("Title") = title '修改.doc文档标题属性
    2. ActiveDocument.Save '保存

    End Sub

    ‘对图片进行处理
    Sub operPics()

    1. Selection.WholeStory '全选
    2. pic_num = Selection.InlineShapes.Count '获得图片个数
    3. For i = 1 To pic_num
    4. '对图片大小进行处理
    5. operPicSize i
    6. Next i
    7. '不保存原始doc-ActiveDocument.Save

    End Sub

    ‘对图片大小进行处理,将未完全显示出来的图片放大至固定宽
    Sub operPicSize(currentPic)

    1. fixed_width = 800 '设置固定宽度
    2. fixed_width_min = 500 '设置固定宽度的最小值
    3. minus_width = 50 '设置每次减少的宽度
    4. '计算图片原始宽度
    5. pic_width_raw = (Selection.InlineShapes(currentPic).Width * 100) / Selection.InlineShapes(currentPic).ScaleWidth
    6. writeLog ("图片显示比例:" & Selection.InlineShapes(currentPic).ScaleWidth & " 图片显示宽度:" & Selection.InlineShapes(currentPic).Width & " 图片原始宽度:" & pic_width_raw)
    7. If Selection.InlineShapes(currentPic).ScaleWidth < 100# Then
    8. '图片显示比例小于100%,需要设置固定宽度
    9. '尝试从800500,与图片显示宽度进行比较,
    10. try = 0
    11. Do
    12. fixed_width_current = fixed_width - minus_width * try
    13. '循环到最小固定宽度时结束
    14. If fixed_width_current < fixed_width_min Then
    15. Exit Do
    16. End If
    17. try = try + 1
    18. If fixed_width_current < pic_width_raw Then
    19. '若图片原始宽度比当前尝试的固定宽度大,则使用当前的固定宽度
    20. '计算长宽比保持不变的图片新高度
    21. height_new = (fixed_width_current / Selection.InlineShapes(currentPic).Width) * Selection.InlineShapes(currentPic).Height
    22. Selection.InlineShapes(currentPic).Width = fixed_width_current
    23. Selection.InlineShapes(currentPic).Height = height_new
    24. writeLog ("修改后图片显示宽度:" & fixed_width_current)
    25. Exit Do
    26. End If
    27. Loop
    28. End If

    End Sub

    ‘写日志
    Sub writeLog(data)

    1. Print #1, data

    End Sub

发表评论

表情:
评论列表 (有 0 条评论,78人围观)

还没有评论,来说两句吧...

相关阅读

    相关 word文档批量转换pdf

    今天和大家一起来学习一下word文档批量的转换pdf的方法。 如果你是单个文档转换pdf,可以直接打开word文档另存为,保存类型选择为pdf即可。但如果是工作或其它需要