Excel VBA复制粘贴文件夹中的所有文件
我的宏自2个月以来运行良好,但现在我需要一些帮助来解决另一个问题。 我们正在我们的服务器上运行一个控制器,它向我们的客户发送带有附件pdf的邮件。现在,此控制器和我的宏有时同时运行,当我的宏创建PDF时,控制器希望发送它,但无法发送,因为它已在创建中。 现在我认为宏可以将pdf保存到另一个文件夹中,然后将所有文件复制粘贴到正确的文件夹中进行发送 我的代码是:Excel VBA复制粘贴文件夹中的所有文件,excel,vba,Excel,Vba,我的宏自2个月以来运行良好,但现在我需要一些帮助来解决另一个问题。 我们正在我们的服务器上运行一个控制器,它向我们的客户发送带有附件pdf的邮件。现在,此控制器和我的宏有时同时运行,当我的宏创建PDF时,控制器希望发送它,但无法发送,因为它已在创建中。 现在我认为宏可以将pdf保存到另一个文件夹中,然后将所有文件复制粘贴到正确的文件夹中进行发送 我的代码是: Function Copy() Dim MyFile2 As Sting Dim myPath2 As String, my
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
但如果我运行它,则会出现一个错误:无法定义编译用户定义类型的错误。
如下所示:。
我一直在谷歌上搜索,但不知道如何设置或导入一些东西来解决这个问题。下面的子部分将把所有文件从源文件夹复制到目标文件夹
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
您的代码无法工作,因为正如@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
函数复制()
将MyFile2设置为字符串
将myPath2设置为字符串,将myPath3设置为字符串
myPath2=“L:\Host\u Export\Pdf Kundenmail\Test”
myPath3=“L:\Host\u Export\Pdf Kundenmail\”
MyFile2=Dir(myPath2&“****”)
在MyFile2“”时执行此操作
文件复制myPath2和MyFile2、myPath3和MyFile2
myFile2=Dir
环
端函数
myfile2=dir肯定是您的意思吗?错误显示在代码的第三行Dim myfile2的字符串中。由于错误,我无法尝试“执行”部分,我将切换它。哦,是的,这是另一个错误,谢谢Dim MyFile2作为字符串
->Dim MyFile2作为字符串
。使用Option Explicit
避免这种MyStypes这是一个非常愚蠢的错误类型,我将设置此选项并更正Sting
此选项更简单,将代替我的代码使用,谢谢!