简体   繁体   English

通过 IBM Notes 使用 vba 发送电子邮件时遇到问题

[英]Trouble with sending emails with vba via IBM Notes

with the function below, I have the option of sending e-mails from Excel via IBM Notes.使用下面的 function,我可以选择通过 IBM Notes 从 Excel 发送电子邮件。 Basically, it works very well.基本上,它工作得很好。 However, I recently had to revise the code, as the message text was always inserted under the IBM Notes signature.但是,我最近不得不修改代码,因为消息文本总是插入到 IBM Notes 签名下。 I was able to solve this problem, but unfortunately two problems have now emerged that I did not had before.我能够解决这个问题,但不幸的是,现在出现了两个我以前没有的问题。

I am grateful for every tip and every help!我感谢每一个提示和每一个帮助!

UPDATE 21.12.2021 21:30: @Tode I've followed your instructions but the problems are still there.更新 21.12.2021 21:30: @Tode 我已按照您的指示进行操作,但问题仍然存在。 May be I've failed to put the codelines in the right order?可能是我未能将代码行按正确的顺序排列?

The problems问题

The "Save" function no longer works, ie IBM Notes also saves the e-mail in the "Sent" folder if I do not want it (parameter blnSaveEMail = false). “保存” function 不再起作用,即如果我不想要,IBM Notes 还会将电子邮件保存在“已发送”文件夹中(参数 blnSaveEMail = false)。

The second problem has to do with my work context: I have two email accounts.第二个问题与我的工作环境有关:我有两个 email 帐户。 A personal service e-mail address jdoe@company.com (mailfile: jdoe.nsf) and a branch e-mail address mybranch@company.de (mailfile: mybranch.nsf).个人服务电子邮件地址 jdoe@company.com(邮件文件:jdoe.nsf)和分支机构电子邮件地址 mybranch@company.de(邮件文件:mybranch.nsf)。 As far as I could determine, both mail files are in the same base directory.据我所知,两个邮件文件都在同一个基本目录中。 If I use the code below with my personal e-mail, the parameter blnQuickSend = true works without problems, if I use my branch e-mail address, IBM Notes asks me whether I want to save the changes, although I would like to send an e-mail without asking.如果我将下面的代码与我的个人电子邮件一起使用,参数 blnQuickSend = true 可以正常工作,如果我使用我的分支电子邮件地址,IBM Notes 会询问我是否要保存更改,尽管我想发送没有询问的电子邮件。

I hope I was able to describe my issue clearly and understandably.我希望我能够清晰易懂地描述我的问题。 I thank you for your attention!我感谢您的关注!

Warm greetings from Dresden Sergeij德累斯顿谢尔盖的热情问候

PS: I'am a german native:), thankfully Google helped me a lot to translate my problem in english. PS:我是德国人:),谢天谢地谷歌帮了我很多,把我的问题翻译成英文。

The code编码

    Public Function Send_EMail( _
    varRecipient As Variant, _
    varCopyTo As Variant, _
    varBlindcopyTo As Variant, _
    strSubject As String, _
    strMessage As String, _
    strAttachement As String, _
    Optional blnSaveEMail As Boolean = True, _
    Optional blnQuickSend As Boolean = False, _
    Optional strAlternative_Mailfile As String _
        ) As Boolean
 
    Dim objLotusNotes As Object
    Dim objMaildatabase As Object 'Die Maildatabase
    Dim strMailServer As String 'Der Mailserver
    Dim strMailFile As String ' Die Maildatei
    Dim objEMail As Object 'Die E-Mail in IBM Notes
    Dim objAttachement As Object 'Das Anlage Richtextfile Object
    Dim objSession As Object 'Die Notes Session
    Dim objEmbedded As Object 'Attachement
    Dim arrAttachements() As String 'Liste mehrere Anhänge
    Dim lngIndex As Long
    Dim strFilepath As String
    Dim objNotesfield As Object 'Datenfeld in IBM Notes
    Dim objCurrentEMail As Object 'Aktuelle E-Mail
          

'Start an IBM Notes Session '启动一个 IBM Notes Session

    Set objSession = CreateObject("Notes.NotesSession")
     

