2015-10-20 36 views
0

我正在用VBA邏輯掙扎着構建一個郵件正文,其中包含未定義的名稱數。應該在電子郵件正文中列出位於「名稱」列「D」表中的名稱。向郵件正文輸出數組的值

這應該把名字放在一個數組中,從列「D」的第2行開始。

Dim i As Integer 
Dim j As Integer 
Dim cellValue() As String 

i = 2 
j = 2 

Do Until IsEmpty(Cells(i,4)) 
    cellValue(i) = Cells(i, 4).Value 
    i = i + 1 
Loop 

坐落在郵件的正文:

"Names : " 
Do While j <= i 
    "Name " & cellValue(j) 
    j = j + 1 
Loop 

我不能夠在電子郵件中得到任何人的名字,還是我的電子郵件右側的另一部分。

+0

你有幾個問題。我建議搜索關於使用ReDim構建陣列的其他問題。另外,當試圖將你的名字插入到你的電子郵件正文中時,你對這個字符串沒有任何作用。每次我做完這些,我都需要爲電子郵件正文連接一個長字符串。另外,如果沒有字符串中的任何返回字符,你將把所有的名字都放在同一行,而不是列表中。顯示更多的代碼也會有所幫助...... :-) – nwhaught

回答

0

我會救你搜索的麻煩....

Dim i As Integer 
Dim j As Integer 
Dim cellValue() As String 

i = 2 
j = 2 

Do Until IsEmpty(Cells(i,4)) 
    ReDim Preserve cellValue(i) 
    cellValue(i) = Cells(i, 4).Value 
    i = i + 1 
Loop 

然後在電子郵件正文:

sBody = "Names : " 
Do While j <= i 
    sBody = sBody & vbNewLine & "Name " & cellValue(j) 
    j = j + 1 
Loop 

'assume you already have this, but for illustration purposes 
Set oApp = CreateObject("Outlook.Application") 
Set oMail = oApp.CreateItem(olMailItem) 

With oMail 
    .To = 'whomever 
    .Body = sBody 
End With 

使用.Body屬性不會允許非常好的格式。如果您需要,請查看.HTMLBody以及如何將HTML嵌入到字符串中,以便根據需要對其進行格式化。

0

nwhaught是正確的,你已經使用ReDim語句來重新分配存儲空間的數組變量。

試試這個:

Dim i As Integer 
Dim j As Integer 
Dim cellValue() 
Dim x : x = 0 

i = 2 
j = 2 

Do Until IsEmpty(Cells(i,4)) 
    ReDim Preserve cellValue(x) 
    cellValue(x) = Cells(i, 4).Value 
    x = x + 1 
    i = i + 1 
Loop 

而在你的電子郵件正文:

For j = LBound(cellValue) To UBound(cellValue) 
    "Name " & cellValue(j) 
Next 

有關使用ReDim更多信息,請thisthis鏈接。