如何将String变量的值插入将最终出现在电子邮件正文中的某些文本中? [英] How do I insert the value of a String variable into some text that will end up in the body of an email?
问题描述
我有一个电子表格,该电子表格将用于跟踪对另一个部门的请求.我想要一个宏来生成并发送一封电子邮件,其中包含一些预定义的文本和一些变量的值.我已经有一些可以扫描相关单元格并存储其值的工作代码.
I have a spreadsheet that is going to be used to track requests made to another department. I would like a Macro to generate and send an email the contains some predefined text and the value of some variables. I already have some working code that scans the relevant cells and stores their values.
我可以生成电子邮件,可以打印文本行,包括在主题中插入一个变量,但是我似乎无法在电子邮件正文的中间插入任何变量的值.我有以下内容:
I can generate the email, I can print lines of text including inserting one variable into the subject, but I can't seem to insert the value of any of the variables in the middle of the body of the email. I have the following:
Sub IssueRequest()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
' Selecting the last entry in column "B"
Range("B7").Select
ActiveCell.Offset(1, 0).Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
ActiveCell.Offset(-1, 0).Select
' Collect the unique value to insert into the subject field
Dim Subject As String
Subject = ActiveCell.Value
ActiveCell.Offset(0, 2).Select
' Collect the Part Number for use in the body of the email
Dim PartNumber As String
PartNumber = ActiveCell.Value
' Collect the Quantity for use in the body of the email
ActiveCell.Offset(0, 1).Select
Dim Qty As String
Qty = ActiveCell.Value
'Create the email
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Hi guys," & vbNewLine & vbNewLine & _
"Please can you issue the following:" & vbNewLine & vbNewLine & _
"Part number: " & vbNewLine & _
"Qty: " & vbNewLine & _
"This is line 4"
On Error Resume Next
With OutMail
.To = "xxxxx.xxxxx@xxxxx-xxxxx.com"
.CC = ""
.BCC = ""
.Subject = Subject
.Body = strbody
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub*
我真的需要能够在String主体的中间插入PartNumber和Qty的值.
I really need to be able to insert the values of PartNumber and Qty in the middle of the String strbody.
推荐答案
strbody = "Hi guys," & vbNewLine & vbNewLine & _
"Please can you issue the following:" & vbNewLine & vbNewLine & _
"Part number: " & PartNumber & vbNewLine & _
"Qty: " & Qty & vbNewLine & _
"This is line 4"
只需在要创建电子邮件正文字符串的代码部分中包含 PartNumber
和 Qty
变量名;请记住使用&
运算符将字符串变量连接在一起.
Just include the PartNumber
and Qty
variable names inside the part of code where you're creating the e-mail body string; remember to use the &
operator to join string variables together.
这篇关于如何将String变量的值插入将最终出现在电子邮件正文中的某些文本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!