2013-03-28 56 views
0

我有一個名爲「Home Audio for Planning(28-3-2013) 」的excel文件,日期會一直保持不變,但文本將保持不變。 ?文件到Outlook使用不同的文件名添加附件

Sub Test() 

    Dim OutApp As Object 
    Dim OutMail As Object 
    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 
    With OutMail 
     .To = "" 
     .CC = "" 
     .BCC = "" 
     .Subject = "This is the Subject line" 
     .Body = "Hello World!" 



     .Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning (28-3-2013).xlsx") 
     .Display 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

    With Application 
     .ScreenUpdating = True 
     .EnableEvents = True 
    End With 

End Sub 

回答

6

請嘗試以下代碼:strLocation將會動態生成。你可以將這個變量傳遞給你的附件。生成的文件名會像家用音響爲Planning_28-03-2013.xlsx

Sub Test() 
    Dim strLocation As String 

    Dim OutApp As Object 
    Dim OutMail As Object 
    Set OutApp = CreateObject("Outlook.Application") 
    Set OutMail = OutApp.CreateItem(0) 
    With OutMail 
     .To = "" 
     .CC = "" 
     .BCC = "" 
     .Subject = "This is the Subject line" 
     .Body = "Hello World!" 

     strLocation = "C:\Users\Desktop\Today\Home Audio for Planning" & Format(Now(), "_DD-MM-YYYY") & ".xlsx" 
     .Attachments.Add (strLocation) 
     .Display 
    End With 
    On Error GoTo 0 

    Set OutMail = Nothing 
    Set OutApp = Nothing 

    With Application 
     .ScreenUpdating = True 
     .EnableEvents = True 
    End With 

End Sub 
0

你有沒有嘗試改變attachemnt名稱動態對於離;

.Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning (" + timeVariable + ").xlsx") 

,你可以設置時間變量之前以所需的格式相匹配的日期的日期。

乾杯

+0

我無法弄清楚如何編寫此代碼 – 2013-03-28 06:30:52

+0

運行時錯誤。路徑不存在!打破我的頭腦。爲了以這種格式保存文件,我給今天的代碼= Format(Now(),「dd-mm-yy」)...所以當我想附加同一個文件時,我得到這個錯誤 – 2013-03-28 07:51:59

+0

@AcidgemcutterAgc路徑參數應該包含本地用戶名,即「C:\ Users \ John Smith \ Desktop \」 – JimmyPena 2013-04-01 20:48:21

0

簡單,

.Attachments.Add ("C:\Users\Desktop\Today\Home Audio for Planning (" & FORMAT(DATE,DD-MM-YYYY)")