这是一个更好的选择。
你的 9 行代码可以减少到 7 行
Dim destpath As String
Dim sourcepath As String
Dim filename As String
sourcepath = "\\spap097\VISIONFILES\Attachments"
destpath = "\\spap097\VISIONFILES\Attachments\Temp Folder for 35870 Visions Files"
filename = "test.docx"
FileCopy sourcepath & "\" & filename, destpath & "\" & filename
或进一步到 5 行
Dim destpath As String, sourcepath As String, filename As String
sourcepath = "\\spap097\VISIONFILES\Attachments"
destpath = "\\spap097\VISIONFILES\Attachments\Temp Folder for 35870 Visions Files"
filename = "test.docx"
FileCopy sourcepath & "\" & filename, destpath & "\" & filename
您可能还想在FILECOPY
此处查看 MSDN 文章
如果它死了,请从链接中引用。
编辑
解决您的问题
如果您是早期绑定,请设置参考Tools | Reference | Microsoft Scripting Runtime
。Dim fso As Scripting.FileSystemObject
如果你是后期绑定,Dim fso As Object
那么你不需要设置参考。
行中有错误fso.CopyFile(sourcepath & "\" & filename, destpath & "\" & filename)
您有括号()
Sub Sample()
'~~> Any of the below will work.
Dim fso As Scripting.FileSystemObject
'Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim destpath As String
Dim sourcepath As String
Dim filename As String
sourcepath = "\\spap097\VISIONFILES\Attachments"
destpath = "\\spap097\VISIONFILES\Attachments\Temp Folder for 35870 Visions Files"
filename = "test.docx"
' now do the copy
fso.CopyFile sourcepath & "\" & filename, destpath & "\" & filename
End Sub