2016-07-09 13 views
0

エラー91に関連する他の関連するソリューションを適切にチェックしていますが、問題の解決方法が見つからないようです。VBAエラー91 - オブジェクトまたはブロック付き変数が設定されていません

次のVBAコードを使用してアプリケーションをクラッシュさせるか、Error 91 'ObjectまたはWith Block Variable not set'を表示して、さまざまなフォルダ(約500ファイル)にネストされたcoreldrawファイルを変換しようとしています。テストするために作成したファイルの他のデモセットと一緒に使用すると、同じコードが正常に動作します。

私は、スクリプトを処理するときにダイアログが表示されることがあります。はいの場合、どうすればこれらのダイアログボックスを回避できますか? Application.DisplayAlerts = Falseがcoreldrawで機能していません。

ただし、この場合は前提に過ぎません。誰かが問題を見つけるのを助けることができますか?私はCorelDrawのVBAを知らないが、私はあなたに次のコードを得ることができると仮定したい有効なコーレルドローファイルは

を発見されたかどうかをチェックしなければならないと言うだろうHERESにコード

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

良いスタート:

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文を収集サイドノートとしては、それらを無駄に複数の時間WHAを知るために、エラーメッセージをデバッグするとき、それは通常のに役立ちます

+0

@ManishJain、これを試しましたか? – user3598756

関連する問題