该代码将帮助大家实现,不改动原本visio文件,仅打开一个文档可完成某一文件夹中visio文件批量导出为PDF文件,并保持原文件命名。
##注释中涉及须按需更改的地方均已表明。
##本vba代码适用于在一个Visio文档中批量将某文件夹中visio文件导出为pdf。
Option Explicit
Sub visio2pdf()
On Error Resume Next
Dim sEveryFile As String, sSourcePath As String, sNewSavePath As String
Application.ScreenUpdating = False '冻结屏幕,以防屏幕抖动
sSourcePath = "D:\test\"
'定位需要修改的文件所在文件夹,注意以\结尾,否则无法读取
Dim vsoDocument As Visio.Document
Dim icount
sEveryFile = Dir(sSourcePath & "*.vsdx")
'specify the necessary document type like "*.doc"
'将需要修改的文件类型锁定,可修改为所需的文件后缀
icount = 0
Dim workname As String
Do While sEveryFile <> ""
workname = Left(sEveryFile, Len(sEveryFile) - 5)
'rename as none .vsdx---need to fix the len you want to delete
'去掉文件名中的文件类型
Set vsoDocument = Documents.Open _
(sSourcePath & workname & ".vsdx")
'Open an existing document
'打开文件
vsoDocument.ExportAsFixedFormat visFixedFormatPDF, workname & ".pdf", visDocExIntentPrint, visPrintAll
'save as pdf in the existing file folder
'将文件导出为pdf
vsoDocument.Close
'Close an existing document
'关闭文件
sEveryFile = Dir
icount = icount + 1
Loop
Application.ScreenUpdating = True '冻结屏幕,此类语句一般成对使用
MsgBox "一共打开" & icount & "个文件"
End Sub