在电子邮件 Lotus Notes 中添加 HTML 签名。

huangapple go评论70阅读模式
英文:

Add HTML signature in email Lotus Notes

问题

I am currently struggling with adding my HTML signature into an email with VBA. I have set up Lotus Notes to add the signature automatically, and it works when I write a new message manually. But when I am using the macro, I only see the path of the signature file (e.g., C:\User1\Email signatures - 2023\AF.html). Here is the code I am using:

'Public Sub SendNotesMail(Subject as string, attachment as string,
'recipient as string, bodytext as string, saveit as Boolean)
' This public sub will send a mail and attachment if necessary to the
' recipient including the body text.
'Requires that Notes client is installed on the system.
Public Sub SendNotesMail(Subject As String, Attachment As String, Attachment1 As String, Recipient As String, ccRecipient1 As String, ccRecipient2 As String, ccRecipient3 As String, ccRecipient4 As String, BodyText As String, SaveIt As Boolean)
'Set up the objects required for Automation into Lotus Notes
    Dim Maildb As Object 'The mail database
    Dim UserName As String 'The current user's Notes name
    Dim MailDbName As String 'The current user's Notes mail database name
    Dim MailDoc As Object 'The mail document itself
    Dim AttachME As Object 'The attachment RichTextFile object
    Dim Session As Object 'The Notes session
    Dim EmbedObj As Object 'The embedded object (Attachment)
    'Start a session to Notes
    Set Session = CreateObject("Notes.NotesSession")
    'Next line only works with 5.x and above. Replace the password with your password
    'Get the session's username and then calculate the mail file name
    'You may or may not need this as for MailDBname with some systems you
    'can pass an empty string or using the above password, you can use other mailboxes.
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
    'Open the mail database in Notes
    Set Maildb = Session.GETDATABASE("", MailDbName)
     If Maildb.IsOpen = True Then
          'Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    
    signature = Maildb.GETPROFILEDOCUMENT("CalendarProfile").GETITEMVALUE("Signature")(0)
    
    'Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = "Memo"
    MailDoc.sendto = Recipient
    MailDoc.CopyTo = Array(ccRecipient1, ccRecipient2, ccRecipient3, ccRecipient4)
    MailDoc.Subject = Subject
    MailDoc.body = BodyText & vbNewLine & vbNewLine & signature
    MailDoc.SAVEMESSAGEONSEND = SaveIt
    'Set up the embedded object and attachment and attach it
    If Attachment <> "" Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment, "Attachment")
        MailDoc.CREATERICHTEXTITEM ("Attchment")
    End If
    Set EmbedObj = Nothing
    If Attachment1 <> "" Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment1")
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", Attachment1, "Attachment1")
        MailDoc.CREATERICHTEXTITEM ("Attchment1")
    End If
    'Send the document
    MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
    MailDoc.send 0, Recipient
    'Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
    MsgBox "Email inviata con successo."
End Sub

Any help would be appreciated.

英文:

i am currently struggling with adding my html signature into an email with vba. I have set up Lotus notes to add the signature automatically and it works when i write a new message manually but when i am using the macro i only see the path of the signature file (ex. C:\User1\Email signatures - 2023\AF.html). Here is the code i am using :

