将选定的Excel范围复制到Outlook

时间:2017-11-21 08:50:08

标签: excel vba vbscript outlook

我想将选定的Excel范围" A1复制到G5"来自Sheet" PUCCH"到vbs脚本中的Outlook。

我有错误

  

类型不匹配

UPDATE user_product_status AS ups JOIN user_accounts ac ON ac.user_id = ups.user_account_id JOIN users u ON u.id = ac.user_id SET access_starts_at = '2022-04-03 08:52:58' WHERE u.email = 'someemail@aaa.com'

.HTMLBody= "PUCCH:<br>" & objWorksheet.Range("A1:G5")

如何更正.Range()中的.HTMLBody?

1 个答案:

答案 0 :(得分:1)

您正尝试将范围本身复制到电子邮件中,而不是先读取值并将其插入电子邮件中。根据这些单元格的格式,您可以尝试这样的

Dim sRangeValues as String  
Dim r as Range

Set outlook = CreateObject("Outlook.Application")
Set email = outlook.CreateItem(0)
Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Open("X:\d\PCCW\realtime_MTR\PUCCH_level.XLSX")
Set xlSht = xlBook.Sheets("PUCCH")

Set objExcel = CreateObject("Excel.Application")                                                               
Set objWorkbook =objExcel.Workbooks.Open("X:\d\PCCW\realtime_MTR\PUCCH_level.XLSX")  

Set objWorksheet = objWorkbook.Worksheets("PUCCH") 
objWorksheet.Activate

set r = objWorksheet.Range("A1:G5")

For i = 1 To r.Rows.Count
    For j = 1 To r.Rows(i).Columns.Count
        sRangeValues = sRangeValues & r.Rows(i).Columns(j).Value & "|"
    Next
    sRangeValues = sRangeValues & Chr(10)
Next

With email
       '.to="Email;"
       .to="Email;"
       .Subject="Houly MTR Alerter"
       .HTMLBody= "PUCCH:<br>" & sRangeValues
       .Send

End With 
Wscript. quit