【VBA】--利用VBA代码导出Visio设计图
通过Visio可以绘制各种架构图、业务流程图,但是Visio并没有直接将一个visio文件中所有绘图导出成图片的功能。通过观察宏录制,结合一些实践,小Z习得了如何使用vba代码导出图片。
步骤为:
1.开启“开发工具”,点击“宏”,创建新宏。
2.编写代码。
代码如下:
Sub ExportToImage()
'Enable diagram services
Dim DiagramServices As Integer
Dim vsoPage As Visio.Page
Dim vsoDocuments As Visio.Documents
Dim vsoPages As Visio.Pages
Dim exportFileUrl As String
Dim exportFileFormat As String
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
exportFileUrl ="C:\Users\18495\Desktop\"
exportFileFormat =".jpg"
Set vsoDocuments = Application.Documents
For Each vsoDocument In vsoDocuments
Set vsoPages = vsoDocument.Pages
For Each vsoPage In vsoPages
Application.ActiveWindow.Page = Application.ActiveDocument.Pages.Item(vsoPage.Name)
Application.Settings.SetRasterExportResolution visRasterUseScreenResolution, 144#, 144#, visRasterPixelsPerInch
Application.Settings.SetRasterExportSize visRasterFitToSourceSize, 11#, 7.006944, visRasterInch
Application.Settings.RasterExportColorFormat = visRasterRGB
Application.Settings.RasterExportOperation = visRasterBaseline
Application.Settings.RasterExportRotation = visRasterNoRotation
Application.Settings.RasterExportFlip = visRasterNoFlip
Application.Settings.RasterExportBackgroundColor = 16777215
Application.Settings.RasterExportQuality = 75
Application.ActiveWindow.Page.export exportFileUrl + vsoPage.Name + exportFileFormat
Next
Next
'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices
End Sub
其中小Z增加了2个变量,用于设置文件路径(exportFileUrl)和文件格式(exportFileFormat),实际使用可以进行变量修改。
之后只要运行代码,目前就默认在桌面上生成一堆图片了,然后就可以愉快的使用这些图片,这可以节省大量时间,毕竟一份visio中包含了大量的图需要使用,一个一个导出太耗费时间了。