&#39;Public Sub SendNotesMail(Subject as string, attachment as string,
&#39;recipient as string, bodytext as string,saveit as Boolean)
&#39;This public sub will send a mail and attachment if neccessary to the
&#39;recipient including the body text.
&#39;Requires that notes client is installed on the system.
Public Sub SendNotesMail(Subject As String, Attachment As String, Attachment1 As String, Recipient As String, ccRecipient1 As String, ccRecipient2 As String, ccRecipient3 As String, ccRecipient4 As String, BodyText As String, SaveIt As Boolean)
&#39;Set up the objects required for Automation into lotus notes
    Dim Maildb As Object &#39;The mail database
    Dim UserName As String &#39;The current users notes name
    Dim MailDbName As String &#39;THe current users notes mail database name
    Dim MailDoc As Object &#39;The mail document itself
    Dim AttachME As Object &#39;The attachment richtextfile object
    Dim Session As Object &#39;The notes session
    Dim EmbedObj As Object &#39;The embedded object (Attachment)
    &#39;Start a session to notes
    Set Session = CreateObject(&quot;Notes.NotesSession&quot;)
    &#39;Next line only works with 5.x and above. Replace password with your password
    &#39;Get the sessions username and then calculate the mail file name
    &#39;You may or may not need this as for MailDBname with some systems you
    &#39;can pass an empty string or using above password you can use other mailboxes.
    UserName = Session.UserName
    MailDbName = Left$(UserName, 1) &amp; Right$(UserName, (Len(UserName) - InStr(1, UserName, &quot; &quot;))) &amp; &quot;.nsf&quot;
    &#39;Open the mail database in notes
    Set Maildb = Session.GETDATABASE(&quot;&quot;, MailDbName)
     If Maildb.IsOpen = True Then
          &#39;Already open for mail
     Else
         Maildb.OPENMAIL
     End If
    
    signature = Maildb.GETPROFILEDOCUMENT(&quot;CalendarProfile&quot;).GETITEMVALUE(&quot;Signature&quot;)(0)
    
    &#39;Set up the new mail document
    Set MailDoc = Maildb.CREATEDOCUMENT
    MailDoc.Form = &quot;Memo&quot;
    MailDoc.sendto = Recipient
    MailDoc.CopyTo = Array(ccRecipient1, ccRecipient2, ccRecipient3, ccRecipient4)
    MailDoc.Subject = Subject
    MailDoc.body = BodyText &amp; vbNewLine &amp; vbNewLine &amp; signature
    MailDoc.SAVEMESSAGEONSEND = SaveIt
     &#39;Set up the embedded object and attachment and attach it
    If Attachment &lt;&gt; &quot;&quot; Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM(&quot;Attachment&quot;)
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, &quot;&quot;, Attachment, &quot;Attachment&quot;)
        MailDoc.CREATERICHTEXTITEM (&quot;Attchment&quot;)
    End If
    Set EmbedObj = Nothing
    If Attachment1 &lt;&gt; &quot;&quot; Then
        Set AttachME = MailDoc.CREATERICHTEXTITEM(&quot;Attachment1&quot;)
        Set EmbedObj = AttachME.EMBEDOBJECT(1454, &quot;&quot;, Attachment1, &quot;Attachment1&quot;)
        MailDoc.CREATERICHTEXTITEM (&quot;Attchment1&quot;)
    End If
    
    &#39;Send the document
    MailDoc.PostedDate = Now() &#39;Gets the mail to appear in the sent items folder
    MailDoc.send 0, Recipient
    &#39;Clean Up
    Set Maildb = Nothing
    Set MailDoc = Nothing
    Set Session = Nothing
    Set EmbedObj = Nothing
    
MsgBox &quot;Email inviata con successo.&quot;

End Sub

Any help would be appreciated.

答案1

得分: 3

以下是您提供的代码的中文翻译部分:

首先:请使用正确的代码来打开用户的邮件文件。以下部分大多是无意义的:

MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
' 在Notes中打开邮件数据库
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
    ' 已经为邮件打开
Else
    Maildb.OPENMAIL
End If

只需替换为:

Set Maildb = Session.GETDATABASE("", "")
Maildb.OPENMAIL

其次:邮件中的附件应该放在“Body”富文本项中,而不是名为“Attachment”或“Attachment1”的项中。

现在针对您的问题:
在日历配置文件的“Signature”项中,只包含到HTML文件的路径。您的代码只是将该路径添加到正文字段中。但是,您需要将HTML文件导入到正文中。不幸的是,导入只能在称为NotesUIDocument的前端UI元素中进行,而不能像您目前所做的那样在后端进行。

