返回列表 上一主題 發帖

檢查檔案是否存在

檢查檔案是否存在

請問程式如何以IF 去檢查
檔案TEST.XLS 是否存在於 C:\


檔案 TEST1.XLS 是否已開啟

請指教

If Dir("C:\TEST.XLS") = Empty Then
     MsgBox "文件不存在。"
   End If
   On Error Resume Next
   Set B = Workbooks("TEST.XLS")
If Err.Number = 0 Then
        MsgBox testfile & "檔案已開啟"
    End If

TOP

請問程式如何以IF 去檢查
檔案TEST.XLS 是否存在於 C:\


檔案 TEST1.XLS 是否已開啟

請指教
enoch 發表於 2011-7-27 14:13



這個可查不同EXCEL進程
參考: http://support.microsoft.com/kb/209189/zh-tw

Sub test()
    Dim result As Integer
    result = FileLocked("C:\TEST.XLS")
    Select Case result
     Case Is = 76
        MsgBox ("路徑不存在。")
     Case Is = 75
        MsgBox ("文件不存在。")
     Case Is = 70
        MsgBox ("檔案已開啟")
     Case Is = 0
        MsgBox ("檔案正常")
    Case Else
        MsgBox ("檔案有不明問題")
    End Select
End Sub
Function FileLocked(strFileName As String) As Integer
   On Error Resume Next
   Open strFileName For Binary Access Read Write Lock Read Write As #1
   Close #1
   FileLocked = Err.Number
   If Err.Number <> 0 Then
    Err.Clear
   End If
End Function
懂得發問,答案就會在其中

今日の一秒は  明日にない
http://kimbalko-chi.blogspot.com
http://kimbalko.blogspot.com

TOP

  1. Sub Ex()
  2.     Dim F As Boolean
  3.     F = CreateObject("Scripting.FileSystemObject").FileExists("D:\TEST.XLS")
  4.     If F = False Then MsgBox "找不到檔案 ??"
  5. End Sub
複製代碼

TOP

謝謝教學,我正好尋找類似問題的vba寫法
Thanks!

TOP

        靜思自在 : 【時間無法遮擋】怕時間消逝,花了許多心血,想盡各式方法要遮擋時間,結果是:浪費了更多時間,且一無所成!
返回列表 上一主題