0

I have a macro in excel that if a drive exists the macro saves the file to my harddrive and thumbdrive. If it doesn't exist, it saves to the harddrive. When the macro runs I am getting an error. Here is the macro:

Sub SaveFile()

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject

Dim filepath As String

name = "Siemens"
filepath = "F:\Dave backup\Open Orders\Label Manifests\Active  Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")

 If fso.DriveExists("F:\") = True Then

        'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
        'ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
        ActiveWorkbook.SaveAs filename:=filepath
    Else

         'ActiveWorkbook.SaveAs filename:="C:\Users\dgray\Documents\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")
        ActiveWorkbook.SaveAs filename:="F:\Dave backup\Open Orders\Label Manifests\Active  Labels Manifest\Manifest Related\File saving testing folder\" & name & "\" & name & " Manifest " & Format(Now, "mm-dd-yyyy")


    End If

End Sub

Here is the error I am getting:

Error Message

I don't know if you can see but the last part of the error message says "\Siemens\8E555720. That should also say the customer name (i.e. Siemens). In the code I have set the customer name in the variable "name". So why is it giving me this crazy error? All help is appreciated.

5
  • I can't see your linked image cause domain is restricted on my end, so I don't know the error. But if what you pasted is your exact code as-is, then your Else statement is trying to save-as to drive F which it just verified is not there. Maybe you meant for the else to only save to drive C:? Commented May 4, 2020 at 20:03
  • 1
    See what Debug.Print filepath prints, fix accordingly ;-) Commented May 4, 2020 at 20:04
  • Add the extension to the end too. Commented May 4, 2020 at 20:10
  • add extension to the end of what? Commented May 4, 2020 at 20:27
  • To the end of the file name. Commented May 4, 2020 at 21:28

2 Answers 2

1

Something like this might be better:

Sub SaveFile()

    Const PATH_C As String = "C:\Users\dgray\Documents\"
    Const PATH_F As String = "F:\Dave backup\Open Orders\Label Manifests\" & _
        "Active Labels Manifest\Manifest Related\File saving testing folder\"

    Dim fileName As String, custName As String

    custName = "Siemens"

    fileName = custName & " Manifest " & Format(Now, "mm-dd-yyyy") & ".xlsx" 'or .xlsm

    ActiveWorkbook.SaveAs fileName:=PATH_C & fileName 'assume C is always available

    'save to F if available
    If Len(Dir(PATH_F)) > 0 Then
        'assumes the custName folder already exists...
        ActiveWorkbook.SaveAs fileName:=PATH_F & custName & "\" & fileName
    End If

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

I will try this tomorrow and let you know how it works. Where is it checking to see if F drive exists?
If Len(Dir(PATH_F)) > 0 checks to see if the path defined in the constant PATH_F is a valid path: it check the whole path, not just F:\
Ok. That part doesn't work. The folders I have listed in the code block are for testing purposes. I will verify the filepath and let you know the results.
"That part doesn't work" - that doesn't tell me much. What does it do instead of work?
0

I can see the space in folder name which may cause this error.

By removing space in the foldername this error would be fixed.

4 Comments

When I am using this, there is an F drive (my thumbdrive as backup). It works for other customers I use this for, but for some reason this is the only one I am having issues with.
By removing space in the foldername this error still show up?
I can't remove the spaces. Those are the folders on my network.
vbforums.com/…. This will help you chr$(34)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.