问题
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