2017-04-14 210 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