2016-09-30 145 views
0

我正在運行一個代碼,它從Excel工作表中獲取數據,將其轉換爲HTML並作爲電子郵件發送出去。下面是該方法使用:將Excel表導出爲HTML

'replace html body' 
htmlString = Replace(htmlString, "#FIELD1#", ws.Range("D5").value) 
htmlString = Replace(htmlString, "#FIELD2#", ws.Range("C6").value) 

現在我有,我想複製粘貼到HTML格式同一個完整的表(邊框,字體等)

能有人幫如何做到這一點?

+0

羅恩去熊先生的博客[郵件範圍/選擇在郵件的正文(http://www.rondebruin.nl/win/s1/outlook/bmail2.htm)的所有代碼和例子你需要。 – 2016-10-01 04:56:21

回答

0

答案在某種程度上取決於您的郵件客戶端。 Outlook與VBA緊密集成。如果您使用的是通用郵件客戶端,則仍應能完成此任務,但可能會遇到問題。

如果您想從Excel中複製並粘貼爲HTML中的Outlook,則已經解答:Copying values from excel to body of outlook email vb.net

如果您想使用通用電子郵件發送請參閱下面的答案。我相信這對HTML來說可以很好地工作。 (如果您的郵件客戶端對傳遞的HTML不滿意,您可以將其轉換爲另存爲文本文件,對於文本文件,您可以從開發人員功能區錄製自己的宏,只需開始錄製並使用另存爲即可保存爲文本文件)。

下面有3個子/功能。我測試了HTMLExport,這是我的代碼。 SendEMail正好來自Chip Pearson的網站,並且應該可以正常工作。我沒有測試ExcelToHTMLToEMail,這只是調用前2

Sub ExcelToHTMLToEMail(BodyRngName as string, 
     Subject As String, _ 
     FromAddress As String, _ 
     ToAddress As String, _ 
     SMTP_Server As String, _ 
     Optional Attachments As Variant = Empty) 

    Dim BodyFileName As String 

    BodyFileName = "C:\temp.htm" 

    HTMLExport RngName, BodyFileName 

    SendEMail Subject, _ 
     FromAddress, _ 
     ToAddress, _ 
     "", _ 
     SMTP_Server, _ 
     BodyFileName, _ 
     Optional Attachments 
End Sub 

Sub HTMLExport(RngName as string, _ 
    HtmlFileName as String, _ 
    Optional PageTitle as string = "") 
    ' 
    ' HTMLExport Macro 
    ' 
    Range(RngName).Select 
    With ActiveWorkbook.PublishObjects.Add(xlSourceRange, HtmlFileName , _ 
     "Sheet1 (7)", RngName, xlHtmlStatic, , "MyPageTitle") 
     .Publish (True) 
     .AutoRepublish = False 
    End With 
End Sub 

你可以找到來自芯片皮爾遜從Excel發送電子郵件代碼:http://www.cpearson.com/Excel/EMail.aspx。該網站包含一個巨大的Excel VBA代碼存儲庫。 簡介

從您的應用程序添加發送電子郵件的功能並不困難。如果您只想發送工作簿,只有主題但沒有內容,則可以使用ThisWorkbook.SendMail。但是,如果要在郵件正文中包含文本或將附加文件包含爲附件,則需要一些VBA代碼。該頁面描述了一個名爲SendEmail的函數,該函數將詳細信息包裝在一個漂亮的,適用於VBA的函數中。你可以在這裏下載代碼文件。

函數的定義是:

Function SendEMail(Subject As String, _ 
     FromAddress As String, _ 
     ToAddress As String, _ 
     MailBody As String, _ 
     SMTP_Server As String, _ 
     BodyFileName As String, _ 
     Optional Attachments As Variant) As Boolean 

哪裏 主題是電子郵件的主題行。

FromAddress是您的電子郵件地址。

ToAddress是電子郵件將發送到的地址。您可以通過用分號分隔電子郵件地址來向多個收件人發送郵件。

MailBody是要成爲消息正文的文本。如果將其留空並且BodyFileName將其命名爲文本文件,則該消息的主體將是由BodyFileName命名的文件中的所有文本。如果BodyFileName和MailBody都爲空,則消息將不帶主體發送。

SMTP_Server是您的發送郵件服務器的名稱。

BodyFileName是將用作消息正文的文本文件的名稱。如果MailBody不爲空,則忽略此參數,並且不會將該文件用作主體。如果MailBody和BodyFileName都不爲空,則將使用MailBody的內容作爲主體並忽略BodyFileName。

附件是一個文件名或附加到郵件的文件名數組。如果附加其中一個文件時出現錯誤,則繼續處理其餘文件併發送電子郵件。

該函數如果成功則返回True,如果發生錯誤則返回False。

代碼需要參考Microsoft CDO for Windows 2000 Library。該文件的典型文件位置是C:\ Windows \ system32 \ cdosys.dll。此組件的GUID是{CD000000-8B95-11D1-82DB-00C04FB1625D},與主要= 1和次要= 0

SectionBreak

守則

的代碼如下所示。你可以在這裏下載代碼文件。

Function SendEMail(Subject As String, _ 
     FromAddress As String, _ 
     ToAddress As String, _ 
     MailBody As String, _ 
     SMTP_Server As String, _ 
     BodyFileName As String, _ 
     Optional Attachments As Variant = Empty) As Boolean 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' SendEmail Function 
' By Chip Pearson, [email protected] www.cpearson.com 28-June-2012 
' 
' This function sends an email to the specified user. 
' Parameters: 
' Subject:  The subject of the email. 
' FromAddress: The sender's email address 
' ToAddress:  The recipient's email address or addresses. 
' MailBody:  The body of the email. 
' SMTP_Server: The SMTP-Server name for outgoing mail. 
' BodyFileName: A text file containing the body of the email. 
' Attachments  A single file name or an array of file names to 
'     attach to the message. The files must exist. 
' Return Value: 
' True if successful. 
' False if failure. 
' 
' Subject may not be an empty string. 
' FromAddress must be a valid email address. 
' ToAddress must be a valid email address. To send to multiple recipients, 
' use a semi-colon to separate the individual addresses. If there is a 
' failure in one address, processing terminates and messages are not 
' send to the rest of the recipients. 
' If MailBody is vbNullString and BodyFileName is an existing text file, the content 
' of the file named by BodyFileName is put into the body of the email. If 
' BodyFileName does not exist, the function returns False. The content of 
' the message body is created by a line-by-line import from BodyFileName. 
' If MailBody is not vbNullString, then BodyFileName is ignored and the body 
' is not created from the file. 
' SMTP_Server must be a valid accessable SMTP server name. 
' If both MailBody and BodyFileName are vbNullString, the mail message is 
' sent with no body content. 
' Attachments can be either a single file name as a String or an array of 
' file names. If an attachment file does not exist, it is skipped but 
' does not cause the procedure to terminate. 
' 
' If you want to send ThisWorkbook as an attachment to the message, use code 
' like the following: 
' ThisWorkbook.Save 
' ThisWorkbook.ChangeFileAccess xlReadOnly 
' B = SendEmail(_ 
'  ... parameters ... 
'  Attachments:=ThisWorkbook.FullName) 
' ThisWorkbook.ChangeFileAccess xlReadWrite 
' 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' Required References: 
' -------------------- 
' Microsoft CDO for Windows 2000 Library 
'  Typical File Location: C:\Windows\system32\cdosys.dll 
'  GUID: {CD000000-8B95-11D1-82DB-00C04FB1625D} 
'  Major: 1 Minor: 0 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

Dim MailMessage As CDO.Message 
Dim N As Long 
Dim FNum As Integer 
Dim S As String 
Dim Body As String 
Dim Recips() As String 
Dim Recip As String 
Dim NRecip As Long 

' ensure required parameters are present and valid. 
If Len(Trim(Subject)) = 0 Then 
    SendEMail = False 
    Exit Function 
End If 

If Len(Trim(FromAddress)) = 0 Then 
    SendEMail = False 
    Exit Function 
End If 

If Len(Trim(SMTP_Server)) = 0 Then 
    SendEMail = False 
    Exit Function 
End If 

' Clean up the addresses 
Recip = Replace(ToAddress, Space(1), vbNullString) 
If Right(Recip, 1) = ";" Then 
    Recip = Left(Recip, Len(Recip) - 1) 
End If 
Recips = Split(Recip, ";") 


For NRecip = LBound(Recips) To UBound(Recips) 
    On Error Resume Next 
    ' Create a CDO Message object. 
    Set MailMessage = CreateObject("CDO.Message") 
    If Err.Number <> 0 Then 
     SendEMail = False 
     Exit Function 
    End If 
    Err.Clear 
    On Error GoTo 0 
    With MailMessage 
     .Subject = Subject 
     .From = FromAddress 
     .To = Recips(NRecip) 
     If MailBody <> vbNullString Then 
      .TextBody = MailBody 
     Else 
      If BodyFileName <> vbNullString Then 
       If Dir(BodyFileName, vbNormal) <> vbNullString Then 
        ' import the text of the body from file BodyFileName 
        FNum = FreeFile 
        S = vbNullString 
        Body = vbNullString 
        Open BodyFileName For Input Access Read As #FNum 
        Do Until EOF(FNum) 
         Line Input #FNum, S 
         Body = Body & vbNewLine & S 
        Loop 
        Close #FNum 
        .TextBody = Body 
       Else 
        ' BodyFileName not found. 
        SendEMail = False 
        Exit Function 
       End If 
      End If ' MailBody and BodyFileName are both vbNullString. 
     End If 

     If IsArray(Attachments) = True Then 
      ' attach all the files in the array. 
      For N = LBound(Attachments) To UBound(Attachments) 
       ' ensure the attachment file exists and attach it. 
       If Attachments(N) <> vbNullString Then 
        If Dir(Attachments(N), vbNormal) <> vbNullString Then 
         .AddAttachment Attachments(N) 
        End If 
       End If 
      Next N 
     Else 
      ' ensure the file exists and if so, attach it to the message. 
      If Attachments <> vbNullString Then 
       If Dir(CStr(Attachments), vbNormal) <> vbNullString Then 
        .AddAttachment Attachments 
       End If 
      End If 
     End If 
     With .Configuration.Fields 
      ' set up the SMTP configuration 
      .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTP_Server 
      .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
      .Update 
     End With 

     On Error Resume Next 
     Err.Clear 
     ' Send the message 
     .Send 
     If Err.Number = 0 Then 
      SendEMail = True 
     Else 
      SendEMail = False 
      Exit Function 
     End If 
    End With 
Next NRecip 
SendEMail = True 
End Function 
If you want to attach the workbook that contains the code, you need to make the file read-only when you send it and then change access back to read-write. For example, 

ThisWorkbook.Save 
ThisWorkbook.ChangeFileAccess xlReadOnly 
B = SendEmail(_ 
    ... parameters ... 
    Attachments:=ThisWorkbook.FullName) 
ThisWorkbook.ChangeFileAccess xlReadWrite 
+0

謝謝你的幫助! –