Gexxx
Gexxx

Reputation: 13

VBA - SaveAs - runtime error if network not accessable

I am trying to save an excel file into a network drive with the code bellow. The problem is that if the network is not accessable, it gives a runtime error.

CAn you please help how to modify the code - to save file on given network drive if network accessable - if not, save into local machine (create a copy in doucuments folder)

Thanks

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

Upvotes: 1

Views: 78

Answers (2)

A.S.H
A.S.H

Reputation: 29352

'.... 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

Upvotes: 2

Tim Wilkinson
Tim Wilkinson

Reputation: 3791

You could use error handling, so if there is an error change the path to something local.

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"

Upvotes: 1

Related Questions