从 Excel VBA 发送格式化的 Lotus Notes 富文本电子邮件
我对 Lotus Script 或 Notes/Domino 的了解很少,但我有一个很久以前从某处复制的过程,它允许我从 VBA 通过 Notes 发送电子邮件。 我通常只将其用于内部通知,其中格式并不重要。
我现在想用它向客户发送外部电子邮件,而企业类型希望电子邮件符合我们的风格指南(基本上是无衬线字体)。
我正要告诉他们该代码仅适用于纯文本,但后来我注意到该例程确实引用了某种 CREATERICHTEXTITEM 对象。 这是否意味着我可以在将正文字符串传递到邮件例程之后对其应用某种格式? 除了维护我们宝贵的品牌价值之外,这对我来说对于突出显示电子邮件中的某些段落也非常方便。
我对“网络”进行了深入研究,看看这段代码是否可以改编,但由于不熟悉 Notes 的对象模型,而且在线 Notes 资源似乎反映了应用程序本身的迟钝性,这意味着我并没有得到太多的帮助。远的。
代码:
Sub sendEmail(EmailSubject As String, EMailSendTo As String, EMailBody As String, MailServer as String) Dim objNotesSession As Object Dim objNotesMailFile As Object Dim objNotesDocument As Object Dim objNotesField As Object Dim sendmail As Boolean 'added for integration into reporting tool Dim dbString As String dbString = "mail\" & Application.UserName & ".nsf" On Error GoTo SendMailError 'Establish Connection to Notes Set objNotesSession = CreateObject("Notes.NotesSession") On Error Resume Next 'Establish Connection to Mail File Set objNotesMailFile = objNotesSession.GETDATABASE(MailServer, dbString) 'Open Mail objNotesMailFile.OPENMAIL On Error GoTo 0 'Create New Memo Set objNotesDocument = objNotesMailFile.createdocument Dim oWorkSpace As Object, oUIdoc As Object Set oWorkSpace = CreateObject("Notes.NotesUIWorkspace") Set oUIdoc = oWorkSpace.CurrentDocument 'Create 'Subject Field' Set objNotesField = objNotesDocument.APPENDITEMVALUE("Subject", EmailSubject) 'Create 'Send To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("SendTo", EMailSendTo) 'Create 'Copy To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("CopyTo", EMailCCTo) 'Create 'Blind Copy To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("BlindCopyTo", EMailBCCTo) 'Create 'Body' of memo Set objNotesField = objNotesDocument.CREATERICHTEXTITEM("Body") With objNotesField .APPENDTEXT emailBody .ADDNEWLINE 1 End With 'Send the e-mail Call objNotesDocument.Save(True, False, False) objNotesDocument.SaveMessageOnSend = True 'objNotesDocument.Save objNotesDocument.Send (0) 'Release storage Set objNotesSession = Nothing Set objNotesMailFile = Nothing Set objNotesDocument = Nothing Set objNotesField = Nothing 'Set return code sendmail = True Exit Sub SendMailError: Dim Msg Msg = "Error # " & Str(Err.Number) & " was generated by " _ & Err.Source & Chr(13) & Err.Description MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext sendmail = False End Sub
I have little Lotus Script or Notes/Domino knowledge but I have a procedure, copied from somewhere a long time ago, that allows me to email through Notes from VBA. I normally only use this for internal notifications where the formatting hasn't really mattered.
I now want to use this to send external emails to a client, and corporate types would rather the email complied with our style guide (a sans-serif typeface basically).
I was about to tell them that the code only works with plain text, but then I noticed that the routine does reference some sort of CREATERICHTEXTITEM object. Does this mean I could apply some sort of formatting to the body text string after it has been passed to the mail routine? As well as upholding our precious brand values, this would be quite handy to me for highlighting certain passages in the email.
I've had a dig about the 'net to see if this code could be adapted, but being unfamiliar with Notes' object model, and the fact that online Notes resources seem to mirror the application's own obtuseness, meant I didn't get very far.
The code:
Sub sendEmail(EmailSubject As String, EMailSendTo As String, EMailBody As String, MailServer as String) Dim objNotesSession As Object Dim objNotesMailFile As Object Dim objNotesDocument As Object Dim objNotesField As Object Dim sendmail As Boolean 'added for integration into reporting tool Dim dbString As String dbString = "mail\" & Application.UserName & ".nsf" On Error GoTo SendMailError 'Establish Connection to Notes Set objNotesSession = CreateObject("Notes.NotesSession") On Error Resume Next 'Establish Connection to Mail File Set objNotesMailFile = objNotesSession.GETDATABASE(MailServer, dbString) 'Open Mail objNotesMailFile.OPENMAIL On Error GoTo 0 'Create New Memo Set objNotesDocument = objNotesMailFile.createdocument Dim oWorkSpace As Object, oUIdoc As Object Set oWorkSpace = CreateObject("Notes.NotesUIWorkspace") Set oUIdoc = oWorkSpace.CurrentDocument 'Create 'Subject Field' Set objNotesField = objNotesDocument.APPENDITEMVALUE("Subject", EmailSubject) 'Create 'Send To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("SendTo", EMailSendTo) 'Create 'Copy To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("CopyTo", EMailCCTo) 'Create 'Blind Copy To' Field Set objNotesField = objNotesDocument.APPENDITEMVALUE("BlindCopyTo", EMailBCCTo) 'Create 'Body' of memo Set objNotesField = objNotesDocument.CREATERICHTEXTITEM("Body") With objNotesField .APPENDTEXT emailBody .ADDNEWLINE 1 End With 'Send the e-mail Call objNotesDocument.Save(True, False, False) objNotesDocument.SaveMessageOnSend = True 'objNotesDocument.Save objNotesDocument.Send (0) 'Release storage Set objNotesSession = Nothing Set objNotesMailFile = Nothing Set objNotesDocument = Nothing Set objNotesField = Nothing 'Set return code sendmail = True Exit Sub SendMailError: Dim Msg Msg = "Error # " & Str(Err.Number) & " was generated by " _ & Err.Source & Chr(13) & Err.Description MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext sendmail = False End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答是肯定的。 冗长的答案令人痛苦。 没有任何优秀的类可以用来操作 Notes 中的富文本项目。 不过,您可以研究的一些内容包括 NotesRichTextStyle、NotesRichTextParagraphStyle 和 NotesRichTextTable 等。 这些类可帮助您定义一些富文本元素并以编程方式将它们添加到富文本字段中。
由于您要发送电子邮件,另一种方法是使用 NotesMIMEEntity 类并使用 HTML 构建电子邮件(方式更简单)。 下面是一些示例代码:
本质上,您需要在 NotesSession 中关闭 ConvertMIME 设置。 然后创建一个新文档,设置邮件属性等。该部分已经在您的 VBA 代码中。 接下来,创建 MIMEEntity 和一个流来保存 HTML 文本。 最后调用 MIMEEntity 对象上的 SetContentFromText 方法。 然后发送您的电子邮件。 请注意,最后一次调用是为 NotesSession 重新打开 ConvertMIME 功能。
我不确定这是否都可以通过 COM 工作,但它在 Notes 中的 LotusScript 代理中工作得很好。
我希望这有帮助!
The short answer is Yes. The long answer is painful. There aren't any great classes exposed to manipulate rich text items in Notes. However a few that you can research are the NotesRichTextStyle, NotesRichTextParagraphStyle, and NotesRichTextTable to name a few. These classes help you define some rich text elements and add them programmatically to your rich text field.
Another approach, since you're sending email, is to use the NotesMIMEEntity classes and build the email using HTML (way way easier). Here's some sample code:
Essentially you'll need to turn the ConvertMIME setting off in the NotesSession. Then create a new document, set the mail properties, etc. That part is already in your VBA code. Next, create the MIMEEntity and a stream to hold your HTML text. Finally call the SetContentFromText method on your MIMEEntity object. Then send your email. Note the last call is to turn the ConvertMIME feature back on for the NotesSession.
I'm not sure if this will all work via COM, but it does work well in LotusScript agents in Notes.
I hope this helps!
要更改其发件人,请创建通用 Notes ID(例如自动发送/YourCompany)并使用该 ID 保存代理。
To change who it's from, create a generic Notes ID (eg Auto Send/YourCompany) and save the agent with that ID.
我喜欢它本来的样子。 然而我不得不改变
dbString = "邮件\" & Application.UserName & “.nsf”
到
dbString = "邮件\" & Application.CurrentUser 和 “.nsf”
第一条评论:我希望在发送时不必打开 Lotus Notes。
第二条评论:我希望我可以更改电子邮件的发件人(即,如果我要向 50 个人发送报告,我希望它来自通用地址,而不是我的工作地址)
I like it just the way it is. However, I had to change
dbString = "mail\" & Application.UserName & ".nsf"
to
dbString = "mail\" & Application.CurrentUser & ".nsf"
First comment: I wish I didn't have to have Lotus Notes open at the time of sending.
Second comment: I wish I could change who the e-mail is from (ie, if I'm sending a report to 50 people, I want it to be from a generic address, rather than my work address)