2017-06-15 91 views
0

我想用outlook模板自動發送郵件,對連接到excel文件的文本進行微小更改。但是,當我選擇HTML格式時,結果不會顯示。這裏是我的代碼:我認爲問題在於.HTMLbody行,因爲代碼中的其他內容都可以正常工作。有人可以幫忙嗎?通過vba發送時替換oft模板中的內容excel

小組測試1()

「在辦公室2000年至2016年 昏暗OutApp工作作爲Outlook.Application 暗淡發件作爲Outlook.MailItem 昏暗細胞作爲範圍

Application.ScreenUpdating = False 
Set OutApp = CreateObject("Outlook.Application") 
Set OutMail = OutApp.CreateItemFromTemplate("C:\Users\600008809\Desktop\Reminder emails\Initial Survey.oft") 

On Error GoTo cleanup 
For Each cell In Columns("G").Cells.SpecialCells(xlCellTypeConstants) 
    If cell.Value Like "?*@?*.?*" And _ 
     LCase(Cells(cell.Row, "H").Value) = "yes" _ 
     And LCase(Cells(cell.Row, "I").Value) <> "send" Then 

     'Set OutMail = OutApp.CreateItem(0) 

     On Error Resume Next 
     With OutMail 
      .To = cell.Value 
      .Subject = "abc!" 
      .BodyFormat = olFormatHTML 
      .HTMLBody = Replace(.HTMLBody, "&lt;&lt; HiringManager &gt;&gt;", Worksheets("Tool").Range(4, 2)) 




      'You can add files also like this 
      '.Attachments.Add ("C:\test.txt") 
      Set .SendUsingAccount = OutApp.Session.Accounts.Item("[email protected]") 
      .Send 
      '.Display 'Or use Display 
     End With 
     On Error GoTo 0 
     Cells(cell.Row, "I").Value = "send" 
     Set OutMail = Nothing 

    End If 
Next cell 

清理: 集OutApp = Nothing Application.ScreenUpdating = True End Sub

回答

0

此行不好:

.HTMLBody = Replace(.HTMLBody, "&lt;&lt; HiringManager &gt;&gt;", Worksheets("Tool").Range(4, 2)

必須更換在這裏的範圍值,或者單元格的值,所以你應該使用

.HTMLBody = Replace(.HTMLBody, "&lt;&lt; HiringManager &gt;&gt;", Worksheets("Tool").cells(4, 2).value