2017-02-28 90 views
1

我正在使用以下vba代码尝试使用附件从IBM Notes发送电子邮件。VBA通​​过IBM Notes发送电子邮件无效?

这里是我的代码:

Sub Send_Email() 

    Dim answer As Integer 
    answer = MsgBox("Are you sure you want to Send All Announcements?", vbYesNo + vbQuestion, "Notice") 
    If answer = vbNo Then 
    Exit Sub 

    Else 

    'Define Parameters for Email 
    Dim s As Object 
    Dim db As Object 
    Dim body As Object 
    Dim bodyChild As Object 
    Dim header As Object 
    Dim stream As Object 
    Dim host As String 
    Dim MailDoc As Object 

    'Define Sheet Parameters 

    Dim i As Long 
    Dim j As Long 
    Dim server, mailfile, user, usersig As String 
    Dim LastRow As Long, ws As Worksheet 
    LastRow = Worksheets(1).Range("F" & Rows.Count).End(xlUp).Row 'Finds the last used row 

    j = 18 


    'Start a session of Lotus Notes 
    Set Session = CreateObject("Notes.NotesSession") 
    'This line prompts for password of current ID noted in Notes.INI 
    Set db = Session.CurrentDatabase 
    Set stream = Session.CreateStream 
    ' Turn off auto conversion to rtf 
    Session.ConvertMime = False 





    With ThisWorkbook.Worksheets(1) 

    For i = 18 To LastRow 



    ' Create message 
    Set MailDoc = db.CreateDocument 
    MailDoc.Form = "Memo" 

    'Set From 
    MailDoc.SendTo = Range("Q" & i).value 

    MailDoc.SentBy = "[email protected]" 
    MailDoc.tmpDisplaySentBy = "[email protected]" 
    MailDoc.FROM = "[email protected]" 
    MailDoc.SendFrom = "[email protected]" 
    MailDoc.Principal = "Food Specials <mailto:[email protected]>" 

    MailDoc.Subject = "Promotion Announcement for week " & Range("I8").value & ", " & Range("T8").value & " - Confirmation required" 


    'MailDoc.SendTo = Range("Q" & i).value 
    'Call MailDoc.ReplaceItemValue("CopyTo", "[email protected]") 

    MailDoc.SaveMessageOnSend = True 

    ' Create the body to hold HTML and attachment 
    Set body = MailDoc.CreateMIMEEntity 
    'Child mime entity which is going to contain the HTML which we put in the stream 
    Set bodyChild = body.CreateChildEntity() 
    Call stream.WriteText(strbody) 
    Call bodyChild.SetContentFromText(stream, "text/html;charset=iso-8859-1", ENC_NONE) 
    Call stream.Close 
    Call stream.Truncate 


    ' Get the attachment file name 
    filename = Range("F" & i).value 
    'A new child mime entity to hold a file attachment 
    Set header = bodyChild.CreateHeader("Content-Type") 
    Call header.SetHeaderVal("multipart/mixed") 
    Set header = bodyChild.CreateHeader("Content-Disposition") 
    Call header.SetHeaderVal("attachment; filename=" & filename) 
    Set header = bodyChild.CreateHeader("Content-ID") 
    Call header.SetHeaderVal(filename) 
    Set stream = Session.CreateStream() 




    Call bodyChild.SetContentFromBytes(stream, "application/msexcel", ENC_IDENTITY_BINARY) ' All my attachments are excel this would need changing depensding on your attachments. 
    'Call bodyChild.SetContentFromBytes(1454, "", Range("F" & i).value, "Attachment") 


    'Send the email 
    Call MailDoc.Send(False) 

    Session.ConvertMime = True ' Restore conversion 










     j = j + 1 

        Next i 
        End With 




    'Clean Up the Object variables - Recover memory 


     Application.CutCopyMode = False 


    MsgBox "Success!" & vbNewLine & "Announcements have been sent." 

    End If 

    End Sub 

它似乎并不想附加任何附件或发送。 我得到一个错误:对象变量或与块变量未设置在这条线:

Call header.SetHeaderVal("multipart/mixed") 

请能有人告诉我在哪里,我错了?

编辑2:

好吧,我设法摆脱错误的和获得的电子邮件发送。

但是,它没有正确发送附件。我看到的是这样的:

enter image description here

下面是代码:

Sub Send_Email() 

Dim answer As Integer 
answer = MsgBox("Are you sure you want to Send All Announcements?", vbYesNo + vbQuestion, "Notice") 
If answer = vbNo Then 
Exit Sub 

Else 

'Define Parameters for Email 
Dim s As Object 
Dim db As Object 
Dim body As Object 
Dim bodyChild As Object 
Dim header As Object 
Dim stream As Object 
Dim host As String 
Dim MailDoc As Object 

'Define Sheet Parameters 

Dim i As Long 
Dim j As Long 
Dim server, mailfile, user, usersig As String 
Dim LastRow As Long, ws As Worksheet 
LastRow = Worksheets(1).Range("F" & Rows.Count).End(xlUp).Row 'Finds the last used row 

j = 18 


'Start a session of Lotus Notes 
Set Session = CreateObject("Notes.NotesSession") 
'This line prompts for password of current ID noted in Notes.INI 
Set db = Session.CurrentDatabase 
Set stream = Session.CreateStream 
' Turn off auto conversion to rtf 
Session.ConvertMime = False 





With ThisWorkbook.Worksheets(1) 

For i = 18 To LastRow 



