Sub photoConv()
Dim myFSO As New FileSystemObject
Dim myPath As String
Dim myPhoto As String
myPath = ThisWorkbook.Path
myPhoto = myPath & "\" & "原始相片" & "\" & "*.jpg"
If myFSO.FileExists(myPhoto) Then
MsgBox "good job"
Else
MsgBox "not good"
End If
End Sub
以上會顯示「not good」
-------------------------------------------------------------------------------
Sub photoConv()
Dim myFSO As New FileSystemObject
Dim myPath As String
Dim myPhoto1 As String
myPath = ThisWorkbook.Path
myPhoto1 = myPath & "\" & "原始相片" & "\" & "1Blue hills.jpg"
If myFSO.FileExists(myPhoto1) Then
MsgBox "good job"
Else
MsgBox "not good"
End If
End Sub
回復 4#baconbacons
試試看 我曾經寫過
Sub test()
Dim FileName As Variant
Dim xlfileName As String
Dim FolderName As Variant
Dim f1 As Object
Set f1 = CreateObject("Scripting.FileSystemObject")
Set myfiles = f1.GetFolder(ThisWorkbook.Path ).Files
n = 0
For Each myfile In myfiles
If UCase(f1.getextensionname(Path:=myfile)) = "JPG" _
Or UCase(f1.getextensionname(Path:=myfile)) = "JPEG" _
Or UCase(f1.getextensionname(Path:=myfile)) = "GIF" _
Or UCase(f1.getextensionname(Path:=myfile)) = "BMP" Then
n = n + 1
End If
MsgBox n
Set f1 = Nothing
End Sub作者: baconbacons 時間: 2014-3-31 09:58
回復 6#yangjie
yangjie大:以下計算圖片數量的寫法是我先在網路上找到的方法「Files.Count」
但不知道為什麼數量結果總是會多出1,所以我必須要扣除1才會符合實際數目
我也有確認過並沒有隱藏檔,能否知道為何有這種情況?
ps:yangjie大 你的方法確實可以 感謝教導
另外再請教yangjie大,你在使用For Each ____ In ____的寫法
是怎樣的思考方式,在什麼情境下會考慮使用到這種方法
這寫法看別人寫都是可以理解,但不知道怎麼用的出來
麻煩指教
----------------------------------------------------------------------------------------------------
Sub photoConv()
Dim myFSO As New FileSystemObject
Dim myPath As String
Dim myPhoto As String, countPhoto As String
myPath = ThisWorkbook.Path
myPhoto = Dir(myPath & "\" & "原始相片" & "\" & "*.jpg")
countPhoto = myFSO.GetFolder(myPath & "\" & "原始相片").Files.Count - 1
If myPhoto <> "" Then
MsgBox (countPhoto)
Else
MsgBox "no photo"
End If
End Sub作者: yangjie 時間: 2014-3-31 11:02
本帖最後由 yangjie 於 2014-3-31 11:09 編輯
回復 7#baconbacons
Sub test()
Dim FileName As Variant
Dim xlfileName As String
Dim FolderName As Variant
Dim f1 As Object
Set f1 = CreateObject("Scripting.FileSystemObject")
n= f1.GetFolder(ThisWorkbook.Path ).Files.Count
MsgBox n
Set f1 = Nothing
End Sub
以上 經測試是 OK 的
f1.GetFolder(ThisWorkbook.Path ).Files 僅是沒有篩選檔案類型的物件
我也是從版主們指導下 體會 學來的
for each a in Object
next
通常是 在每一個(each a)須做同一動作(或在篩選條件下) 使用
這是個人想法
希望對你有所幫助作者: baconbacons 時間: 2014-3-31 16:11
修改後程式碼如下:
Sub test1()
Dim f1 As Object
Set f1 = CreateObject("Scripting.FileSystemObject")
n = f1.GetFolder(ThisWorkbook.Path & "\" & "原始相片").Files.Count
MsgBox n
Set f1 = Nothing
End Sub作者: yangjie 時間: 2014-4-1 00:06