将模板从Excel复制到Outlook

时间:2016-08-09 10:55:16

标签: excel vba excel-vba outlook

我有一个驻留在excel文件中的模板。单击预览按钮后,此模板将显示在outlook及其主题中,等等。

我有这个代码可以正常工作但不在正文字段中工作。

Sub previewMail()
Dim objMail, objOutLook As Object
Dim rngTo, rngCC, rngBCC, rngBody As Range
Dim lRow As Long
Dim i As Integer


Set objOutLook = CreateObject("Outlook.Application")
Set objMail = objOutLook.CreateItem(0)
Set main = ThisWorkbook.Sheets("Main")

lRow = main.Cells(Rows.Count, 2).End(xlUp).Row

For i = 11 To lRow
    With main
        Set rngTo = .Range("B" & i)
        Set rngBody = .Range(.Range("C10:N30"), .Range("C10:N30")) 
    End With

    With objMail
        .To = rngTo.Value
        .Subject = "Sample"
        'i like the rngbody to be here
        .HTMLBody = RangetoHTML(rngBody)' from Ron de Bruin site
        .Display

    End With
Next i
End Sub

这是上述范围内的模板。

enter image description here 有谁能帮我解决这个问题?我曾尝试过来自Ron de Bruin的this,但我无法让它发挥作用。这只会产生一个"隐形表"。

2 个答案:

答案 0 :(得分:2)

编辑:OP表示文字不在范围内,但在范围前面的文本框中。

使用此代码查找文本框名称:

for i = 1 to activesheet.chartobjects.count
    debug.print chartobjects(i).name
next i

它会像Textbox1之类的东西,然后使用(未经测试):

dim strBody as string
Set strBody = activesheet.chartobjects("Textbox1").Value
.HTMLBody = strbody

答案 1 :(得分:1)

尝试Range.PasteAndFormat wdChartPicture

实施例

P:FILEUPLOAD

确保设置对Microsoft Outlook和Microsoft Word的引用 Option Explicit Sub previewMail() Dim objMail, Main, objOutLook As Object Dim rngTo, rngCC, rngBCC, rngBody As Range Dim lRow As Long Dim i As Integer Dim wordDoc As Word.Document '<--- Set objOutLook = CreateObject("Outlook.Application") Set objMail = objOutLook.CreateItem(0) Set Main = ThisWorkbook.Sheets("Main") Set wordDoc = objMail.GetInspector.WordEditor '<--- lRow = Main.Cells(Rows.count, 2).End(xlUp).Row For i = 11 To lRow With Main Set rngTo = .Range("B" & i) Set rngBody = .Range(.Range("C10:N30"), .Range("C10:N30")) rngBody.Copy '<--- End With With objMail .To = rngTo.Value .Subject = "Sample" .Display wordDoc.Range.PasteAndFormat wdChartPicture '<--- ' Or 'wordDoc.Range.PasteAndFormat wdChartPicture & .HTMLBody = " " End With Next i End Sub

工具&gt; 参考...

enter image description here