VBA move file to network folder and prompt if not connected

Felix Atagong

Active Member
Joined
Jun 27, 2003
Messages
359
After some parsing, I need to move certain files to a shared network folder, ...but if I haven't logged in before the VBA will give an error that the path does not exist.
I know there is a way to check if the folder is accessible or not but is it possible to prompt for the login and password (or fill these in automatically) so that the macro will proceed?
I use the following script (borrowed from a well-known Excel site):

Code:
Sub Move_Certain_Files_To_Folder()

    Dim FSO As Object
    Dim FromPath As String
    Dim ToPath As String
    Dim FileExt As String
    Dim FNames As String

    FromPath = "C:\E2A"
    ToPath = "Z:\edi\reception\" ' or "\\123.456.789.001\edi\reception\" (remote network folder)

    FileExt = "EDI_1234_*.*"   

    If Right(FromPath, 1) <> "\" Then
        FromPath = FromPath & "\"
    End If

    FNames = Dir(FromPath & FileExt)
    If Len(FNames) = 0 Then
        MsgBox "No files in " & FromPath
        Exit Sub
    End If

    Set FSO = CreateObject("scripting.filesystemobject")

    FSO.MoveFile Source:=FromPath & FileExt, Destination:=ToPath
    MsgBox "FILES TRANSFERRED TO " & ToPath

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top