用WORD中的vba批量转换为PDF

转载自:licanli2082@126网易博客

对于只是简单运行的方法,步骤如下:

1. 让WORD2007、2010调出开发选项卡。调出的办法是,点击WORD左上角的圆圈形按钮,从下拉菜单中选择”选项“,然后点击左侧的常规选项卡,再从右边窗口中选中”显示开发工具“
 
2. 回到WORD主界面,选择”开发工具“面板栏,从这个面板的左边选择”宏“按钮
3. 在宏窗口中输入一个宏名字(任意),然后点新建。
4. 在宏编辑窗口中,将下面的代码粘贴到sub与end sub中间,然后点绿色三角形的运行按钮即可
5. 浏览选择要转换的WORD文档,确定,看窗口状态栏提示完成,即可看到所有WORD文档转换了PDF在原目录(原来的WORD文档不会被影响,请放心)
如下图所示,

 

用WORD中的vba批量转换为PDF_第1张图片

 

Application.DisplayAlerts = False
Application.ScreenUpdating = False
 
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
Dim wdDoc As Document
Dim showFolder As Boolean
showFolder = False
With fDialog
.Filters.Add "Word文件", "*.doc;*.docx;*.docm", 1
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
'如果选择了本文档则跳过
If InStrRev(vrtSelectedItem, ThisDocument.Name) = 0 Then
On Error Resume Next
Set wdDoc = Application.Documents.Open(vrtSelectedItem, ReadOnly:=True)
wdDoc.SaveAs Left(vrtSelectedItem, Len(vrtSelectedItem) - 5), wdFormatPDF
wdDoc.Close False
 
End If
Next vrtSelectedItem
If showFolder Then Call Shell("explorer.exe " & Left(fDialog.SelectedItems(1), _
InStrRev(fDialog.SelectedItems(1), "")), vbMaximizedFocus)
End If
End With
 
Set fDialog = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True

你可能感兴趣的:(word,pdf,VBA)