玩vba通过莲花笔记发送电子邮件

时间:2014-10-09 18:06:48

标签: vba email excel-vba lotus-notes excel

我已经看过很多与此相关的帖子,无论是在整体网络还是在stackoverflow上。但是,我没有看到人们做出太多变化,真的玩这个宏。

好的,可以使用VBA通过Lotus Notes发送电子邮件,但是如何让这些电子邮件更酷?例如更改字体格式或颜色?更改样式,选择将图片作为链接插入还是嵌入?

嗯,这是我到目前为止通过搜索并做出一些改变而得到的:

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

顺便说一句,我使用Windows任务计划程序调用VBS,然后调用一个宏来调用宏来发送带有特定主题和文本的电子邮件。由于我有几个生成电子邮件的宏,每个都有主题和文本,我认为这会更好。

这是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 

1 个答案:

答案 0 :(得分:2)

这里有几件事:

  • 如果您可以先在Domino Designer中编写代码(即使用Notes客户端和Domino Designer客户端安装机器),这将会更容易。目前您正在将Notes用作COM服务器。最大的缺点是,如果出现故障,你几乎没有调试信息。首先在LotusScript中编写代码,然后将其移植到VBS(它们是非常相似的BASIC方言)。

  • 您可以创建Notes RichText电子邮件(这是您现在使用CreateRichTextItem执行的操作)。您可以使用不同的方法操作RichTextItem,其中最重要的是NotesRichTextStyle,您必须将其视为“后续更改所有内容的格式化位置”。您需要创建NotesRichTextStyle对象,配置它(即字体,粗体等)并将其插入到富文本字段中。如果这听起来很笨拙,那是因为它是。

    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)
    
  • 如果你想要更多的控制权,那么你可以创建一个包含在Mime中的HTML电子邮件,但它充其量只是繁琐而你正在寻找几天的痛苦步骤直到它起作用,你真的会需要一个经验丰富的专业人士。这是一个好的开始:other Stackoverflow question

  • 您引用用户邮件引用的方式非常糟糕。它是硬编码的,并且只能用于那个特定的数据库,例如,即使有问题的人改变了名称。这要好得多:

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