'Open IBM-Notes-Database '打开 IBM-Notes-Database

    strMailServer = objSession.GetEnvironmentString("MailServer", True)
    
    If VBA.Len(strAlternative_Mailfile) = 0 Then
        strMailFile = objSession.GetEnvironmentString("MailFile", True)
    Else
        strMailFile = "mail/" & strAlternative_Mailfile
    End If
    
    Set objMaildatabase = objSession.GETDATABASE(strMailServer, strMailFile)

'If your constructed path (variable strMailFile) is wrong or the database cannot be accessed 'then this line will make sure to fallback to the mailfile configured in your location document in Notes Client. '如果您构建的路径(变量 strMailFile)错误或无法访问数据库,则此行将确保回退到您在 Notes 客户端的位置文档中配置的邮件文件。

    If Not objMaildatabase.IsOpen Then objMaildatabase.OPENMAIL
     

'Create new email '创建新的 email

    Set objEMail = objMaildatabase.CREATEDOCUMENT

'set saveoption '设置保存选项

    objEMail.ReplaceItemValue "SAVEOPTIONS", "0"

'Put content in fields '将内容放入字段中

    Set objNotesfield = objEMail.APPENDITEMVALUE("Subject", strSubject)
    Set objNotesfield = objEMail.APPENDITEMVALUE("SendTo", varRecipient)
    Set objNotesfield = objEMail.APPENDITEMVALUE("BlindCopyTo", varBlindcopyTo)
    Set objNotesfield = objEMail.APPENDITEMVALUE("CopyTo", varCopyTo)
    

'Load workspace '加载工作区

    Set objLotusNotes = CreateObject("Notes.NotesUIWorkspace")
     

'Add attachements '添加附件

    arrAttachements = VBA.Split(strAttachement, ";")
     
    For lngIndex = LBound(arrAttachements) To UBound(arrAttachements)
        strFilepath = arrAttachements(lngIndex)
        If strFilepath <> "" And VBA.Dir(strFilepath) <> "" Then
            Set objAttachement = objEMail.CREATERICHTEXTITEM("Attachment" & lngIndex)
            Set objEmbedded = _
                objAttachement.EMBEDOBJECT(1454, "", strFilepath, "Attachment" & lngIndex)
        End If
    Next

'Open eMail in frontend and assign to NotesUIDocument variable '在前端打开 eMail 并分配给 NotesUIDocument 变量

    Set objCurrentEMail = objLotusNotes.EDITDOCUMENT(True, objEMail)
    

'Put content into email '将内容放入email

     objCurrentEMail.GotoField "Body"
     objCurrentEMail.InsertText strMessage 

'Check, whether the email should be sent immediately or not '检查,email是否应该立即发送

    If blnQuickSend = True Then

'Send email '送email

        objCurrentEMail.Send

'Save email, if requested '如果需要,请保存 email

        If blnSaveEMail Then objCurrentEMail.Save
        

'Close email '关闭 email

        objCurrentEMail.Close
        
    End If

'Return TRUE '返回真

    Send_EMail = True
    

End Function

Ok... where should I start... there are some logical errors in your code based on not understanding the methods you use and the difference between frontend- and backend- classes...好的...我应该从哪里开始...由于不了解您使用的方法以及前端和后端类之间的区别,您的代码中存在一些逻辑错误...

Let's begin at the top:让我们从顶部开始:

'Check whether the maildatabase is open or not 'Throws an error, 
'if the database is not open
If Not objMaildatabase.IsOpen Then objMaildatabase.OPENMAIL

Your comment is wrong.你的评论是错误的。 No error is thrown at all.根本不会抛出任何错误。 If your constructed path (variable strMailFile) is wrong or the database cannot be accessed then this line will make sure to fallback to the mailfile configured in your location document in Notes Client.如果您构建的路径(变量 strMailFile)错误或无法访问数据库,则此行将确保回退到您在 Notes 客户端的位置文档中配置的邮件文件。

'Create new email-document
objLotusNotes.EDITDOCUMENT True, objEMail

Again: Comment is wrong.再次:评论是错误的。 What this command does is: It opens the email that you created in backend (represented by variable objEMail) in the frontend.该命令的作用是:它在前端打开您在后端创建的 email(由变量 objEMail 表示)。

