当前位置: 首页 > news >正文

【VBA】把目录及子目录下所有doc/docx转换为pdf格式

Document.SaveAs2 方法 (Word) | Microsoft Learn

https://learn.microsoft.com/zh-cn/office/vba/api/word.wdsaveformat

新建一个1.xls

用office的excel打开1.xls

查看代码

把下面的代码粘贴到右边的代码区域 

Sub Main()Dim path As Stringpath = "F:\doc"VisitFolder (path)Debug.Print "操作完成"
End SubSub VisitFolder(fpath As String)Dim testStr As StringDim result As StringDim OFso As Object, baseFolder As Object, ofile As ObjectSet OFso = CreateObject("Scripting.FileSystemObject")Set baseFolder = OFso.GetFolder(fpath)Debug.Print "baseFolder: " & baseFolder' visit sub folderFor Each folder In baseFolder.SubFoldersDebug.Print "folder.path: " & folder.pathCall VisitFolder(folder.path)Next' visit filesFor Each ofile In baseFolder.FilesDebug.Print "ofile.path: " & ofile.pathPosition = InStr(1, ofile.path, ".doc") ' rename .doc to .pdfIf Position > 0 ThentestStr = Mid(ofile.path, 1, Position - 1)result = testStr & ".pdf"Debug.Print resultCall WordToPDF(ofile.path, result)ElseDebug.Print "子字符串未找到"End IfNext
End SubSub Main()Dim path As Stringpath = "F:\doc"VisitFolder (path)Debug.Print "操作完成"
End SubSub VisitFolder(fpath As String)Dim testStr As StringDim result As StringDim OFso As Object, baseFolder As Object, ofile As ObjectSet OFso = CreateObject("Scripting.FileSystemObject")Set baseFolder = OFso.GetFolder(fpath)Debug.Print "baseFolder: " & baseFolder' visit sub folderFor Each folder In baseFolder.SubFoldersDebug.Print "folder.path: " & folder.pathCall VisitFolder(folder.path)Next' visit filesFor Each ofile In baseFolder.FilesDebug.Print "ofile.path: " & ofile.pathPosition = InStr(1, ofile.path, ".doc") ' rename .doc to .pdfIf Position > 0 ThentestStr = Mid(ofile.path, 1, Position - 1)result = testStr & ".pdf"Debug.Print resultCall WordToPDF(ofile.path, result)ElseDebug.Print "子字符串未找到"End IfNext
End Sub' doc/docx save as pdf
Sub WordToPDF(srcpath As String, destpath As String)Dim word As ObjectDim doc As ObjectSet word = CreateObject("Word.Application")Set doc = word.Documents.Open(srcpath) ' doc, docx都可以doc.SaveAs2 Filename:=destpath, FileFormat:=17 ' pdfdoc.Closeword.Quit
End Sub' doc/docx save as pdf
Sub WordToPDF(srcpath As String, destpath As String)Dim word As ObjectDim doc As ObjectSet word = CreateObject("Word.Application")Set doc = word.Documents.Open(srcpath) ' doc, docx都可以doc.SaveAs2 Filename:=destpath, FileFormat:=17 ' pdfdoc.Closeword.Quit
End Sub


创建一些测试的目录和doc

代码的初始化目录设置下要转换的根目录,这里假设以E:\docs2pdfs\doc\为例:

设置好后,点击运行

如果电脑的word可以转换,这个脚本正常一般是可以执行的,执行完的效果会是这样:

 对应的目录会将docx生成pdf,其实doc也是可以转成pdf的

有兴趣的话,可以调试下vba脚本。

感谢阅读。 

相关文章:

  • synchronized 学习序章
  • 第三章支线五 ·组件之城 · 构建与复用的魔法工坊
  • 鹰盾加密虚拟机保护技术的深度解析:从指令级虚拟化到动态对抗系统
  • 【一文理解】下采样与上采样区别
  • 代码随想录算法训练营第60期第六十四天打卡
  • 什么是数据转换?数据转换有哪些方式?
  • C++ 智能指针实现原理
  • 香橙派3B学习笔记9:Linux基础gcc/g++编译__C/C++中动态链接库(.so)的编译与使用
  • Mybatisplus3.5.6,用String处理数据库列为JSONB字段
  • 【CF】Day80——Codeforces Round 872 (Div. 2) C⭐D (思维 + 模拟 | 树 + 思维 + 组合数学 + 分数取模)
  • 未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?
  • 【valse2025】CV与ML领域重要进展
  • python打卡训练营打卡记录day50
  • 【Java工程师面试全攻略】Day7:分布式系统设计面试精要
  • 蓝牙 BLE 扫描面试题大全(2):进阶面试题与实战演练
  • 聊聊 Pulsar:Producer 源码解析
  • python打卡day50
  • 常见的http状态码
  • 重温经典算法——二分查找
  • Word中如何对文献应用的格式数字连起来,如:【1-3】
  • 京东网站建设框架图/百度引擎
  • 山西建设厅网站密钥/产品推广ppt
  • 网站建设费应计入什么科目/优化方案模板
  • 做电子芯片的有那些交易网站/建网站需要哪些步骤
  • 学做网站要多久/百度认证官网
  • 广东网站建设企业/网络营销培训班