2016-11-10 69 views
0

我的VBA知識極其有限。 我有一個領導聯繫人的Excel電子表格。我想設置一個下拉列表,向我選擇的特定人員發送電子郵件,然後返回電子郵件正文中的聯繫人信息。我不知道如何讓電子郵件自動填充,現在,彈出的電子郵件在聯繫人信息的正文中具有「真實」,而不是返回單元格中的文本值。如何以及在哪裏我添加代碼來解決這個問題?從excel電子表格中制​​作主題分配電子郵件

Sub DropDown7_Change() 

    Dim answer As String 

    answer = MsgBox("Are you sure you want to assign this lead?", _ 
     vbYesNo, "Send Email") 
    ' Above code informs the user that an automated email will be sent 

    'Code uses the users answer to either carryout the generated email process or to not save the changes. 
    If answer = vbNo Then Cancel = True 
    If Cancel = True Then Exit Sub 

    If answer = vbYes Then 
     'Connects to outlook and retrieves information needed to create and send the email. 
     Set OutlookApp = CreateObject("Outlook.Application") 
     Set OlObjects = OutlookApp.GetNamespace("MAPI") 
     Set newmsg = OutlookApp.CreateItem(olMailItem) 

     'Contains the email address of the person receiving the email. 
     newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email 
     newmsg.Body = "Hello," & vbNewLine & _ 
      "You have been assigned a lead. Please follow up with the contact" & vbNewLine & _ 
      ActiveCell.Offset(0, 3).Range("K5").Select 
      ActiveCell.Offset(0, 6).Range("K5").Select 
      ActiveCell.Offset(0, 7).Range("K5").Select 

     'Above code has the body of the automated email 
     newmsg.Display 
    End If 

End Sub ' End of function 

回答

0

如果你試圖讓那些OffsetRange("K5")值,那麼你需要使用Offset.Value,這樣Range("K5").Offset(0, 3).Value,這將獲得價值3列單元格「K5」的權利。

下面的代碼,將從3個細胞列偏移細胞「K5」給你添加值郵件正文:

Sub DropDown7_Change() 

    Dim answer As String 

    answer = MsgBox("Are you sure you want to assign this lead?", _ 
     vbYesNo, "Send Email") 
    ' Above code informs the user that an automated email will be sent 

    'Code uses the users answer to either carryout the generated email process or to not save the changes. 
    If answer = vbNo Then 
     Exit Sub 
    Else 
     If answer = vbYes Then 

      'Connects to outlook and retrieves information needed to create and send the email. 
      Set OutlookApp = CreateObject("Outlook.Application") 
      Set OlObjects = OutlookApp.GetNamespace("MAPI") 
      Set newmsg = OutlookApp.CreateItem(olMailItem) 

      'Contains the email address of the person receiving the email. 
      newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email 
      newmsg.body = "Hello," & vbNewLine & _ 
       "You have been assigned a lead. Please follow up with the contact" & vbNewLine & _ 
       Range("K5").Offset(0, 3).Value & vbNewLine & _ 
       Range("K5").Offset(0, 6).Value & vbNewLine & _ 
       Range("K5").Offset(0, 7).Value & vbNewLine      

      'Above code has the body of the automated email 
      newmsg.Display 
     End If 
    End If 

End Sub