2016-08-09 122 views
1

我有一個駐留在Excel文件中的模板。一旦我點擊預覽按鈕,這個模板將顯示在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,但我無法完成它的工作。這隻給出了一個「不可見表」的產品。

+0

你確定你的設置rngBody代碼是正確的?它總是指向'範圍(「C10:N30」)'。 – Jochen

+0

@Jochen是的,這是正確的。 – ramj

+0

什麼不起作用?我很困惑 – 0m3r

回答

1

嘗試Range.PasteAndFormat wdChartPicture

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 

確保引用設置對Microsoft Outlook和Microsoft Word Object

工具>參考......

enter image description here

+0

@ramj這就是你想要做的嗎? – 0m3r

+1

這是一個偉大的想法,但是身體是否可以編輯的方式還有其他嗎? – ramj

+0

@ramj好吧試試'wordDoc.Range.PasteAndFormat wdChartPicture&.HTMLBody =「」' – 0m3r

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 
+0

這有一個錯誤,「Object Required」/ – ramj

+0

這將返回一個錯誤'This member can only for a Chart Object' – ramj