2017-10-03 19 views
0

画像のリストとそのサイズ(幅/高さ)をspreadshitするマクロを使用しようとしています。私はマクロを実行すると数秒間実行され、フォルダ内の2000個のイメージのうち116個のレコードを取得し、オートメーションエラーでクラッシュするだけではありません。Excel VBA - オートメーションエラー

私はfixexを適用するために調査し、私はアップデート、repoarオフィスなどで見つけることができ、何もこの問題を修正していません。

Public Sub Image() 
Dim strFile As String 
Dim stdPic As StdPicture 
Dim lngWidth As Long 
Dim lngHeight As Long 
Dim strPath As String 
Dim lngRow As Long ' Made this a Long just in case you have a LOT of pictures 

strPath = "C:\IMAGES\" 

' Get all files (we'll filter the results below) 
' strFile = Dir$(strPath & "\*.jpg") 
    strFile = Dir$(strPath & "\*.*") 

' Find the last row in Col A 
lngRow = Range("A10000").End(xlUp).Row 

Do While Len(strFile) 
    ' Select the picture types you want. In this case jpg, bmp and png 
    If UCase$(Right$(strFile, 4)) = ".JPG" Or _ 
     UCase$(Right$(strFile, 4)) = ".BMP" Or _ 
     UCase$(Right$(strFile, 4)) = ".PNG" Then 
     Set stdPic = LoadPicture(strPath & "\" & strFile) 
     lngRow = lngRow + 1 
     Range("A" & lngRow).Value = strFile 
     Range("B" & lngRow).Value = Round(stdPic.Width/26.4583) 
     Range("C" & lngRow).Value = Round(stdPic.Height/26.4583) 
    End If 
    strFile = Dir$ 
Loop 
End Sub 

enter image description here

誰もがアイデア、なぜこれが起こっているがありますか?

+0

可能性が高い画像ファイルのいずれかをロードするために失敗しています。 'set stdPic'行をいくつかの適切なエラー処理とそれが画像をロードしたことを確認してラップします。もう一つの有用な保護手段は、 "〜image.JPG"のようなテンポラリファイルを避けるために〜で始まるファイルを避けることです。 – Zerk

+0

したがって、特定のイメージを削除してもまだ116を実行して停止します。 :-( – Slavisha

+0

'Set stdPic = LoadPicture ...'の直前に 'Debug.Print"を追加して読み込み: "&strPath&" \ "&strFile'これは読み込みしようとしている画像を確認するものです。奇妙なことに他のものと違うものがあるかもしれません。 – BruceWayne

答えて

0

これはトリックを行う必要があります。

いくつかのコメント:

  • は二重のバックスラッシュを避け、あなたはstrPathにそれを持っていただけでなく、strFileの割り当てでそれを使用しました。
  • 画像の読み込みに失敗した場合は、stdPicをクリアし、 エラー処理を無効にし、stdPicを読み込み、エラー処理を再度有効にし、 画像が正しく読み込まれるようにstdPicが存在するかどうかを確認します。それは しなかった場合、それはエントリを取得しません。 が間違って破損する可能性があることに対処するときは、それが正常であることを確認することが最善であると が期待通りに機能しています。以下

改正サブ:

Public Sub Image() 
Dim strFile As String 
Dim stdPic As StdPicture 
Dim lngWidth As Long 
Dim lngHeight As Long 
Dim strPath As String 
Dim lngRow As Long ' Made this a Long just in case you have a LOT of pictures 

strPath = "C:\IMAGES" 

' Get all files (we'll filter the results below) 
' strFile = Dir$(strPath & "\*.jpg") 
    strFile = Dir$(strPath & "\*.*") 

' Find the last row in Col A 
lngRow = Range("A10000").End(xlUp).Row 

Do While Len(strFile) 
    ' Select the picture types you want. In this case jpg, bmp and png 
    If Left(strFile, 1) <> "~" And _ 
     (UCase$(Right$(strFile, 4)) = ".JPG" Or _ 
     UCase$(Right$(strFile, 4)) = ".BMP" Or _ 
     UCase$(Right$(strFile, 4)) = ".PNG") Then 
      Set stdPic = Nothing 
      On Error Resume Next 
       Set stdPic = LoadPicture(strPath & "\" & strFile) 
      On Error GoTo 0 
      If Not stdPic Is Nothing Then 
       lngRow = lngRow + 1 
       Range("A" & lngRow).Value = strFile 
       Range("B" & lngRow).Value = Round(stdPic.Width/26.4583) 
       Range("C" & lngRow).Value = Round(stdPic.Height/26.4583) 
      End If 
    End If 
    strFile = Dir$ 
Loop 
End Sub 
+0

Excelent、どうもありがとうございます。 – Slavisha

関連する問題