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

bcs8qyzn  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(167)

我目前正在努力添加我的html签名到电子邮件与vba.我已经设置了Lotus notes自动添加签名,当我手动写一封新邮件时,它可以工作,但当我使用宏我只看到签名文件的路径(例如. C:\User1\Email signatures - 2023\AF.html).下面是我使用的代码:

'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 neccessary 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 users notes name
    Dim MailDbName As String 'THe current users 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 password with your password
    'Get the sessions 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 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

任何帮助将不胜感激。

kmpatx3s

kmpatx3s1#

首先:请使用正确的代码打开用户的邮件文件。这大多是胡说八道:

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

只要把它换成

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

第二:邮件中的附件转到“正文”文本项,而不是“附件”或“附件1”...
现在说说你的问题:在日历配置文件的“Signature”项中,只有html文件的路径。您的代码只是将该路径添加到body字段。但您需要将html文件导入到body。不幸的是,导入只能在前端UI元素NotesUIDo中进行,而不能在后端进行,因为您现在正在这样做。
要解决这个问题,您需要:

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

这看起来像这样:

Dim body As Object 'The body of the mail
MailDoc.Subject = Subject
Set Body = MailDoc.CREATERICHTEXTITEM("Body")
Body.AppendText( BodyText )
Body.AddNewline(2)
MailDoc.SAVEMESSAGEONSEND = SaveIt
 'Set up the embedded object and attachment and attach it
If Attachment <> "" Then
    Body.EMBEDOBJECT(1454, "", Attachment, "Attachment")
End If
If Attachment1 <> "" Then
    Body.EMBEDOBJECT(1454, "", Attachment1, "Attachment1")
End If

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

代码未经测试,可能需要更多的tweeking,可能包含错别字。

相关问题