我正在尝试使用下面的代码将excel文件保存到网络驱动器中。 问题是,如果网络不可访问,则会产生运行时错误。
您可以帮忙修改代码 - 如果网络可访问,则将文件保存在给定的网络驱动器上 - 如果没有,保存到本地机器(在doucuments文件夹中创建一个副本)
由于
Dim datum_ As String
Dim path_ As String
datum_ = Format(Now, "yyyy-mm-dd hh-mm")
path_ = "\\networkfolder"
'Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=path_ & datum_ & ".xlsm"
'Application.DisplayAlerts = True
答案 0 :(得分:2)
'.... code
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.SaveAs Filename:=path_ & datum_ & ".xlsm"
If Err.Number <> 0 Then ActiveWorkbook.SaveAs Filename:=myLocalPath_ & datum_ & ".xlsm"
Application.DisplayAlerts = True
On Error Goto 0
' .... code
答案 1 :(得分:1)
您可以使用错误处理,因此如果出现错误,请更改本地路径。
Dim datum_ As String
Dim path_ As String
datum_ = Format(Now, "yyyy-mm-dd hh-mm")
path_ = "\\networkfolder"
On Error GoTo localPath
'Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=path_ & datum_ & ".xlsm"
'Application.DisplayAlerts = True
'rest of code here
Exit Sub
localPath:
path_ = "insert local path here"
ActiveWorkbook.SaveAs Filename:=path_ & datum_ & ".xlsm"