简体   繁体   English

玩VBA通过Lotus Notes发送电子邮件

[英]Playing with vba sending email through lotus notes

I have already seen a lot of posts related to that, both on the web overall or on stackoverflow. 我已经在整个Web或stackoverflow上看到了很多与此相关的帖子。 However, I didn't see people making too much changes and really playing with this macro. 但是,我没有看到人们进行太多更改并真正使用此宏。

Ok, it's possible to send emails through lotus notes using VBA, but how can I make these emails cooler? 好的,可以使用VBA通过Lotus Notes发送电子邮件,但是如何使这些电子邮件更酷? Change font format or color for example? 例如更改字体格式或颜色? Change the styles, choosing to insert picture as a link or embedding it? 更改样式,选择插入图片作为链接还是将其嵌入?

Well, this is what I got so far by searching around and making a few changes: 好吧,这是到目前为止我通过搜索并进行一些更改得到的结果:

Sub SendEmail(Subject, Text)

Dim Maildb As Object
Dim mailDoc As Object
Dim body As Object
Dim session As Object
'Start a session to notes
Set session = CreateObject("Lotus.NotesSession")
'This line prompts for password of current ID noted in Notes.INI
'Call session.Initialize 
Call session.Initialize("Your Password Here")
'Open the mail database in notes
Set Maildb = session.GetDatabase("Mail Server", "mail\    .nsf")
If Not Maildb.IsOpen = True Then
Call Maildb.Open
End If
'Create the mail document
Set mailDoc = Maildb.CreateDocument
Call mailDoc.ReplaceItemValue("Form", "Memo")
'Set the recipient (you can write the name of a list you saved in your Lotus Notes)
Call mailDoc.ReplaceItemValue("SendTo", "email1@email.com.br")
'Set subject
Call mailDoc.ReplaceItemValue("Subject", Subject)
'Create and set the Body content
Set body = mailDoc.CreateRichTextItem("Body")
Call body.AppendText(Text)
'Example to create an attachment (optional)
Call body.AddNewLine(2)
'Insert an pdf attached
Call body.EmbedObject(1453, "", "C:\Desktop\Test.pdf")
Call body.AddNewLine(2) 'add line to separate text
'Message in the end of the email
Call body.AppendText("This is an automatic message.")
'Example to save the message (optional)
mailDoc.SaveMessageOnSend = True
'Send the document
'Gets the mail to appear in the Sent items folder
Call mailDoc.ReplaceItemValue("PostedDate", Now())
Call mailDoc.send(False)
'Clean Up
Set Maildb = Nothing
Set mailDoc = Nothing
Set body = Nothing
Set session = Nothing

End Sub

Btw, I use the Windows Task Scheduler to call a VBS which will then call a macro that will call the macro to send email with a specific subject and text. 顺便说一句,我使用Windows Task Scheduler调用了VBS,VBS随后将调用一个宏,该宏将调用该宏以发送带有特定主题和文本的电子邮件。 As I have several macros that generates emails and each one has its subject and text, I thought this would be better. 由于我有几个生成电子邮件的宏,每个宏都有其主题和文本,因此我认为这样做会更好。

This is the vbs (this is probably useless and everyone knows here, but I will share anyway): 这就是vbs(这可能没什么用,每个人都知道,但是我还是会分享):

'Run VBA Using VBS
Option Explicit

On Error Resume Next

ExcelMacroExample

Sub ExcelMacroExample() 

    Dim xlApp 
    Dim xlBook 

    Set xlApp = CreateObject("Excel.Application") 
    Set xlBook = xlApp.Workbooks.Open("C:\Desktop\Macros.xlsm") 'Excel filename
    xlApp.Run "SendEmail" 'Excel macro name
    xlApp.Quit 

    Set xlBook = Nothing 
    Set xlApp = Nothing 

End Sub 

A couple of things here: 这里有几件事:

  • It's going to be easier if you can first write the code in Domino Designer (ie get a machine installed with the Notes Client and the Domino Designer Client). 如果您首先可以在Domino Designer中编写代码(即,将Notes客户端和Domino Designer客户端安装在计算机上),这将变得更加容易。 At the moment you are using Notes as a COM Server. 目前,您正在使用Notes作为COM服务器。 The big disadvantage is that you have almost no debugging information if something fails. 最大的缺点是,如果出现故障,您几乎没有调试信息。 Write the code first in LotusScript, then port it to VBS (they are very similar dialects of BASIC). 首先在LotusScript中编写代码,然后将其移植到VBS(它们与BASIC的方言非常相似)。

  • You can create a Notes RichText E-Mail (this is what you are doing now with CreateRichTextItem). 您可以创建一个Notes RichText电子邮件(这是使用CreateRichTextItem进行的操作)。 You can manipulate the RichTextItem with different methods, the most important one being NotesRichTextStyle, which you have to think of as 'bits of formatting that will change everything afterwards'. 您可以使用不同的方法来操作RichTextItem,其中最重要的方法是NotesRichTextStyle,您必须将其视为“格式化的位,之后将改变所有内容”。 You need to create the NotesRichTextStyle Object, configure it (ie font, bold, etc) and insert it into the rich text field. 您需要创建NotesRichTextStyle对象,对其进行配置(即,字体,粗体等)并将其插入到RTF文本字段中。 If this sounds klunky, that's because it is. 如果听起来很笨拙,那是因为。

     Dim db As NotesDatabase Dim session As New NotesSession Set db = session.CurrentDatabas Dim doc As New NotesDocument(db) Call doc.AppendItemValue("From", session.UserName) Call doc.AppendItemValue("Subject", _ "Meeting time changed") Dim richStyle As NotesRichTextStyle Set richStyle = session.CreateRichTextStyle Dim richText As New NotesRichTextItem(doc, "Body") Call richText.AppendText("The meeting is at ") richStyle.Bold = True Call richText.AppendStyle(richStyle) Call richText.AppendText("3:00") richStyle.Bold = False Call richText.AppendStyle(richStyle) Call richText.AppendText(" not 2:00") Call doc.Save(True, False) 
  • If you want even more control, then you can create an HTML E-Mail wrapped in Mime, but it's fiddly at best and you're looking at several days of painful steps until it works, and you really would need an experienced professional for this. 如果您想要更多的控制权,则可以创建一个用Mime包装的HTML电子邮件,但它充其量只是一件很麻烦的事,您需要等待几天的痛苦步骤才能起作用,为此,您确实需要经验丰富的专业人员。 This is a good start: other Stackoverflow question 这是一个好的开始: 其他Stackoverflow问题

  • The way you're referencing the user's mail reference is horrible. 您引用用户邮件引用的方式很糟糕。 It's hardcoded and will only ever work for that one particular database, even if the person in question changes name, for instance. 它经过硬编码,并且仅适用于该特定数据库,即使有问题的人更改了姓名,例如。 This is much better: 这样更好:

     Dim db As New NotesDatabase( "", "" ) Call db.OpenMail 

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

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