为了修复这个问题,您需要:

  • 将您刚刚创建的后端NotesDocument传递到前端(Notes客户端需要打开)
  • 将光标设置到“Body”项
  • 导入签名文件
  • 发送前端文档
  • 保存前端文档
  • 关闭前端文档

代码如下:

Dim body As Object '邮件正文
MailDoc.Subject = Subject
Set Body = MailDoc.CREATERICHTEXTITEM("Body")
Body.AppendText(BodyText)
Body.AddNewline(2)
MailDoc.SAVEMESSAGEONSEND = SaveIt

' 设置嵌入对象和附件并附加它们
If Attachment <> "" Then
    Body.EMBEDOBJECT(1454, "", Attachment, "Attachment")
End If
If Attachment1 <> "" Then
    Body.EMBEDOBJECT(1454, "", Attachment1, "Attachment1")
End If

' 创建UI元素
Dim Workspace As Object
Dim uidoc As Object
Set Workspace = CreateObject("Notes.NotesUIWorkspace")
' 发送文档
Set uidoc = Workspace.EditDocument(True, MailDoc)
uidoc.GotoField("Body")
uidoc.Import("HTML文件", signature)
uidoc.Send()
uidoc.Save()
uidoc.document.SaveOptions = "0"
uidoc.Close(True)

请注意,此代码未经测试,可能需要进一步调整,并可能包含拼写错误。

英文:

First of all: PLEASE use the right code to open the mailfile of the user. This is mostly nonsense:

MailDbName = Left$(UserName, 1) &amp; Right$(UserName, (Len(UserName) - InStr(1, UserName, &quot; &quot;))) &amp; &quot;.nsf&quot;
&#39;Open the mail database in notes
Set Maildb = Session.GETDATABASE(&quot;&quot;, MailDbName)
 If Maildb.IsOpen = True Then
      &#39;Already open for mail
 Else
     Maildb.OPENMAIL
 End If

Just replace it with

Set Maildb = Session.GETDATABASE(&quot;&quot;, &quot;&quot;)
Maildb.OPENMAIL

Second: Attachments in Mails go to the "Body" richtext-item not to one called "Attachment" or "Attachment1"...

Now to your problem:
In the item "Signature" in the calendar profile there is only the path to the html file. Your code simply adds that path to the body field. But you need to import the html file to the body. Unfortunately importing is only possible in a frontend UI element called NotesUIDocument and NOT in the backend as you do it at the moment.

To fix that you need to:

  • bring the backend NotesDocument that you just created to the frontend (Notes Client needs to be open for that)
  • set the cursor to the "Body" item
  • Import the signature file
  • send the frontend document
  • save the frontend document
  • close the frontend document

This looks like this:

Dim body As Object &#39;The body of the mail
MailDoc.Subject = Subject
Set Body = MailDoc.CREATERICHTEXTITEM(&quot;Body&quot;)
Body.AppendText( BodyText )
Body.AddNewline(2)
MailDoc.SAVEMESSAGEONSEND = SaveIt
 &#39;Set up the embedded object and attachment and attach it
If Attachment &lt;&gt; &quot;&quot; Then
    Body.EMBEDOBJECT(1454, &quot;&quot;, Attachment, &quot;Attachment&quot;)
End If
If Attachment1 &lt;&gt; &quot;&quot; Then
    Body.EMBEDOBJECT(1454, &quot;&quot;, Attachment1, &quot;Attachment1&quot;)
End If

&#39;Create the UI Element
Dim Workspace As Object
Dim uidoc as Object
Set Workspace = CreateObject(&quot;Notes.NotesUIWorkspace&quot;)
&#39;Send the document
Set uidoc = Workspace.EditDocument( True, MailDoc )
uidoc.GotoField( &quot;Body&quot; )
uidoc.Import(&quot;HTML File&quot;, signature)
uidoc.Send()
uidoc.Save()
uidoc.document.SaveOptions = &quot;0&quot;
uidoc.Close(True)

Code is untested, might need some more tweeking and might contain typos.

huangapple
  • 本文由 发表于 2023年4月13日 19:19:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定