2014-02-10 189 views
0

我在写一个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 = "[email protected]; [email protected]" 
    .CC = "" 
    .BCC = "" 
    .Subject = "Report_" & var1 
    .Body = "Text" 
    .Attachments.Add (var2) 
    .Display 
    End With 

    End Sub 
+0

不,如果你把路径+文件名到VAR2为字符串它的工作 - 不使用VAR1 - 测试的目的? – Max

+0

你遇到的问题究竟是什么?你有错误信息还是只有没有附件? – Max

+0

@最大,是的。但我已经解决了它。必须将var1定义为long而不是string。现在它可以工作。无论如何,感谢您的意见! – user181796

回答

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 = "[email protected]; [email protected]" 
.CC = "" 
.BCC = "" 
.Subject = "Report_" & var1 
.Body = "Text" 
.Attachments.Add (var2) 
.Display 
End With 

End Sub