Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将生成的PDF移动到网络驱动器上的新文件夹_Vba_Excel - Fatal编程技术网

Vba 将生成的PDF移动到网络驱动器上的新文件夹

Vba 将生成的PDF移动到网络驱动器上的新文件夹,vba,excel,Vba,Excel,我有一个宏,可以执行并创建PDF文件。每次运行宏时,都会生成PDF。我想将报告的最后一个版本(每天运行三次)移动到名为“过去的报告”的文件夹中。我一直在玩下面的脚本,但它不适合我。活动报告文件夹仅包含最近创建的PDF 有人能帮忙吗?如果需要,很高兴添加更多信息 Public Sub transferFile() On Error GoTo nextIt Set fileSystemObject = CreateObject("Scripting.FileSystemObject") PDFPa

我有一个宏,可以执行并创建PDF文件。每次运行宏时,都会生成PDF。我想将报告的最后一个版本(每天运行三次)移动到名为“过去的报告”的文件夹中。我一直在玩下面的脚本,但它不适合我。活动报告文件夹仅包含最近创建的PDF

有人能帮忙吗?如果需要,很高兴添加更多信息

Public Sub transferFile()
On Error GoTo nextIt

Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
PDFPath = "D:\####\Pinging Program\Active Report\"
pastPDFPath = "D:\####\Pinging Program\Past Reports" 
sSourceFile = PDFPath & Dir(PDFPath & "*.pdf")
sDestinationFile = "D:\####\Pinging Program\Past Reports"  

'move file
If Dir(sSourceFile) <> "" Then
    fileSystemObject.moveFile sSourceFile, sDestinationFile
End If

nextIt:

End Sub
公共子传输文件()
错误转到下一个
设置fileSystemObject=CreateObject(“Scripting.fileSystemObject”)
PDFPath=“D:\\\\\\\\\\\\ping程序\活动报告”
pastPDFPath=“D:\\\\\\\\\\\\ping程序\过去的报告”
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
sDestinationFile=“D:\\\\\\\\\\\\ping程序\过去的报告”
'移动文件
如果目录(sSourceFile)“,则
fileSystemObject.moveFile文件源文件,sdestinitionfile
如果结束
下一步:
端接头

您的目标文件夹缺少最后一个斜杠。此外,作为对未来的建议,如果您没有像Victor建议的那样绕过错误处理,那么错误将更容易理解。您的代码如下所示:

Public Sub transferFile()


Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
PDFPath = "C:\test\Active Report\"
pastPDFPath = "C:\test\Past Reports"
sSourceFile = PDFPath & Dir(PDFPath & "*.pdf")
sDestinationFile = "C:\test\Past Reports\"

'move file
If Dir(sSourceFile) <> "" Then
    fileSystemObject.moveFile sSourceFile, sDestinationFile
End If

End Sub
公共子传输文件()
设置fileSystemObject=CreateObject(“Scripting.fileSystemObject”)
PDFPath=“C:\test\Active Report\”
pastPDFPath=“C:\test\pass Reports”
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
sDestinationFile=“C:\test\pass Reports\”
'移动文件
如果目录(sSourceFile)“,则
fileSystemObject.moveFile文件源文件,sdestinitionfile
如果结束
端接头

我已经测试了它,它的工作如预期。请注意,

您的目标文件夹缺少最后一个斜杠。此外,作为对未来的建议,如果您没有像Victor建议的那样绕过错误处理,那么错误将更容易理解。您的代码如下所示:

Public Sub transferFile()


Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
PDFPath = "C:\test\Active Report\"
pastPDFPath = "C:\test\Past Reports"
sSourceFile = PDFPath & Dir(PDFPath & "*.pdf")
sDestinationFile = "C:\test\Past Reports\"

'move file
If Dir(sSourceFile) <> "" Then
    fileSystemObject.moveFile sSourceFile, sDestinationFile
End If

End Sub
公共子传输文件()
设置fileSystemObject=CreateObject(“Scripting.fileSystemObject”)
PDFPath=“C:\test\Active Report\”
pastPDFPath=“C:\test\pass Reports”
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
sDestinationFile=“C:\test\pass Reports\”
'移动文件
如果目录(sSourceFile)“,则
fileSystemObject.moveFile文件源文件,sdestinitionfile
如果结束
端接头

我已经测试了它,它的工作如预期。关于,

您是否可以删除错误转到下一个
并检查错误?使用
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
您希望得到什么?因为如果您打印出
sSourceFile
它只会返回
“D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ping程序\活动报告\”
@VictorMoraes-
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
会将
sSourceFile
设置为名称(包括路径)在
PDFPath
目录中找到的第一个
.pdf
文件。(如果该目录中没有
.pdf
文件,那么它将只返回与
PDFPath
相同的值,但如果该目录中至少有一个
.pdf
文件,它将返回预期的值。)@YowE3K正如nbayly指出的那样,我在测试中也丢失了后斜杠,因此我的结果是错误的。我今天刚学到了一些新东西,所以谢谢大家:)哎呀,谢谢大家
sSourceFile
不正确,缺少“`”。谢谢大家。对于错误处理,您如何在构建过程中将它们放在一起?您是否将它们留到开发过程结束时?您是否可以删除错误转到下一步的
并检查错误?使用
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
您希望得到什么?因为如果您打印出
sSourceFile
它只会返回
“D:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ping程序\活动报告\”
@VictorMoraes-
sSourceFile=PDFPath&Dir(PDFPath&“*.pdf”)
会将
sSourceFile
设置为名称(包括路径)在
PDFPath
目录中找到的第一个
.pdf
文件。(如果该目录中没有
.pdf
文件,那么它将只返回与
PDFPath
相同的值,但如果该目录中至少有一个
.pdf
文件,它将返回预期的值。)@YowE3K正如nbayly指出的那样,我在测试中也丢失了后斜杠,因此我的结果是错误的。我今天刚学到了一些新东西,所以谢谢大家:)哎呀,谢谢大家
sSourceFile
不正确,缺少“`”。谢谢大家。对于错误处理,您如何在构建过程中将它们放在一起?您是否将它们留到开发过程的末尾?