vba 如何将字符串变量的值插入到将出现在电子邮件正文中的某些文本中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34768068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:10:14  来源:igfitidea点击:

How do I insert the value of a String variable into some text that will end up in the body of an email?

vbaexcel-vbaemailvariablesexcel

提问by Baz

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 = "[email protected]"
    .CC = ""
    .BCC = ""
    .Subject = Subject
    .Body = strbody
    .Send
End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing

End Sub*

I really need to be able to insert the values of PartNumber and Qty in the middle of the String strbody.

我真的需要能够在 String strbody 的中间插入 PartNumber 和 Qty 的值。

回答by luke_t

strbody = "Hi guys," & vbNewLine & vbNewLine & _
          "Please can you issue the following:" & vbNewLine & vbNewLine & _
          "Part number: " & PartNumber & vbNewLine & _
          "Qty: " & Qty & vbNewLine & _
          "This is line 4"

Just include the PartNumberand Qtyvariable 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.

只需在创建电子邮件正文字符串的代码部分中包含PartNumberQty变量名称;请记住使用&运算符将字符串变量连接在一起。