【问题标题】:How to Print an Array in the body of an outlook mail?如何在 Outlook 邮件正文中打印数组?
【发布时间】:2019-04-11 20:03:49
【问题描述】:

我正在为我的公司执行一个项目。我创建了一个数组并在其中存储了一些数据。

我需要在电子邮件正文中打印或粘贴数组。

你可以注意到下面的对象 .Body = "Hello, " & vbNewLine & vbNewLine & "你能确认下面的净额吗?" & T(p + 1, 8)

数组 T(p+1,8) 没有出现在邮件正文中。

代码下方:


出现错误继续下一步 使用 OutMail

.to = 地址

.CC = "otceequityderivativesettlement@xxxxxx.com"

.BCC = ""

.Subject = "确认起息日的金额" & " " & VALUEDATE & " " & CTPY

.Body = "你好," & vbNewLine & vbNewLine & "你能确认下面的净额吗?" & T(p + 1, 8)

.Attachments.Add "R:\Fmp\Fmp10\All\POLE DERIVES ACTIONS\SSI xxxxx\SSI xxxx.pdf"

.显示

End With
On Error GoTo 0

Set OutMail = Nothing

【问题讨论】:

  • 主要错误是滥用On Error Resume Nextstackoverflow.com/questions/31753201/… 删除它并修复之前绕过的任何错误。输入足够的代码,以便潜在的受访者可以立即复制和运行。 stackoverflow.com/help/mcve提供数组样本。
  • 代码很长而且重复。每次我在论坛上粘贴代码时,人们都会感到失望。反正我会做/吹,代码:
  • 尝试将代码缩减为可以复制和运行的内容,如stackoverflow.com/help/mcve提供的链接中所述,“尽可能少地使用仍然会产生相同问题的代码”。除非使用得当,否则尤其要删除 On Error Resume Next。

标签: arrays vba email outlook


【解决方案1】:
   Sub Macro()


Dim i As Variant

Dim l As Long

Dim p As Long

Dim mySIay() As Variant

Dim DataRange As Range

Dim cell As Range

Dim x As Long

Dim CSico As Long

Dim CTradeID As Long

Dim CBusinessEvent As Long

Dim CNetAmount As Long

Dim CTradeDate As Long

Dim CPaymentDate As Long

Dim CMaturity As Long

Dim CNominal As Long

Dim Label As Variant

Dim ra As Range

Dim T() As Variant

Dim DSum As Double

Dim DSum2 As Double

Dim p2 As Variant

Dim Address As String

l = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

p = ActiveSheet.Cells(Rows.Count, 5).End(xlUp).Row

ps = Worksheets("Sheet2").Cells(Rows.Count, 2).End(xlUp).Row

Dim CTPY As String

Dim VALUEDATE As Date

Dim Contacts As String


ReDim T(p + 1, 8)


For i = 1 To l

If Cells(1, i).Value = "Value Date" Then VALUEDATE = Cells(2, i).Value

Next i



For i = 1 To l

If Cells(1, i).Value = "Counterparty" Then CTPY = Cells(2, i).Value

Next i



'Primo
Set ra = Cells.Find(What:="Sicovam", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

If ra Is Nothing Then
        MsgBox ("Sicovam not found")



        Else

Dim SI() As Variant

i = 0

ReDim SI(0)

Range(ra, ra.End(xlDown)).Select

 i = 0

For Each cell In Range(ra, ra.End(xlDown))
     SI(i) = cell
    i = i + 1
    ReDim Preserve SI(i)
Next


End If

'Secondo

Set ra = Cells.Find(What:="Trade ID", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Trade ID not found")



        Else

Dim TI() As Variant

 i = 0

ReDim TI(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     TI(i) = cell
    i = i + 1
    ReDim Preserve TI(i)
Next


    End If

'Terzo

Set ra = Cells.Find(What:="Business Event", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Business Event not found")



        Else
 Dim BE() As Variant

 i = 0

 ReDim BE(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     BE(i) = cell
    i = i + 1
    ReDim Preserve BE(i)
Next


    End If

'Quarto

Set ra = Cells.Find(What:="Net Amount", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Net Amount not found")



        Else

 Dim NA() As Variant

 i = 0

 ReDim NA(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     NA(i) = cell
    i = i + 1
    ReDim Preserve NA(i)
Next


    End If

'Quinto

Set ra = Cells.Find(What:="Trade Date", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Trade Date not found")


        Else

 Dim TD() As Variant

 i = 0

 ReDim TD(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     TD(i) = cell
    i = i + 1
    ReDim Preserve TD(i)
Next

 End If

'Sesto

Set ra = Cells.Find(What:="Payment Date", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Payment Date not found")


        Else

 Dim PD() As Variant

 i = 0

 ReDim PD(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     PD(i) = cell
    i = i + 1
    ReDim Preserve PD(i)
Next

 End If

'Settimo

Set ra = Cells.Find(What:="Maturity", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Maturity not found")


        Else

 Dim MA() As Variant

 i = 0

 ReDim MA(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     MA(i) = cell
    i = i + 1
    ReDim Preserve MA(i)
Next

 End If


'Ottavo

Set ra = Cells.Find(What:="Nominal", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Nominal not found")


        Else

 Dim NO() As Variant

 i = 0

 ReDim NO(0)

        Range(ra, ra.End(xlDown)).Select

       i = 0
      For Each cell In Range(ra, ra.End(xlDown))
     NO(i) = cell
    i = i + 1
    ReDim Preserve NO(i)
Next

 End If

For i = 0 To p

T(i, 1) = SI(i)

Next i



For i = 0 To p

T(i, 2) = TI(i)

Next i


For i = 0 To p

T(i, 3) = BE(i)

Next i


For i = 0 To p

T(i, 4) = NA(i)

Next i


For i = 0 To p

T(i, 5) = TD(i)

Next i


For i = 0 To p

T(i, 6) = PD(i)

Next i


For i = 0 To p

T(i, 7) = MA(i)

Next i


For i = 0 To p

T(i, 8) = NO(i)

Next i



With Application.WorksheetFunction
   DSum = .Sum(.Index(T, 0, 5))
End With


DSum2 = Int(DSum * 100)
DSum = DSum2 / 100

T(p, 4) = DSum
T(p, 0) = "TOTAL"



Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


ActiveSheet.Range("B" & p + 1 & ":I" & 2 * p + 1) = T()

ActiveSheet.Range("B" & p + 1 & ":I" & 2 * p + 1).Copy




'For i = 1 To p2

'If Worksheets("Sheet2").Cells(i, 2).Value = CTPY Then Address = Worksheets("Sheet2").Cells(i, 4).Value


'Next i





       Address = Application.WorksheetFunction.VLookup(CTPY, _
              Worksheets("Sheet2").Range("B:D"), 3, 1)




On Error Resume Next
    With OutMail
        .to = Address
        .CC = ""
        .BCC = ""
        .Subject = "Amount to confirm Value Date" & " " & VALUEDATE & " " & CTPY
        .Body = "Hello, " & vbNewLine & vbNewLine & "Could you confirm the net amount below?" & T(p + 1, 8)
        .Attachments.Add ""
        .Display

End With
On Error GoTo 0

Set OutMail = Nothing





End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多