How can we copy a one file from Remote desktop to local machine using Excel VBA

只谈情不闲聊 提交于 2019-12-20 06:26:56

问题


How would I copy a file from Remote desktop to a local machine using Excel VBA?


回答1:


Yes, you can copy files between different computers/servers in VBA. You didn't specify much so here is an example of coping from the local machine to a remote machine. Just change the parameters for the copy (or move) for the reverse.

In my example I'm accessing the admin share 'Z$' on the remote machine. You can specify any share name.

I tested this in Excel 2013.

Option Explicit 'always declare your vars!

Sub CopyFile()
    Dim FSO:           Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim strFile:       strFile = "c:\temp\RemoteCopyTest\mytestfile.txt"
    Dim strTargetPath: strTargetPath = "\\Server\Z$\"

    'verify the remote folder exists
    If FSO.FolderExists(strTargetPath) Then
        'verify the source file exists
        If FSO.FileExists(strFile) Then
            'use FSO.MoveFile <Source>,<Target> if you want to move instead of copy
            FSO.CopyFile strFile, strTargetPath
        Else
            MsgBox "ERROR:  Source FIle does not exist or is not accessible."
        End If
    Else
        MsgBox "ERROR:  Target Folder does not exist or is not accessible."
    End If
End Sub



回答2:


When you connect to your remote PC\ Server you have options to share resources ie if your using Remote Desktop Connection its Options\z Local Resources\ More\ Drives

You can pick your local c drive, which will now be available to you on remote machine... in Windows Explorer.

So when you run your Excel VBA you just have to move file from Remote to your local C drive listed on the remote machine (It may give it a new drive letter)

As DevilsAdvocate has said..its simply copying 1 file from here to there

BTW You can save your Remote Connection setup...so that its the same settings each time you connect to that PC/Server



来源:https://stackoverflow.com/questions/34451904/how-can-we-copy-a-one-file-from-remote-desktop-to-local-machine-using-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!