2014-08-29 752 views
1

我使用此VBA代碼發送電子郵件,當用戶單擊一行中的單元格。如何在Excel VBA電子郵件中添加簽名?

我想添加一個帶有圖像的簽名到我的電子郵件正文。我怎樣才能修改我的代碼來放置它?

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

If Target.Column = Range("BL1").Column Then 

    If Target.Row > 7 And Target.Value = "Take Action" Then 

     Set OutApp = CreateObject("Outlook.Application") 
     Set OutMail = OutApp.CreateItem(0) 

     strbody = "<p style='font-family:calibri;font-size:16'>" & "Dear Sirs," & "<br><br>" & vbNewLine & vbNewLine & _ 
       "F.A.O: " & "<b>" & Range("B" & ActiveCell.Row) & "</b>" & "," & vbNewLine & vbNewLine & _ 
       "<br>" & "This is an urgent update on the status of your account." & "<br>" & vbNewLine & _ 
       "Our records show that your insurance is due to expire on: " & "<b>" & Format(Range("BE" & ActiveCell.Row), "dd" & " Mmmm " & "yyyy") & "." & "</b>" & " To ensure that you remain active on our systems as an approved Hewden Stuart Ltd Supplier, it is important that you provide us with the details of your renewed insurance policy. Please can you provide us with these details for the following insurance as soon as possible, in order to remain active on our systems:" & vbNewLine & vbNewLine & _ 
       "<br><br>" & "Insurance: " & "<b>" & "{Insurance Type Goes Here}" & "</b>" & "<br>" & vbNewLine & _ 
       "Due for Period: " & "<b>" & Format(Range("BE" & ActiveCell.Row), "dd" & " Mmmm " & "yyyy") & "</b>" & " - " & "<b>" & Format(Range("BE" & ActiveCell.Row) + 365, "dd" & " Mmmm " & "yyyy") & "</b>" & vbNewLine & vbNewLine & _ 
       "<br><br>" & "Note:" & "<br>" & vbNewLine & _ 
       "Please ensure that the above information is provided by your insurance broker, or your insurer, in the form of a standard letter or certificate. If your insurance is in the name of a parent company, please provide a breakdown of the companies covered from your insurer. In order to provide us with the above information, please login to your Control Panel with your unique Username and Password and attach your documents. Regrettably, failure to provide us with the information requested will result in suspension of your account. If you have any queries, please email us at [email protected]" & vbNewLine & vbNewLine & _ 
       "<br><br>" & "Your Reference:" & "<br><br>" & vbNewLine & vbNewLine & _ 
       "<b>" & Range("AB" & ActiveCell.Row) & "</b>" & vbNewLine & _ 
       "<p style='font-family:calibri;font-size:13'>" & "Please quote your unique Supplier Reference number when providing us with any insurance documents and in the even that you should have any enquiries." & "</p>" & vbNewLine & vbNewLine & _ 
       "<p style='font-family:calibri;font-size:16'>" & "<br>" & "Kind Regards," & "<br><br>" & vbNewLine & vbNewLine & _ 
       "<b>" & "Hewden Supply Chain Department" & "</b>" & "</P>" 

     With OutMail 
      .SentOnBehalfOfName = "[email protected]" 
      .To = "mark.o'[email protected]" 
      .CC = "" 
      .BCC = "" 
      .Subject = "Important! - Insurance Alert!" 
      .HTMLbody = strbody 
      .Attachments.Add ("P:\cover.jpg") 
      .Send 'or use .Display 
     End With 

    End If 

End If 

End Sub 
+0

我已經澄清了這個問題的答案。 – 2014-09-05 11:46:29

回答

1

在Outlook 2007 工具 - 選項 - 郵件格式選項卡 按住CNTRL鍵和簽名

這裏簽名保持打開的文件夾和路徑和文件名,點擊會顯示出來。這將是用於調用下面的GetSignature函數的路徑和文件名。

Function GetSignature(fPath As String) As String 
    Dim fso As Object 
    Dim TSet As Object 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set TSet = fso.GetFile(fPath).OpenAsTextStream(1, -2) 
    GetSignature= TSet.readall 
    TSet.Close 
End Function 

在您的發送電子郵件代碼中,聲明一個字符串變量來存放從GetSignature函數返回的簽名文本。一旦設置,那麼該字符串就需要被附加到電子郵件正文的末尾。

Dim StrSignature As String 
StrSignature = GetSignature(sPath) 

.HTMLbody = strbody & vbNewLine & vbNewLine & StrSignature 
5

Outlook將簽署新的未修改的消息(請不要修改此之前,身體)當你調用MailItem.Display當你訪問MailItem.GetInspector屬性(這將導致該消息被顯示在屏幕上)或 - 您不必對返回的Inspector對象執行任何操作,但Outlook將使用簽名填充消息正文。

一旦添加了簽名,請閱讀HTMLBody屬性並將其與您嘗試設置的HTML字符串合併。請注意,您不能簡單地連接2個HTML字符串 - 需要合併字符串。例如。如果要將字符串插入HTML正文的頂部,請查找<body子字符串,然後找到下一個出現的>(這將處理具有屬性的<body>元素),然後在該>之後插入HTML字符串。嵌入式圖像附件和樣式必須分開處理。

在一般情況下,簽名的名稱存儲在可通過IOlkAccountManager擴展MAPI界面訪問的帳戶配置文件數據中。由於該接口是擴展MAPI,因此只能使用C++或Delphi進行訪問。如果單擊IOlkAccountManager按鈕,則可以在OutlookSpy中看到界面及其數據。

Outlook對象模型不公開簽名或訪問帳戶的任意屬性。

如果使用Redemption是一個選項,您可以使用它的RDOAccount對象(可用任何語言訪問,包括VBA)。新消息簽名名稱存儲在0x0016001F屬性中,回覆簽名位於0x0017001F。您也可以使用RDOAccountReplySignatureNewSignature屬性。
所有Outlook簽名都通過RDOSession.Signatures集合公開。
使用RDOSignature.ApplyTo可以將簽名應用於郵件 - 它將負責正確合併數據並引入嵌入式圖像附件和樣式。

編輯:夏季2017年,只有MailItem.Display插在Outlook 2016 MailItem.GetInspector簽名不這樣做了。

0

有同樣的問題,尤其是當使用HTMLbody。如果不使用這個的:

With OutMail 
    .SentOnBehalfOfName = "[email protected]" 
    .To = "mark.o'[email protected]" 
    .CC = "" 
    .BCC = "" 
    .Subject = "Important! - Insurance Alert!" 
    .HTMLbody = strbody 
    .Attachments.Add ("P:\cover.jpg") 
    .Send 'or use .Display 
    End With* 

你應該這樣做:

With OutMail 
    .SentOnBehalfOfName = "[email protected]" 
    .To = "mark.o'[email protected]" 
    .CC = "" 
    .BCC = "" 
    .Subject = "Important! - Insurance Alert!" 
    ''''' part that is missing 
    . Display 
    End with 
    With OutMail 
    ''''' 
    .HTMLbody = strbody & vbNewLine & .HTMLBody ' add the signature without losing the HTML-formatting of the signature 
    .Attachments.Add ("P:\cover.jpg") 
    .Send 'or use .Display 
    End With 
+0

您不能簡單地連接兩個HTML字符串 - 請參閱我的答案。 – 2017-02-28 20:21:37

相關問題