簡體   English   中英

玩VBA通過Lotus Notes發送電子郵件

[英]Playing with vba sending email through lotus notes

我已經在整個Web或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 Task Scheduler調用了VBS,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 

這里有幾件事:

  • 如果您首先可以在Domino Designer中編寫代碼(即,將Notes客戶端和Domino Designer客戶端安裝在計算機上),這將變得更加容易。 目前,您正在使用Notes作為COM服務器。 最大的缺點是,如果出現故障,您幾乎沒有調試信息。 首先在LotusScript中編寫代碼,然后將其移植到VBS(它們與BASIC的方言非常相似)。

  • 您可以創建一個Notes RichText電子郵件(這是使用CreateRichTextItem進行的操作)。 您可以使用不同的方法來操作RichTextItem,其中最重要的方法是NotesRichTextStyle,您必須將其視為“格式化的位,之后將改變所有內容”。 您需要創建NotesRichTextStyle對象,對其進行配置(即,字體,粗體等)並將其插入到RTF文本字段中。 如果聽起來很笨拙,那是因為。

     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電子郵件,但它充其量只是一件很麻煩的事,您需要等待幾天的痛苦步驟才能起作用,為此,您確實需要經驗豐富的專業人員。 這是一個好的開始: 其他Stackoverflow問題

  • 您引用用戶郵件引用的方式很糟糕。 它經過硬編碼,並且僅適用於該特定數據庫,即使有問題的人更改了姓名,例如。 這樣更好:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM