2016-11-16 108 views
0

我有一個執行並創建PDF文件的宏。每次運行宏時,都會生成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 
+0

你可以刪除'On Error GoTo nextIt'並檢查錯誤? –

+1

你想用'sSourceFile = PDFPath&Dir(PDFPath&「* .pdf」)''得到什麼?因爲如果你打印出'sSourceFile',它只會返回'「D:\ #### \ Pinging Program \ Active Report \」' –

+2

@VictorMoraes - 'sSourceFile = PDFPath&Dir(PDFPath&「* .pdf」) '將'sSourceFile'設置爲'PDFPath'目錄中第一個.pdf文件的名稱(包括路徑)。 (如果該目錄中有** no **'.pdf'文件,那麼它將只返回與「PDFPath」相同的值,但如果該目錄中至少有一個.pdf文件,它將返回預計。) – YowE3K

回答

2

您的目標文件夾缺少最終的斜槓。此外,作爲未來的建議,如果您不像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 

我測試過了,它按預期工作。問候,

相關問題