0

我有一张 Excel 表格,用作报告的自动邮寄。就目前而言,它将 Excel 工作簿的实际副本附加到电子邮件中并将其发送出去。邮件包含几个不同的人,他们每天收到不同的报告。由于某些文件的大小,我开始遇到一个问题,我无法再发送电子邮件,因为它们太大了,所以我想改用发送文件的链接,我已经打了墙。

我使用 Lotus Notes 8.5。VBA 将在一个范围内循环,每个单元格都有一个由“,”分隔的报告列表。它获取列表并将其作为字符串传递给邮件程序。邮件程序获取字符串,将其转换为数组并将其拆分,然后检查以确保报告是最新的。一封电子邮件中最多可以包含 10 个不同的报告。我已尝试创建 HTML MIME 电子邮件以包含链接。这是我目前拥有的代码:

Sub Send_HTML_Email(ByRef Name As String, ByRef Address As String, ByRef Reports As String)

    Const ENC_IDENTITY_8BIT = 1729

     'Send Lotus Notes email containing links to files on local computer

    Dim NSession As Object 'NotesSession
    Dim NDatabase As Object 'NotesDatabase
    Dim NStream As Object 'NotesStream
    Dim NDoc As Object 'NotesDocument
    Dim NMIMEBody As Object 'NotesMIMEEntity
    Dim SendTo As String
    Dim subject As String
    Dim HTML As String, HTMLbody As String
    Dim Array1() As String
    Dim Links As String
    Dim gRange As Variant
    Dim i As Integer

    SendTo = "myEmail@address.com"
    subject = "My Subject " & Name & "."
    Debug.Print subject

    Set NSession = CreateObject("Notes.NotesSession") 'using Lotus Notes Automation Classes (OLE)
    Set NDatabase = NSession.GetDatabase("", "")

    If Not NDatabase.IsOpen Then NDatabase.OPENMAIL

    Set NStream = NSession.CreateStream

     Array1 = Split(Reports, ",")

    i = 1

        HTML = "<html>" & vbLf & _
        "<head>" & vbLf & _
        "<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />" & vbLf & _
        "</head>" & vbLf & _
        "<body>" & vbLf & _
        "<p>" & gRange.Value & "</p>"

    For Each gRange In Array1

    Select Case gRange

        Case "Report name 1"
        Reports = "G:\file Location\Report Name 1.xlsx"
        Case "Report name 2"
        Reports = "G:\file Location\Report Name 2.xlsx"
        Case "Report name 3"
        Reports = "G:\file Location\Report Name 3.xlsx"
        Case "Report name 4"
        Reports = "G:\file Location\Report Name 4.xlsx"
        Case "Report name 5"
        Reports = "G:\file Location\Report Name 5.xlsx"
        Case "Report name 6"
        Reports = "G:\file Location\Report Name 6.xlsx"
    End Select

        If Reports <> "" And Format(FileDateTime(Reports), "mm/dd/yyyy") = Format(Now, "mm/dd/yyyy") Then

        Select Case gRange

            Case "Report name 1"
            Links = "G:\file%20Location\Report%20Name%201.xlsx"
            Case "Report name 2"
            Links = "G:\file%20Location\Report%20Name%202.xlsx"
            Case "Report name 3"
            Links = "G:\file%20Location\Report%20Name%203.xlsx"
            Case "Report name 4"
            Links = "G:\file%20Location\Report%20Name%204.xlsx"
            Case "Report name 5"
            Links = "G:\file%20Location\Report%20Name%205.xlsx"

        End Select

            If Links <> "" Then
            HTMLbodyi = ""<a href='file://" & Links & "'>" & gRange & "</a><br>""
            End If

        "</body>" & vbLf & _
        "</html>"

        i = i + 1

        End If

    Next gRange


    NSession.ConvertMime = False 'Don't convert MIME to rich text

    Set NDoc = NDatabase.CreateDocument()

    With NDoc
        .Form = "Memo"
        .subject = subject
        .SendTo = Split(SendTo, ",")

        Set NMIMEBody = .CreateMIMEEntity
        NStream.WriteText HTML
        NMIMEBody.SetContentFromText NStream, "text/html; charset=UTF-8", ENC_IDENTITY_8BIT

        .Send False
        .Save True, False, False
    End With

    NSession.ConvertMime = True 'Restore conversion

    Set NDoc = Nothing
    Set NSession = Nothing

End Sub

我正在使用 Case 语句来切换基于数组的报表和链接集,该数组来自单元格中具有不同报表名称的单元格。一个人的单元格可能只有Report name 1Report name 3,而下一个人拥有所有这些。

我真的很感激我能得到的任何帮助!

电子邮件将发送,但它们要么是空白的,要么它们到达第一个HTMLbodyi并且只包含链接应该去的初始<a位置,然后其余部分是空白的。

4

1 回答 1

3

我看到这条线有问题:

HTMLbodyi = "<a href='file://" & Links & "></><br>"

HTML 格式不正确。需要改成这样:

HTMLbodyi = "<a href='file://" & Links & "'>" & gRange & "</a><br>"

另一个问题是您在循环的每次迭代中都设置了 HTML 行,但您真正想要做的只是将链接附加到循环中。您需要分解此代码:

HTML = "<html>" & vbLf & _
    "<head>" & vbLf & _
    "<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />" & vbLf & _
    "</head>" & vbLf & _
    "<body>" & vbLf & _
    "<p>" & gRange.Value & "</p>" & _
    HTMLbodyi & _
    "</body>" & vbLf & _
    "</html>"

将您的 HTML 字符串设置为循环之前的标记部分。然后在循环中,附加链接。然后最后在循环之后,添加</body></html>到 HTML 字符串。

编辑: 这是更新的代码。按照 HTML 变量查看关键更改。

Sub Send_HTML_Email(ByRef Name As String, ByRef Address As String, ByRef Reports As String)

Const ENC_IDENTITY_8BIT = 1729

 'Send Lotus Notes email containing links to files on local computer

Dim NSession As Object 'NotesSession
Dim NDatabase As Object 'NotesDatabase
Dim NStream As Object 'NotesStream
Dim NDoc As Object 'NotesDocument
Dim NMIMEBody As Object 'NotesMIMEEntity
Dim SendTo As String
Dim subject As String
Dim HTML As String, HTMLbody As String
Dim Array1() As String
Dim Links As String
Dim gRange As Variant
Dim i As Integer

SendTo = "myEmail@address.com"
subject = "My Subject " & Name & "."
Debug.Print subject

Set NSession = CreateObject("Notes.NotesSession") 'using Lotus Notes Automation Classes (OLE)
Set NDatabase = NSession.GetDatabase("", "")

If Not NDatabase.IsOpen Then NDatabase.OPENMAIL

Set NStream = NSession.CreateStream

 Array1 = Split(Reports, ",")

i = 1

HTML = "<html>" & vbLf & _
    "<head>" & vbLf & _
    "<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />" & vbLf & _
    "</head>" & vbLf & _
    "<body>" & vbLf


For Each gRange In Array1

Select Case gRange

    Case "Report name 1"
    Reports = "G:\file Location\Report Name 1.xlsx"
    Case "Report name 2"
    Reports = "G:\file Location\Report Name 2.xlsx"
    Case "Report name 3"
    Reports = "G:\file Location\Report Name 3.xlsx"
    Case "Report name 4"
    Reports = "G:\file Location\Report Name 4.xlsx"
    Case "Report name 5"
    Reports = "G:\file Location\Report Name 5.xlsx"
    Case "Report name 6"
    Reports = "G:\file Location\Report Name 6.xlsx"
End Select

    If Reports <> "" And Format(FileDateTime(Reports), "mm/dd/yyyy") = Format(Now, "mm/dd/yyyy") Then

    Select Case gRange

        Case "Report name 1"
        Links = "G:\file%20Location\Report%20Name%201.xlsx"
        Case "Report name 2"
        Links = "G:\file%20Location\Report%20Name%202.xlsx"
        Case "Report name 3"
        Links = "G:\file%20Location\Report%20Name%203.xlsx"
        Case "Report name 4"
        Links = "G:\file%20Location\Report%20Name%204.xlsx"
        Case "Report name 5"
        Links = "G:\file%20Location\Report%20Name%205.xlsx"

    End Select

        If Links <> "" Then
        HTML = HTML & ""<p><a href='file://" & Links & "'>" & gRange & "</a></p>""
        End If

    i = i + 1

    End If

Next gRange

HTML = HTML & "</body>" & vbLf & "</html>"

NSession.ConvertMime = False 'Don't convert MIME to rich text

Set NDoc = NDatabase.CreateDocument()

With NDoc
    .Form = "Memo"
    .subject = subject
    .SendTo = Split(SendTo, ",")

    Set NMIMEBody = .CreateMIMEEntity
    NStream.WriteText HTML
    NMIMEBody.SetContentFromText NStream, "text/html; charset=UTF-8", ENC_IDENTITY_8BIT

    .Send False
    .Save True, False, False
End With

NSession.ConvertMime = True 'Restore conversion

Set NDoc = Nothing
Set NSession = Nothing

结束子

于 2013-10-29T18:15:33.837 回答