'Select the current email
Set objCurrentEMail = objLotusNotes.CurrentDocument

and assigns it to a NotesUIDocument- frontend- variable (select the current email is wrong).并将其分配给 NotesUIDocument-frontend- 变量(选择当前的 email 是错误的)。 As "EDITDOCUMENT" already returns as NotesUIDocument, you could shorten this like this:由于“EDITDOCUMENT”已经作为 NotesUIDocument 返回,您可以这样缩短:

'Open eMail in frontend and assign to NotesUIDocument variable
Set objCurrentEMail = objLotusNotes.EDITDOCUMENT(True, objEMail)

After having created a frontenddocument you still continue to manipulate the (now linked) backend document.创建前端文档后,您仍然可以继续操作(现在链接的)后端文档。 You should move the creation of the frontend all the way down to the end of your code as having a document open in frontend does not work well with manipulating the same document in backend, especially when handling NotesRichtextItems and attachments.您应该将前端的创建一直移动到代码的末尾,因为在前端打开文档不能很好地在后端处理相同的文档,尤其是在处理 NotesRichtextItems 和附件时。 So move the above lines just below the for- loop.因此,将上述行移到 for 循环的下方。

'Set if email should be saved or not
objEMail.SAVEMESSAGEONSEND = blnSaveEMail

Yes... but no: You set the property SAVEMESSAGEONSEND to the backend document objEMail.是的...但不是:您将属性 SAVEMESSAGEONSEND 设置为后端文档 objEMail。 Unfortunately the frontend- document objCurrentEMail does not care at all for this.不幸的是,前端文档 objCurrentEMail 根本不关心这个。 To have your code obey this option, you would have to use the send- method of objEMail, not the send- method of objCurrentEMail.要让您的代码遵循此选项,您必须使用 objEMail 的 send- 方法,而不是 objCurrentEMail 的 send- 方法。 If you want the frontend to not save a document that it sends, you need to do it differently by setting a field called "SAVEOPTIONS" to "0":如果您希望前端不保存它发送的文档,您需要通过将名为“SAVEOPTIONS”的字段设置为“0”来做不同的事情:

objEMail.ReplaceItemValue( "SAVEOPTIONS", "0" )


'Send email
 objCurrentEMail.Send False, varRecipient

regarding your comment: almost... unfortunately you try the NotesDocument backend method "send" (that has 2 parameters) against the NotesUIDocument- Object "objCurrentEMail. NotesUIDocument has a send method as well, but it does not have any parameters..normally an error should be thrown here....关于您的评论:几乎...不幸的是,您尝试针对 NotesUIDocument-Object“objCurrentEMail”使用 NotesDocument 后端方法“send”(具有 2 个参数)。NotesUIDocument 也有一个发送方法,但它没有任何参数..通常这里应该抛出一个错误......

EITHER you try to send the backend:您尝试发送后端:

objEMail.Send False, varRecipient

OR you send it in the frontend:或者你在前端发送它:

objCurrentEMail.Send

Your "objCurrentEMail.Close" will always ask you if you want to save the document unless you have set SAVEOPTIONS = "0".您的“objCurrentEMail.Close”将始终询问您是否要保存文档,除非您已设置 SAVEOPTIONS = “0”。 If you really want to save the document after sending, use如果您真的想在发送后保存文档,请使用

objCurrentEMail.Save

before the close.收盘前。

Hope that helps you sort out some of the issues.希望能帮助你解决一些问题。

thank you very much for your help.非常感谢您的帮助。 I'm not good at working with the NOTES-API.我不擅长使用 NOTES-API。

I've tried to follow your instructions, but unfortuneately the problems are still there.我已尝试按照您的指示进行操作,但不幸的是问题仍然存在。

