2017-04-14 77 views
1

我試圖將附加到電子郵件的日常系統生成報告保存到文件夾。將Outlook附件保存到文件夾並用日期重命名該文件

然後,將附件文件名附加日期(文件中的修改日期)。我能夠將文件保存到文件夾。但是,重命名的部分似乎不適合我。

有人可以幫助爲什麼重命名的一塊不工作?非常感謝!

Public Sub saveAttachtoBIFolder(itm As Outlook.MailItem) 
    Dim objAtt As Outlook.Attachment 
    Dim saveFolder As String 
    Dim fso As Object 
    Dim oldName As Object 

    Dim file As String 
    Dim DateFormat As String 
    Dim newName As Object 


    saveFolder = "C:\BI Reports" 


    Set fso = CreateObject("Scripting.FileSystemObject") 
    On Error Resume Next 

    For Each objAtt In itm.Attachments 
     file = saveFolder & "\" & objAtt.DisplayName 
     objAtt.SaveAsFile file 
     Debug.Print "file="; file ' the full file path printed on immediate screen 

     Set oldName = fso.GetFile(file) ' issue seems to start from here 
     DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ") 
     newName = DateFormat & objAtt.DisplayName 
     oldName.Name = newName 

     Debug.Print "DateFormat="; DateFormat 'the date format printed on the immediate screen 
     Set objAtt = Nothing 
    Next 

    Set fso = Nothing 
End Sub 
+0

上的錯誤繼續下一步應該只用於具有繞過錯誤的特定目的,然後使用On Error GoTo 0關閉。這裏似乎沒有用處。在錯誤恢復時刪除以查看錯誤,以便您可以調試代碼。 – niton

回答

1

newName需求是stringNOT Object如此暗淡newName As String還我會分配給objAtt.DisplayNamestring variable

見例

Set FSO = CreateObject("Scripting.FileSystemObject") 
For Each objAtt In itm.Attachments 
    File = saveFolder & "\" & objAtt.DisplayName 
    objAtt.SaveAsFile File 
    Debug.Print File ' the full file path printed on immediate screen 

    Set oldName = FSO.GetFile(File) ' issue seems to start from here 
    Debug.Print oldName 

    Dim newName As String 
    Dim AtmtName As String 

    AtmtName = objAtt.DisplayName 
    Debug.Print AtmtName 


    DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ") 
    Debug.Print DateFormat 

    newName = DateFormat & " " & AtmtName 
    oldName.Name = newName 
    Debug.Print newName 'the date format printed on the immediate screen 
Next 
+1

有沒有辦法在文件末尾追加日期?反轉AtmtName&「_」&DateFormat不會。我得到filename.xlsx_2017-04-15。相反,我怎麼能得到filename_2017-04-15.xlsx – NinjaWarrior

+0

@NinjaWarrior這是一個很好的問題不要介意發佈一個新的問題,我會盡力幫助你。 – 0m3r

相關問題