批量将Word文档转换为PDF的VBA宏
' 批量将Word文档转换为PDF的VBA宏 ' ' 该宏用于将Excel表格中指定的Word文档转换为PDF格式。 ' ' 使用方法: ' 1. 打开Excel表格,并确保表格中包含需要转换的Word文档的文件名(例如:"F:\任务1、602-五室-机械系统\10 验收\文档1.docx")。 ' 2. 在Excel表格中选中需要转换的Word文档的单元格范围。 ' 3. 在VBA编辑器中运行此宏。 ' ' 注: ' 1. 请确保您的计算机上已安装Microsoft Word。 ' 2. 请将代码中的文件路径和文件名替换为您的实际路径和文件名。 '
Sub SubToPDF()
' 定义变量 Dim i As Integer Dim wordApp As Word.Application Set wordApp = CreateObject("Word.Application") Dim name, wordName As String Dim doc As Word.Document Dim j As Integer
' 循环遍历Excel表格中指定的单元格范围 For j = 1 To 2 For i = 2 To 21
Debug.Print Cells(i, j).Value
wordName = "F:\任务1、602-五室-机械系统\10 验收\" & Cells(i, j) & ".docx"
name = "F:\任务1、602-五室-机械系统\10 验收\" & Cells(i, j) & ".pdf"
' 打开Word文档
Set doc = wordApp.Documents.Open(wordName)
' 将Word文档转换为PDF格式
doc.ExportAsFixedFormat OutputFileName:= _
name, ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, to:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
name = "F:\任务1、602-五室-机械系统\10 验收\" & Cells(i, j) & "-封面.pdf"
doc.ExportAsFixedFormat OutputFileName:= _
name, ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=1, to:=1, Item:= _
wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
' 关闭Word文档
doc.Close
Next Next
' 退出Word应用程序 wordApp.Quit
End Sub
原文地址: https://www.cveoy.top/t/topic/njFJ 著作权归作者所有。请勿转载和采集!