Here is my code with your suggestions.这是我的代码以及您的建议。 I think, I didn't place the codeline in the right order.我想,我没有按正确的顺序放置代码行。 Could you please look over it again?请你再看一遍好吗?

    Public Function Send_EMail( _
    varRecipient As Variant, _
    varCopyTo As Variant, _
    varBlindcopyTo As Variant, _
    strSubject As String, _
    strMessage As String, _
    strAttachement As String, _
    Optional blnSaveEMail As Boolean = True, _
    Optional blnQuickSend As Boolean = False, _
    Optional strAlternative_Mailfile As String _
        ) As Boolean
 
    Dim objLotusNotes As Object
    Dim objMaildatabase As Object 'Die Maildatabase
    Dim strMailServer As String 'Der Mailserver
    Dim strMailFile As String ' Die Maildatei
    Dim objEMail As Object 'Die E-Mail in IBM Notes
    Dim objAttachement As Object 'Das Anlage Richtextfile Object
    Dim objSession As Object 'Die Notes Session
    Dim objEmbedded As Object 'Attachement
    Dim arrAttachements() As String 'Liste mehrere Anhänge
    Dim lngIndex As Long
    Dim strFilepath As String
    Dim objNotesfield As Object 'Datenfeld in IBM Notes
    Dim objCurrentEMail As Object 'Aktuelle E-Mail
          

'Start an IBM Notes Session '启动一个 IBM Notes Session

    Set objSession = CreateObject("Notes.NotesSession")
     

'Open IBM-Notes-Database '打开 IBM-Notes-Database

    strMailServer = objSession.GetEnvironmentString("MailServer", True)
    
    If VBA.Len(strAlternative_Mailfile) = 0 Then
        strMailFile = objSession.GetEnvironmentString("MailFile", True)
    Else
        strMailFile = "mail/" & strAlternative_Mailfile
    End If
    
    Set objMaildatabase = objSession.GETDATABASE(strMailServer, strMailFile)

'If your constructed path (variable strMailFile) is wrong or the database cannot be accessed 'then this line will make sure to fallback to the mailfile configured in your location document in Notes Client. '如果您构建的路径(变量 strMailFile)错误或无法访问数据库,则此行将确保回退到您在 Notes 客户端的位置文档中配置的邮件文件。

    If Not objMaildatabase.IsOpen Then objMaildatabase.OPENMAIL
     

'Create new email '创建新的 email

    Set objEMail = objMaildatabase.CREATEDOCUMENT

'set saveoption '设置保存选项

    objEMail.ReplaceItemValue "SAVEOPTIONS", "0"

'Put content in fields '将内容放入字段中

    Set objNotesfield = objEMail.APPENDITEMVALUE("Subject", strSubject)
    Set objNotesfield = objEMail.APPENDITEMVALUE("SendTo", varRecipient)
    Set objNotesfield = objEMail.APPENDITEMVALUE("BlindCopyTo", varBlindcopyTo)
    Set objNotesfield = objEMail.APPENDITEMVALUE("CopyTo", varCopyTo)
    

'Load workspace '加载工作区

    Set objLotusNotes = CreateObject("Notes.NotesUIWorkspace")
     

'Add attachements '添加附件

    arrAttachements = VBA.Split(strAttachement, ";")
     
    For lngIndex = LBound(arrAttachements) To UBound(arrAttachements)
        strFilepath = arrAttachements(lngIndex)
        If strFilepath <> "" And VBA.Dir(strFilepath) <> "" Then
            Set objAttachement = objEMail.CREATERICHTEXTITEM("Attachment" & lngIndex)
            Set objEmbedded = _
                objAttachement.EMBEDOBJECT(1454, "", strFilepath, "Attachment" & lngIndex)
        End If
    Next

'Open eMail in frontend and assign to NotesUIDocument variable '在前端打开 eMail 并分配给 NotesUIDocument 变量

    Set objCurrentEMail = objLotusNotes.EDITDOCUMENT(True, objEMail)
    

'Put content into email '将内容放入email

     objCurrentEMail.GotoField "Body"
     objCurrentEMail.InsertText strMessage 

'Check, whether the email should be sent immediately or not '检查,email是否应该立即发送

    If blnQuickSend = True Then

'Send email '送email

        objCurrentEMail.Send

'Save email, if requested '如果需要,请保存 email

        If blnSaveEMail Then objCurrentEMail.Save
        

'Close email '关闭 email

        objCurrentEMail.Close
        
    End If

'Return TRUE '返回真

    Send_EMail = True
    

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM