2014-10-09 219 views
0

我已经看到很多帖子都涉及到,无论是在整个网络上还是在stackoverflow上。但是,我没有看到人们做出太多改变,并且真的在玩这个宏。使用vba通过莲花笔记发送电子邮件

好的,可以通过使用VBA的莲花笔记发送电子邮件,但是我怎样才能让这些电子邮件更酷?例如更改字体格式或颜色?更改样式,选择插入图片作为链接或嵌入它?

好了,这是我走到这一步,通过摸索和作出一些改变:

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", "[email protected]") 
'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

你或许应该问这个作为一个问题显示*基本*代码,并提供*你*的回答(用更新/修改后的代码及说明)作为*答案*这个问题。干杯。 – 2014-10-09 18:27:18

回答

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) 
    
  • 如果你想要更多的控制,那么你可以创建一个HTML电子邮件裹在MIME,但它是繁琐的,在最好的,你看着痛苦的步骤数天,直到它的工作原理,以及您真的需要一个经验丰富的专业人员。这是一个好的开始:other Stackoverflow question

  • 你引用用户的邮件引用的方式是可怕的。它是硬编码的,并且只会对那个特定的数据库有效,例如,即使有人在更改名称。这是好多了:

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