在Lotus Notes中发布指向Excel文档的链接

时间:2013-10-29 16:53:28

标签: html excel vba lotus-notes lotusscript

我有一张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,然后其余为空白。

1 个答案:

答案 0 :(得分: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

End Sub