从Excel到Outlook正文

时间:2015-09-10 03:05:41

标签: vb.net excel outlook

我需要从Excel中获取一个范围并将其粘贴到Outlook电子邮件中。问题在于,当我这样做时,它只是粘贴文本而不是表格。下面是代码。我试过图片,但我不想嵌入它,不明白为什么它不会像我复制图像那样粘贴它。我正在使用Office 2013。

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim objexcel As New Excel.Application
    Dim objWorkbook As Excel.Workbook
    Dim objWorksheet As Excel.Worksheet
    Dim Range As Excel.Range

    objexcel.DisplayAlerts = False


    objWorkbook = objexcel.Workbooks.Open("c://Derp")

    objWorksheet = CType(objWorkbook.Worksheets.Item("Sheet1"), Excel.Worksheet)
    objWorksheet.Unprotect()

    objWorksheet.Cells(2, 1).value = txtTravEmails.Text
    objWorksheet.Cells(2, 4).value = Txttravemdate.Text
    objWorksheet.Cells(3, 1).value = txtTravUnread.Text
    objWorksheet.Cells(3, 4).value = Txttravundate.Text
    objWorksheet.Cells(6, 1).value = txtITweb.Text
    objWorksheet.Cells(6, 4).value = Txtwebdate.Text
    objWorksheet.Cells(7, 1).value = txtITAPI.Text
    objWorksheet.Cells(7, 4).value = Txtapidate.Text
    objWorksheet.Cells(8, 1).value = txtITpend.Text
    objWorksheet.Cells(8, 4).value = Txtpenddate.Text
    'objWorksheet.Range("A1:D9").CopyPicture()
    Range = objWorksheet.Range("A1:D9")
    Range.Copy()

    If Chkmorning.Checked = True Then
        Dim Email As New Email_Templates
        Email.Morning()
    ElseIf Chkmid.Checked = True Then
        Dim Email As New Email_Templates
        Email.Mid()
    ElseIf Chkevening.Checked = True Then
        Dim Email As New Email_Templates
        Email.Evening()
    ElseIf Chkmidnight.Checked = True Then
        Dim Email As New Email_Templates
        Email.Midnight()
    End If

    objWorkbook.Save()
    objWorkbook.Close()
    objexcel.Quit()


End Sub

Public Sub Morning()

    Dim Morning As String = "<html>" +
                "<HEAD>" +
                "<TITLE>Email</TITLE>" +
                "</HEAD>" +
                "<BODY>" +
                "<font face=Calibri>" +
                "<b> This is the morning report sent at " + DateAndTime.Now.ToShortTimeString + " EST! " +
                "<br><br>" + Clipboard.GetText +
                    "</font>" +
                    "</BODY>" +
                    "</HTML>"

    mail.Recipients.Add("derp@derp.com")
    mail.BCC = ""

    mail.Subject =  Date.Now.ToShortDateString
    mail.HTMLBody = Morning
    'mail.Body = "This is the morning report sent at " & DateAndTime.Now.ToShortTimeString & " EST!" & vbNewLine & Clipboard.GetText

    mail.Display()
End Sub

1 个答案:

答案 0 :(得分:0)

您必须从剪贴板数据中传递所需的格式。

Dim Morning As String = "<html>" +
            "<HEAD>" +
            "<TITLE>Email</TITLE>" +
            "</HEAD>" +
            "<BODY>" +
            "<font face=Calibri>" +
            "<b> This is the morning report sent at " + DateAndTime.Now.ToShortTimeString + " EST! " +
            "<br><br>" + Clipboard.GetText(TextDataFormat.Html) +
                "</font>" +
                "</BODY>" +
                "</HTML>"

使用Clipboard.GetText method TextDataFormat constants有一个可选的格式参数。