2017-08-04 60 views
1

我被这个问题困住了,我已经尝试了很多东西,但没有工作。 我想发送电子邮件与这个宏,我可以发送电子邮件,但电子邮件正文不包括称为“许可证”的变量值。我有columnC上的许可证列表,columnG上的邮件列表E和剩余列表上的列表。当我运行这个宏电子邮件是完美的,但许可证不与身体附加,我想发送每个许可证到电子邮件下一行。任何帮助?VBA如何将变量插入到电子邮件的正文中

Sub SendReminderMail() 
Dim OutLookApp As Object 
Dim OutLookMailItem As Object 
Dim iCounter As Integer 
Dim MailDest As String 

Cells(iCounter & "C").Select 
Dim permit As String 
permit = Cells(iCounter & "C").Value 

Set OutLookApp = CreateObject("Outlook.application") 
Set OutLookMailItem = OutLookApp.CreateItem(0) 
With OutLookMailItem 
    MailDest = "" 
     For iCounter = 2 To WorksheetFunction.CountA(Columns(5)) 
      If MailDest = "" And Cells(iCounter, 5).Offset(0, 2) = "Send Reminder" Then 
      MailDest = Cells(iCounter, 5).Value 
       ElseIf MailDest <> "" And Cells(iCounter, 5).Offset(0, 2) = "Send Reminder" Then 
      MailDest = MailDest & ";" & Cells(iCounter, 5).Value 
      End If 
Next iCounter 
    .BCC = MailDest 
    .Subject = "Soon to Expire" 


    .Body = "Reminder: Your Permit is about to expire Permit#" & permit 
'here is the part where i want my permit variables to attach 

    .Send 
End With 
Set OutLookMailItem = Nothing 
Set OutLookApp = Nothing 
End Sub 
+0

代码正确,在'permit'设置好后立即设置一个断点,并检查它的值。您可能有错误的工作表,因为您没有完全限定您的单元格引用。 – braX

+0

事实上,你根本不需要'.Select' ...只是更新这一行......'permit = Cells(iCounter,“C”)。Value' – braX

+0

@braX,非常感谢你的帮助,我刚刚尝试过,但它显示了另一个错误。运行时错误'287':应用程序定义或对象定义的错误,错误引用'.Send'行。任何想法? –

回答

0

使用

permit = Cells(iCounter, "C").Value

permit = Range("C" & iCounter).Value

看到细微的差别?

+0

我刚刚那样做,并修复了错误。现在它根本不发送电子邮件。 –

+0

这是一个完全独立的问题。 https://social.msdn.microsoft.com/Forums/office/en-US/ecce7b2a-8e4c-4e2e-b2e4-ad95f006d808/runtime-error-287-applicationdefined-or-objectdefined-error?forum=accessdev – braX

相关问题