我写了个程序从映射的盘符考文件到另一个映射盘符。X,P都是映射盘符,但有时会提示文件未找到,偶尔会提示文件已存在,但我已经在程序里做了判断了的,另外,如果我不想它跳提示,要怎么做,因为我是加在计划任务里的,如果跳提示了。没能及时点掉,就影响下次计划任务的运行了
我判断网络盘是否存在是这样写的
' Check the network if work
If Dir("X:\", 16) = "" Or Dir("P:\", 16) = "" Then
pId = Shell("Connect.bat", 1)
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 等待程序结束
Call CloseHandle(pHnd)
End If
End If
我判断文件是否存在是这样写的,我是先判断目的文件夹是不是有着个文件,有就不拷贝了:
If Dir(destpath & "\" & filename) = "" Then
FileCopy Sourcepath & "\" & Fielname, Destpath & "\" & Filename
End If
我判断网络盘是否存在是这样写的
' Check the network if work
If Dir("X:\", 16) = "" Or Dir("P:\", 16) = "" Then
pId = Shell("Connect.bat", 1)
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 等待程序结束
Call CloseHandle(pHnd)
End If
End If
我判断文件是否存在是这样写的,我是先判断目的文件夹是不是有着个文件,有就不拷贝了:
If Dir(destpath & "\" & filename) = "" Then
FileCopy Sourcepath & "\" & Fielname, Destpath & "\" & Filename
End If
Dim sDire As String '目录 (不含“\”)
Dim sFile As String '文件名
Dim sRes As String
Dim iRes As Integer
sDriv = "H:"
sDire = ""
sFile = "sql.txt"
On Error GoTo ErrLine
iRes = VBA.FileSystem.GetAttr(sDriv)
On Error GoTo 0
If Not ((iRes And VBA.vbDirectory) = VBA.vbDirectory) Then
MsgBox "磁盘“" + sDriv + "”不存在!"
Exit Sub
End If
If Len(sDire) > 0 Then '如目录字符串不空,则检查目录
sRes = VBA.FileSystem.Dir(sDriv + "\" + sDire, vbDirectory)
sRes = LCase(sRes)
If LCase(Right(sDire, Len(sRes))) <> sRes Then
MsgBox "路径“" + sDriv + "\" + sDire + "”不存在!"
Exit Sub
End If
End If
sRes = VBA.FileSystem.Dir(sDriv + "\" + IIf(Len(sDire) > 0, sDire + "\", "") + sFile)
If LCase(sRes) <> LCase(sFile) Then
MsgBox "文件“" + sDriv + "\" + IIf(Len(sDire) > 0, sDire + "\", "") + sFile + "”不存在!"
Exit Sub
End If
MsgBox "文件“" + sDriv + "\" + IIf(Len(sDire) > 0, sDire + "\", "") + sFile + "”存在!", vbInformation, "提示"
Err.Clear
Exit Sub
ErrLine:
Err.Clear
MsgBox "磁盘“" + sDriv + "”不存在!"
End Sub参考一下这个,已经过测试(映射网络驱动器、及物理磁盘中均测试通过)!
不想跳提示的做法,就是用 前面的 ON ERROR (你可以查阅一下 ON ERROR 的用法!)sub aa
'被“on error errLine 及 on error goto 0 包着的代码段中一旦有异常错误时程序均会自动跳转到 errLine: 后去执行...
on error errLine
' Check the network if work
If Dir("X:\", 16) = "" Or Dir("P:\", 16) = "" Then
pId = Shell("Connect.bat", 1)
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 等待程序结束
Call CloseHandle(pHnd)
End If
End If
on error goto 0
exit suberrLine:
err.clear
end sub
更正楼上的 on error errLine 应为 on error goto errLinesub aa
'被“on error goto errLine 及 on error goto 0 包着的代码段中一旦有异常错误时程序均会自动跳转到 errLine: 后去执行...
on error goto errLine
' Check the network if work
If Dir("X:\", 16) = "" Or Dir("P:\", 16) = "" Then
pId = Shell("Connect.bat", 1)
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 等待程序结束
Call CloseHandle(pHnd)
End If
End If
on error goto 0
exit suberrLine:
err.clear
end sub