我有一个.mdb数据库,正在我的宏中查询它。我把查询结果放在一个数组中。我想在Outlook正文中显示它们,但这给了我一个错误:
下标超出范围
Public Sub sendNotifForm4()
Dim userArray() As Variant
Dim i As Integer
Dim x As Integer
Dim objOutlook As Object
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = OpenDatabase("C:/Users/FTK1187/Desktop/eArchiveMaster.mdb", False, False, ";")
Set rs = db.OpenRecordset(Name:="SELECT userName FROM userTable WHERE flag = 'NO'")
rs.MoveFirst
Do While Not rs.EOF
userArray = rs.GetRows
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
x = (UBound(userArray, 1) - LBound(userArray, 1) + 1)
'On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application") ' Determine if Outlook is open
If Err <> 0 Then 'If Not open it
Call Shell(SysCmd(acSysCmdAccessDir) & "OUTLOOK.EXE")
End If
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
objOutlookMsg.Subject = " E - Archiving User Account Approvements "
objOutlookMsg.Body = objOutlookMsg.Body & "Dear Admin," & _
vbNewLine & vbNewLine & "Please approve this user accounts" & _
vbNewLine & vbNewLine
For i = 0 To UBound(userArray)
objOutlookMsg.Body = objOutlookMsg.Body & "User Name:"
objOutlookMsg.Body = objOutlookMsg.Body & userArray(i)
objOutlookMsg.Body = objOutlookMsg.Body & "Approval : NO"
Next i
objOutlookMsg.Body = objOutlookMsg.Body & vbNewLine & vbNewLine & "Best Regards"
'admin table loop here:
Set objOutlookRecip = objOutlookMsg.Recipients.Add("Mustafa.Demir@pw.utc.com") '
objOutlookRecip.Type = olTo
'Call sendMail(objOutlookMsg, objOutlookRecip, "Repair Engineering", olTo)
objOutlookMsg.Send
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
End Sub
我尝试使用objOutlookMsg.HTMLBody
,但无法正确运行。您能帮我解决这个问题吗?
答案 0 :(得分:1)
.getrows
'。getRows函数返回一个二维数组(0,0), '尝试使用变量检查器查看数组UserArray发生了什么
For i = 0 To UBound(userArray)
objOutlookMsg.Body = objOutlookMsg.Body & "User Name:"
objOutlookMsg.Body = objOutlookMsg.Body & userArray(0, i)
objOutlookMsg.Body = objOutlookMsg.Body & "Approval : NO"
Next i