2016-07-09 125 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

相關問題