' Create message 
Set MailDoc = db.CreateDocument 
MailDoc.Form = "Memo" 

'Set From 
MailDoc.SendTo = Range("Q" & i).value 

MailDoc.SentBy = "[email protected]" 
MailDoc.tmpDisplaySentBy = "[email protected]" 
MailDoc.FROM = "[email protected]" 
MailDoc.SendFrom = "[email protected]" 
MailDoc.Principal = "Food Specials <mailto:[email protected]>" 

MailDoc.Subject = "Promotion Announcement for week " & Range("I8").value & ", " & Range("T8").value & " - Confirmation required" 


'MailDoc.SendTo = Range("Q" & i).value 
'Call MailDoc.ReplaceItemValue("CopyTo", "[email protected]") 

MailDoc.SaveMessageOnSend = True 

' Create the body to hold HTML and attachment 
Set body = MailDoc.CreateMIMEEntity 
'Child mime entity which is going to contain the HTML which we put in the stream 
Set bodyChild = body.CreateChildEntity() 
Call stream.WriteText(strbody) 
Call bodyChild.SetContentFromText(stream, "text/html;charset=iso-8859-1", ENC_NONE) 
Call stream.Close 
Call stream.Truncate 


filename = Range("F" & i).value 

'A new child mime entity to hold a file attachment 
     Set bodyChild = body.CreateChildEntity() 
     Set header = bodyChild.CreateHeader("Content-Type") 
     header.SetHeaderVal ("multipart/mixed") 

     Set header = bodyChild.CreateHeader("Content-Disposition") 
     header.SetHeaderVal ("attachment; filename=" & filename) 

     Set header = bodyChild.CreateHeader("Content-ID") 

     header.SetHeaderVal (filename) 

     Set stream = Session.CreateStream() 


     Call bodyChild.SetContentFromBytes(stream, "application/msexcel", ENC_IDENTITY_BINARY) ' All my attachments are excel this would need changing depensding on your attachments. 


'Send the email 
Call MailDoc.Send(False) 

Session.ConvertMime = True ' Restore conversion 










    j = j + 1 

       Next i 
       End With 




'Clean Up the Object variables - Recover memory 


    Application.CutCopyMode = False 


MsgBox "Success!" & vbNewLine & "Announcements have been sent." 

End If 

End Sub 

请有人可以告诉我为什么我的Excel文件没有正确安装?谢谢

回答

0

您正试图CallObject的方法,没有必要这样做。

Call是调用Sub的过时方式。它不再是必需的,并且通常最终导致微妙的运行时错误,应该避免。

改变

Call header.SetHeaderVal("multipart/mixed") 

header.SetHeaderVal = "multipart/mixed" 

应该做的伎俩。如果这样做,并且您将RTE置于下一行,请重复该过程以适应Call的所有不必要用途。

另外,我不知道的注意事项(用它几年前,从来没有编程的话),但是这个代码

Set header = bodyChild.CreateHeader("Content-Type") 
Call header.SetHeaderVal("multipart/mixed") 
Set header = bodyChild.CreateHeader("Content-Disposition") 
Call header.SetHeaderVal("attachment; filename=" & filename) 
Set header = bodyChild.CreateHeader("Content-ID") 

,你一直在同一个变量header设置为一个新的项目看起来很可疑。我不确定你会如何去设定这些,但它看起来不正确。

其他建议:

  • 使用通用Object具体Notes.<something>对象类型更改Dim语句。 (除非Notes需要一个通用的对象 - 我没有使用Notes的年龄,并且没有为它编程。)
  • 删除大量多余的空白行。有些空白可以帮助将代码直观地分组为逻辑块,但是所有附加内容使其很难阅读。
  • 正确缩进代码。由于大部分数据都是左对齐的,因此很难区分IF,WithFor块的结束位置,但随机数位将随机数缩进。
    • 如果您IfEnd If语句在同一列排队虽然这其中包含的所有东西都要缩(2或4列),可以很容易地看到什么是包含在If声明。
  • 退房Rubberduck - 它会自动完成缩进你以及带来了很多其他很酷的技巧&玩具表。 (不是作者,而是一个快乐的用户和无意的beta测试器。)
+0

好的,谢谢我设法让代码工作(种)。没有更多的错误,但我的Excel文件没有正确附加。请参阅EDIT2 – user7415328

+0

我将假定您的附件未附加,因为您没有正确分配它。您需要消除“Call”的所有不当使用。我挑选了最初的几个 - 你将不得不追捕其余的。 *我认为VBA中没有任何'Call'的正确用法。这是一个过时的功能,留在规范中,以防止古代码破坏Office产品的新版本。 – FreeMan

0

它在我看来像你已经得到了你的MIME头的顺序和结构错误。您首先生成一个文本\ html部分,然后是多部分\ mixed,然后您将multipart \的内容设置为应用程序\ msexcel混合。

多部分\混合部分应该是一个容器。它没有自己的内容。它包含两个或更多子部分。

您应该在顶层创建一个multipart \ mixed MIMEEntity(body的子体),然后创建两个属于multipart \ mixed MIMEEntity子级的二级子MIMEntities:一个包含content-type text \ html,第二个是内容类型应用程序\ msexcel。

最好的策略是手动发送一个看起来像你想要的消息,然后看看它的MIME源代码,并在你的代码中复制它的树结构和顺序。

此外,application \ msexcel content-type适用于旧式.xls文件。您可能想要查看this article以获取更新的版本。