使用word批量将.docx(或者.doc)转成.pdf

1.打开word,按下alt+F11,打开Microsoft Visual Basic for Applications;

2.点击“插入”,再点击“模块”,将一下代码复制粘贴到那个文本框内;

'docx转pdf
Option Explicit
Sub docx2pdf()
Dim sEveryFile As String
Dim sSourcePath As String
Dim sNewSavePath As String
Dim CurDoc As Object
sSourcePath = "C:\files_zhang\信安2班实验\信安2班安全编程实验报告2\"
sEveryFile = Dir(sSourcePath & "*.docx")
Do While sEveryFile <> ""
    Set CurDoc = Documents.Open(sSourcePath & sEveryFile, , , , , , , , , , , msoFalse)
    sNewSavePath = VBA.Strings.Replace(sSourcePath & "PDF文件\" & sEveryFile, ".docx", ".pdf")
    CurDoc.SaveAs2 sNewSavePath, wdFormatPDF
    CurDoc.Close SaveChanges:=False
    sEveryFile = Dir
Loop
Set CurDoc = Nothing
End Sub

3.注意,第8行的sSourcePath 的值为你想要批量转的docx所在的文件目录,并且要在docx所在的同级目录下创建一个叫"PDF文件"的文件夹,这样批量转的pdf就会保存到这个"PDF文件"中。

猜你喜欢

转载自blog.csdn.net/a_cherry_blossoms/article/details/111499673