2013-10-29 80 views
0

我有一張Excel工作表,我用它作爲郵件自動報告功能。目前,它會將Excel工作簿的實際副本附加到電子郵件中併發送出去。郵件包含幾個不同的人,他們每天都會得到不同的報告。由於一些文件的大小,我開始遇到一個問題,我無法再發送電子郵件了,因爲它們太大了,所以我想切換到鏈接發送文件,而我已經打了壁。在Lotus Notes中發佈指向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 = "[email protected]" 
    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,其中鏈接應該去,其餘爲空。

回答

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變量查看關鍵更改。

子Send_HTML_Email(爲ByRef名稱作爲字符串,爲ByRef地址作爲字符串,爲ByRef報告作爲字符串)

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 = "[email protected]" 
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 

末次

+0

我理解的格式不正確的HTML,我忽略了」,但whne你說拆分HTML來追加'HTMLbodyi',我很難獲得格式化權限。它看起來像這樣:我在最初的問題中修改了代碼。 – William

+0

我已經更新了我的答案。 –

+0

謝謝!我得到了它的工作。我非常感謝Ken的幫助。 – William