通过Excel VBA将多个visio文件转换为pdf

时间:2016-06-04 03:29:13

标签: vba excel-vba pdf macros visio

我遇到了这段代码的问题。 这段代码的目的是将保存在文件夹中的所有visio文件转换为来自EXCEL VBA宏的pdf ...

使用此代码,我可以打开visio文件但无法转换为pdf文件....它显示" Object Required"错误....

FName = myPath & myFile
    Dim objDoc: Set objeDoc = VisioApp.documents.Open(FName)
objDoc.ExportAsFixedFormat VisFixedFormatTypes = 1, FName, VisDocExIntent = 1, VisPrintOutRange = 0

我遇到上述行的问题

Sub LoopAllExcelFilesInFolder()

Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Dim FName As String

'Optimize Macro Speed
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
'Retrieve Target Folder Path From User
  Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    With FldrPicker
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1) & "\"
    End With
'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
  myExtension = "*.vsd"
'Target Path with Ending Extention
  myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder

  Do While myFile <> ""


    Set VisioApp = CreateObject("Visio.Application")
    FName = myPath & myFile
    Dim objDoc: Set objeDoc = VisioApp.documents.Open(FName)

   'Application.ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, "C:\Users\20098323\Desktop\BHAVIK\VAC-30001.pdf", visDocExIntentPrint, visPrintAll, 1, 53, False, True, True, True, False
objDoc.ExportAsFixedFormat VisFixedFormatTypes = 1, FName, VisDocExIntent = 1, VisPrintOutRange = 0
      myFile = Dir
  Loop
'Message Box when tasks are completed
  MsgBox "Task Complete!"
ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

Set VisioApp = CreateObject("Visio.Application")
FName = myPath & myFile
VisioApp.documents.Open FName
Application.ActiveDocument.ExportAsFixedFormat visFixedFormatPDF, _
             FName, visDocExIntentPrint, visPrintAll, _
             1, 53, False, True, True, True, False

在这里,您创建了一个Visio VisioApp的实例,但在最后一行中使用了Application - 默认情况下,这将指向代码运行的Excel,而不是Visio。您需要将其替换为VisioApp

此外,您正在使用Visio对象库(visFixedformatPDF等)定义的某些常量。 Excel VBA不知道这些常量的值,因此您可以执行以下操作之一:

  1. 在Excel VBProject中将引用添加到Visio对象库
  2. 将常量替换为其数值(可以在Visio VBE对象浏览器中查找)
  3. 在Excel VBA代码的顶部定义常量和值。例如:

    Const visFixedFormatPDF = 1