即使参考scrrun.dll也无法识别FileSystemObject

时间:2013-10-22 16:53:38

标签: excel excel-vba vba

这应该很容易但我甚至无法编译。我使用了“regsvr32 scrrun.dll”并重新启动无济于事。这是代码:

Dim fso As Scripting.FileSystemObject
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)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这是一个更好的选择。

您的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 HERE

上查看MSDN文章

如果它死了,请从链接中引用。

enter image description here

修改

解决您的问题

如果您是早期绑定,请将引用设置为Tools | Reference | Microsoft Scripting RuntimeDim 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