office VB 【word批量转换pdf】

1、office选项自定义打开开发者工具

2、点击visual basic编辑器,插入模块,运行代码

Sub ConvertWordsToPdfs()
'Updated by Extendoffice 20181123
Dim xIndex As String
Dim xDlg As FileDialog
Dim xFolder As Variant
Dim xNewName As String
Dim xFileName As String
Set xDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xDlg.Show <> -1 Then Exit Sub
xFolder = xDlg.SelectedItems(1) + "\"
xFileName = Dir(xFolder & "*.*", vbNormal)
While xFileName <> ""
If ((Right(xFileName, 4)) <> ".doc" Or Right(xFileName, 4) <> ".docx") Then
xIndex = InStr(xFileName, ".") + 1
xNewName = Replace(xFileName, Mid(xFileName, xIndex), "pdf")
Documents.Open FileName:=xFolder & xFileName, _
ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""
ActiveDocument.ExportAsFixedFormat OutputFileName:=xFolder & xNewName, _
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
ActiveDocument.Close
End If
xFileName = Dir()
Wend
End Sub

 

 

 

 3、选择目录批量转换,转换后的PDF会在原目录下生成

 

 

二、其他小功能:

1、 ## 统一表格宽度
Sub AdjustTableSize()
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.AutoFitBehavior (wdAutoFitWindow)
Next tbl
End Sub

2、##统一底色
Sub AdjustTableSize()
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
tbl.AutoFitBehavior (wdAutoFitWindow)
If tbl.Rows.Count >= 1 Then ' 检查表格是否有至少一行
With tbl.Rows(1).Range.Shading
.BackgroundPatternColor = RGB(244, 244, 244) ' 设置底色,这里使用红色作为示例
End With
End If
Next tbl
End Sub

 

posted @ 2023-04-08 00:21  The-End-Ronin  阅读(253)  评论(0编辑  收藏  举报