附加名称更改的文件

时间:2014-02-10 13:00:50

标签: vba outlook-vba

我正在编写一个VBA查询,当您按下单词按钮时会自动发送电子邮件。我还希望它附加一个文件。然而问题是它需要附加的文件有一个名称,其中包含一个更改的元素(如report + weeknr,日期部分更改)

但是因为我还将weeknr包含在邮件主题中(例如“Subject:report + weeknr”),但我可以通过创建一个变量来自动附加文档,该变量是来自report + weeknr的结果。但它不起作用。谁知道我怎么能让这个工作?请参阅以下代码:

Sub Sendmessage()


Dim OutApp As Object
Dim OutMail As Object
Dim var1 As String
Dim sentto As Long

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


var1 = InputBox("Insert week")
'Line below is where it goes wrong. Var2 leads to C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt

var2 = "C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt"

    With OutMail
    .To = "marcvanderpeet@gmail.com; marc@gmail.com"
    .CC = ""
    .BCC = ""
    .Subject = "Report_" & var1
    .Body = "Text" 
    .Attachments.Add (var2)
    .Display
   End With

   End Sub

1 个答案:

答案 0 :(得分:0)

Sub Sendmessage()


Dim OutApp As Object
Dim OutMail As Object
Dim var1 As String
Dim sentto As Long

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


var1 = InputBox("Insert week")
'Line below is where it goes wrong. Var2 leads to C:\Documents and    Settings\aa471714\Desktop\SENS referentenrapportage - week " & var1 & ".ppt

var2 = "C:\Documents and Settings\aa471714\Desktop\SENS referentenrapportage - week " &  var1 & ".ppt"

With OutMail
.To = "marcvanderpeet@gmail.com; marc@gmail.com"
.CC = ""
.BCC = ""
.Subject = "Report_" & var1
.Body = "Text" 
.Attachments.Add (var2)
.Display
End With

End Sub