2016-01-13 2 views
0

我有一個電子表格將用於跟蹤對另一個部門的請求。我想要一個宏來生成併發送一個包含一些預定義文本和一些變量值的電子郵件。我已經有了一些可以掃描相關單元並存儲其值的工作代碼。如何將一個字符串變量的值插入到一些文本中,這些文本最終會出現在電子郵件的正文中?

我可以生成電子郵件,我可以打印文本行,包括在主題中插入一個變量,但我似乎無法在電子郵件正文中插入任何變量的值。我有以下幾點:

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* 

我真正需要的是能夠插入在String strbody的中間部分號碼和數量的值。

回答

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

就包括在這裏你正在創建的電子郵件正文串碼部內的PartNumberQty變量名;請記住使用&運算符將字符串變量連接在一起。

相關問題