在 CDO 电子邮件的正文中打印数组

Print array in text body of CDO email

我已经成功地用数据加载了一个数组 myArray(30,1) 并且想在 CDO 的文本正文中打印数据 email.This 是工作正常的代码部分(它一直是几个月的服务),然后我尝试在文本正文中打印数组。你可以看到我只是传递变量 FullnameAmt:

 Set iMsg = CreateObject("CDO.Message")
                With iMsg
                    Set .Configuration = iConf


                    .To = "MC123@gmail.com"
                    .From = """ME Report Error"" <MC124@gmail.com>"
                    .Subject = "Current Period Monthly Report variance"
                    .TextBody = "Hello," & vbNewLine & vbNewLine & _
                                "Please review the variances to NAV in the following report:" & vbNewLine & vbNewLine & _
                                "Report Location:  " & FullName & vbNewLine & vbNewLine & _
                                "Variance amount:  " & Amt & vbNewLine
                    .Send
                End With
                Set iMsg = Nothing

现在,当我尝试循环遍历文本正文中的数组时,出现错误:

 Set iMsg = CreateObject("CDO.Message")
                With iMsg
                    Set .Configuration = iConf


                    .To = "MC123@gmail.com"
                    .From = """ME Report Error"" <MC124@gmail.com>"
                    .Subject = "Current Period Monthly Report variance"
                    .TextBody = "Hello," & vbNewLine & vbNewLine & _
          ERROR  ------------ >  & For x = LBound(myArray, 1) To UBound(myArray, 1)
                                  For y = LBound(myArray, 2) To UBound(myArray, 2)
                                    ThisWorkbook.Sheets("Sheet1").Cells(x + 1, y + 1) = myArray(x, y)
                                Next y
                            Next x
                    .Send
                End With
                Set iMsg = Nothing

我知道循环内部的语法不正确。我用它把数组放到电子表格中(效果很好)。但我希望稍作修改就可以将其放入电子邮件正文中,但它似乎根本不喜欢我将代码放在那里。我收到的错误是

"Compile Error: Expected: Expression"

错误落在.TextBody行下方第一行的&处。我什至不相信这个目标是可能的。我可能必须在电子表格中打印数组并将其作为附件通过电子邮件发送,但我更愿意将其打印在文本正文中。

非常感谢!

你有两个连续的符号。

... & _
& ...

物理行解析器删除了下划线,因此逻辑行解析器看到了

... & & ...

& concatinates strings

_ continues a logical line on the next physical line

放在电子邮件正文之外:

For x = LBound(myArray, 1) To UBound(myArray, 1)
      For y = LBound(myArray, 2) To UBound(myArray, 2)
        ThisWorkbook.Sheets("Sheet1").Cells(x + 1, y + 1) = myArray(x, y)
    Next y
Next x

将范围复制到剪贴板:

ThisWorkbook.Sheets("Sheet1").Range(ThisWorkbook.Sheets("Sheet1").Cells(1+LBound(myArray, 1),1+LBound(myArray, 2)),ThisWorkbook.Sheets("Sheet1").Cells(1+UBound(myArray, 1),1+UBound(myArray, 2))).Copy

然后您可以从剪贴板中获取数据:

"Hello," & vbNewLine & vbNewLine & ClipBoard_GetData()

但首先您需要将函数声明放在代码的顶部,在任何函数或子函数之外:

Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _ 
   As Long 
Declare Function CloseClipboard Lib "User32" () As Long 
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As _ 
   Long) As Long 
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags&, ByVal _ 
   dwBytes As Long) As Long 
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _ 
   As Long 
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _ 
   As Long 
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) _ 
   As Long 
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _ 
   ByVal lpString2 As Any) As Long 

Public Const GHND = &H42 
Public Const CF_TEXT = 1 
Public Const MAXSIZE = 4096

Function ClipBoard_GetData() As String
   Dim hClipMemory As Long 
   Dim lpClipMemory As Long 
   Dim MyString As String 
   Dim RetVal As Long 

   If OpenClipboard(0&) = 0 Then 
      MsgBox "Cannot open Clipboard. Another app. may have it open" 
      Exit Function 
   End If 

   ' Obtain the handle to the global memory 
   ' block that is referencing the text. 
   hClipMemory = GetClipboardData(CF_TEXT) 
   If IsNull(hClipMemory) Then 
      MsgBox "Could not allocate memory" 
      GoTo OutOfHere 
   End If 

   ' Lock Clipboard memory so we can reference 
   ' the actual data string. 
   lpClipMemory = GlobalLock(hClipMemory) 

   If Not IsNull(lpClipMemory) Then 
      MyString = Space$(MAXSIZE) 
      RetVal = lstrcpy(MyString, lpClipMemory) 
      RetVal = GlobalUnlock(hClipMemory) 

      ' Peel off the null terminating character. 
      MyString = Mid(MyString, 1, InStr(1, MyString, Chr$(0), 0) - 1) 
   Else 
      MsgBox "Could not lock memory to copy string from." 
   End If 

OutOfHere: 

   RetVal = CloseClipboard() 
   ClipBoard_GetData = MyString

End Function

要在带有 vba 的字符串中插入双引号 ("),您需要使用 4 个双引号:

MyName = "Fred " & """" & "Flinstone" & """"

字符串值为 [Fred "Flinstone"]