2012-12-17 136 views
2

我想建立\編輯郵件signiture在Excel中:如何添加Excel範圍爲圖片到Outlook郵件正文

 
1st cell  : |Regards,  | 
2nd cell (Name) : |Asaf Gilad | 
3rd Cell (Title): |PMO   | 
4th cell (Mail) : |[email protected] | 

所以,當我點擊發送,郵件的正文將看如:

 
Dear sir 
................................ 
....... Message Content ........ 
................................ 
................................ 

Regards, 
Asaf Gilad  
PMO   
[email protected] 

簽名包含圖片以及。

我設法將範圍保存爲圖片,並將該圖片作爲附件發送,但圖片原來是空的,因爲它沒有正確地作爲附件發送。

這裏是我使用的代碼:

 
Public Sub ExportEmail(recipentName As String) 
    On Error GoTo err: 
    Dim olApp As Outlook.Application 
    Dim olNs As Outlook.Namespace 
    Dim olMail As Outlook.MailItem 
    Dim strEmailTo As String, strEmailCC As String, strEmailBCC As String 
    Dim FNAME As String 
    Dim oRange As Range 
    Dim oChart As Chart 
    Dim oImg As Picture 
    strEmailTo = "" 
    strEmailCC = "" 
    strEmailBCC = "" 
    strEmailTo = "[email protected]" 
    strEmailCC = "[email protected] 
    If strEmailTo "" Then 
     Set olApp = New Outlook.Application 
     Set olNs = olApp.GetNamespace("MAPI") 
     olNs.Logon 
     Set olMail = olApp.CreateItem(olMailItem) 
     olMail.To = strEmailTo 
     olMail.CC = strEmailCC 
     olMail.BCC = strEmailBCC 
     olMail.Subject = " My Subject" 
     Set oRange = Sheets(1).Range("A1:Z100") 
     Set oChart = Charts.Add 
     oRange.CopyPicture xlScreen, xlPicture 
     oChart.Paste 
     FNAME = Environ$("temp") & "\testPic.gif" 
     oChart.Export Filename:=FNAME, FilterName:="GIF" 
     olMail.Attachments.Add FNAME 
     olMail.HTMLBody = "" & _ 
      "" 
     olMail.Attachments.Add FNAME 
     olMail.Send 
    End If 
    Application.StatusBar = False 
    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True 
    Kill FNAME 
    Set olApp = Nothing 
    Set olNs = Nothing 
    Set oRange = Nothing 
    Set oChart = Nothing 
    Set oImg = Nothing 
    Exit Sub 
err: 
    MsgBox err.Description 
End Sub 
+0

當Outlook已經有該向導輕鬆添加簽名時,爲什麼要創建簽名添加程序? – bonCodigo

+0

我還不確定。客戶定義了這種需求。也許他希望我的工具發送的電子郵件的簽名與其他郵件不同。向他詢問原因是個好主意,但我覺得這個excel-outlook任務很有趣。除了目前的任務,它可以幫助我自動化一些郵件撰寫(不能想到很好的例子:可能是報告摘要) –

回答

1

這是一個很好的問題,阿薩夫。當我構建了自動電子郵件解決方案時,我發現難以獲得簽名行。這很可能,但並不容易。也許它在2010年更新,但我還沒有檢查。

我所做的是將整個身體放置到驅動器上的文本文件中,並使用任何想要格式化的html標記。這使我在製作格式良好的電子郵件時具有很大的靈活性,我也可以分配變量。

然後我通過Microsoft Scripting Runtime庫訪問這些文件。

請參見下面的代碼片段:

Option Explicit 

Const strEmailBoiler As String = "\\server\path\folder\subfolder\email_text\" 

Sub PrepMessage() 

Dim strBody As String, strMon As String 

strMon = range("Mon").Value 
strFY = range("FY").Value 
strBody = FileToString(strEmailBoiler, "reports_email_body.txt") 

strBody = Replace(strBody, "[MONTH]", strMon) 
strBody = Replace(strBody, "[YEAR]", Right(strFY, 2)) 
strBody = Replace(strBody, "[FILE PATH]", strFileName) 

SendMail "[email protected]", "Subject Goes Here " & strMon & " YTD", strBody 

End Sub 

Function FileToString(ByVal strPath As String, ByVal strFile As String) As String 
'requires reference to Microsoft Scripting Runtime Object Library (or late binding) 

    Dim ts As TextStream 

    Set fso = New FileSystemObject 
    Set ts = fso.OpenTextFile(strPath & strFile, ForReading, False, TristateUseDefault) 

    FileToString = ts.ReadAll 
    ts.Close 

    Set ts = Nothing 
    Set fso = Nothing 

End Function 

Sub SendMail(strTo As String, strSubject As String, strHTMLBody As String, Optional strAttach As String, Optional strCC As String) 
'requires reference to Microsoft Outlook X.X Object Library (or late binding) 

Dim olApp As Outlook.Application 
Dim olMI As Outlook.MailItem 

Set olApp = CreateObject("Outlook.Application") 
Set olMI = olApp.CreateItem(olMailItem) 

With olMI 

    .To = strTo 
    .Subject = strSubject 
    .HTMLBody = strHTMLBody 
    If strAttach <> vbNullString Then .Attachments.Add strAttach 
    .Display 'using this because of security access to Outlook 
    '.Send 

End With 

End Sub 

然後我reports_email_body.txt文件將是這樣的:

<p>Hello Person,</p> 
<p>The Reports file for [MONTH] FY[YEAR] has been saved in the following location:</p> 
<p><a href="[FILE PATH]">[FILE PATH]</a></p> 
<p>Best,</p> 
<br> 
Scott Holtzman 
<br>My Address 
<br>my title 
<br>whatever else... 
1

在Excel 2010中(也可能是2007年),你可以添加.HTMLBody到年底你身體字符串。例如,使用這樣的東西:

.HTMLBody = "<br>" & strbody & .HTMLBody 
       ' <br> is an HTML tag to turn the text into HTML 
       ' strbody is your text from cell C9 on the mail tab 
       ' .HTMLBody inserts your email signature 

這將至少解決您的簽名線問題。

我正在尋找相同問題的解決方案:插入範圍作爲圖片。

相關問題