vba VBA复制粘贴文件夹中的所有文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42118824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA copy paste all files in a folder
提问by Patrick
My macro is running well since 2 months but now I need some help for another issue. We are running an controller on our server which is sending mails to our customer with attached pdf. Now this controller and my macro are running sometimes at the same time and when my macro is creating pdfs the controller wants to send it but is not able to do so because its already in creation. Now I thought the macro could save pdf into another folder and after that it will copy paste all files into the right folder for sending.
我的宏运行 2 个月以来运行良好,但现在我需要一些帮助来解决另一个问题。我们在我们的服务器上运行一个控制器,它向我们的客户发送带有附件 pdf 的邮件。现在这个控制器和我的宏有时同时运行,当我的宏创建 pdf 时,控制器想要发送它但无法发送,因为它已经在创建中。现在我认为宏可以将 pdf 保存到另一个文件夹中,然后它将所有文件复制粘贴到正确的文件夹中进行发送。
My code is this:
我的代码是这样的:
Function Copy()
Dim MyFile2 As Sting
Dim myPath2 As String, myPath3 As String
myPath2 = "L:\Host_Export\Pdf-Kundenmail\Test\"
myPath3 = "L:\Host_Export\Pdf-Kundenmail\"
MyFile2 = Dir(myPath2 & "*.*")
Do
If MyFile2 = "" Then Exit Do
FileCopy myPath2 & MyFile2, myPath3 & MyFile2
End If
myFile2 = Dir
Loop
End Function
But if I run it there is an error: error on compilation userdefined typ could not be defined.
like this: https://i0.wp.com/www.port135.com/wp-content/uploads/2012/08/error1-1.png.
I alredy googled but don't get it how to set up or import something to fix this issue.
但是如果我运行它会出现错误:error on compilation userdefined typ could not be defined.
像这样:https://i0.wp.com/www.port135.com/wp-content/uploads/2012/08/error1-1.png。我已经用谷歌搜索过,但不知道如何设置或导入一些东西来解决这个问题。
采纳答案by User632716
Your code won't work because as @user3598756 said, you spelled string incorrectly. To improve your form though, use a do while loop to combine the if and do statements like so:
您的代码将不起作用,因为正如@user3598756 所说,您拼写了错误的字符串。但是,为了改进您的表单,请使用 do while 循环来组合 if 和 do 语句,如下所示:
Function Copy()
Dim MyFile2 As String
Dim myPath2 As String, myPath3 As String
myPath2 = "L:\Host_Export\Pdf-Kundenmail\Test\"
myPath3 = "L:\Host_Export\Pdf-Kundenmail\"
MyFile2 = Dir(myPath2 & "*.*")
Do while MyFile2 <> ""
FileCopy myPath2 & MyFile2, myPath3 & MyFile2
myFile2 = Dir
Loop
End Function
回答by Harun24HR
Following sub will copy all files from source folder to destination folder.
以下 sub 会将所有文件从源文件夹复制到目标文件夹。
Sub AllFiles()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
FromPath = "C:\Users\Alam\Music\Awlad Hossain" 'Souece Folder
ToPath = "C:\MyExcelFiles" 'Destination folder
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub