2016-07-09 97 views
0

请不要标记重复项,因为我已经正确地检查了特定于错误91的其他相关解决方案,但没有一个人似乎为我解决我遇到的问题提供了解决方案。VBA错误91-对象或块变量未设置

我试图使用以下VBA代码将嵌套在各种文件夹(大约500个文件)中的我的coreldraw文件转换为其应用程序,或者显示错误91'对象或未设置块变量'。与我创建的其他演示文件集一起使用时,相同的代码绝对可以正常工作。

我可以推测的一种情况是在处理脚本时显示一些对话框的文件。如果是,我应该如何阻止这些对话框。 Application.DisplayAlerts = False在coreldraw中不起作用。

但是,这种情况只是一个假设。有人能帮我找到问题吗?继承人的代码

Sub NewFolder() 
Dim FileSystem As Object 
Dim HostFolder As String 
HostFolder = "My folder Path" 
Set FileSystem = CreateObject("Scripting.FileSystemObject") 
DoFolder FileSystem.GetFolder(HostFolder) 
End Sub 

Sub DoFolder(folder) 
Dim SubFolder 
For Each SubFolder In folder.SubFolders 
    DoFolder SubFolder 
Next 
Dim File 
For Each File In folder.Files 
If InStr(File.Name, ".cdr") Then 
Application.OpenDocument (File) 
End If 
Dim filepath As String 
filepath = ActiveDocument.FullFileName 
Dim doc1 As Document 
Dim SaveOptions As StructSaveAsOptions 
Set SaveOptions = CreateStructSaveAsOptions 
Set doc1 = ActiveDocument 

With SaveOptions 
    .EmbedVBAProject = True 
    .Filter = cdrCDR 
    .IncludeCMXData = False 
    .Range = cdrAllPages 
    .EmbedICCProfile = True 
    .Version = cdrVersion17 
End With 

doc1.SaveAs filepath, SaveOptions 
doc1.Close 
    ' Operate on each file 
Next 
End Sub 
+0

调试错误消息,当知道世界卫生大会通常帮助t行正在抛出错误。 – Jeeped

回答

0

我说你必须检查是否有有效的Corel绘图文件已经发现

我不知道的CorelDraw VBA但我认为你可以得到下面的代码作为良好的开端:

Sub DoFolder(folder) 
    Dim SubFolder 
    For Each SubFolder In folder.SubFolders 
     DoFolder SubFolder 
    Next 
    Dim file 
    Dim doc1 As Document 
    Dim filepath As String 
    Dim SaveOptions As StructSaveAsOptions 
    For Each file In folder.Files 
     If InStr(file.Name, ".cdr") Then 
      Set doc1 = GetDocument(file) '<--| try and get a valid CorelDraw document with the given file: see 'GetDocument()' function at the bottom 
      If Not doc1 Is Nothing Then '<--| if you succeed then go on with your code 
       filepath = ActiveDocument.FullFileName 
       Set SaveOptions = CreateStructSaveAsOptions 
       With SaveOptions 
        .EmbedVBAProject = True 
        .Filter = cdrCDR 
        .IncludeCMXData = False 
        .Range = cdrAllPages 
        .EmbedICCProfile = True 
        .Version = cdrVersion17 
       End With 
       doc1.SaveAs filepath, SaveOptions 
       doc1.Close 
      End If 
     End If 
     ' Operate on each file 
    Next 
End Sub 

Function GetDocument(file As Variant) As Document 
    On Error Resume Next 
    Set GetDocument = OpenDocument(file) 
End Function 

作为一个方面说明我收集之外的所有Dim语句循环不要让他们跑白白多时间

+0

@ManishJain,你试过这个吗? – user3598756

相关问题