首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据特定单元格内容发送电子邮件

根据特定单元格内容发送电子邮件
EN

Stack Overflow用户
提问于 2017-04-07 20:50:26
回答 2查看 56关注 0票数 0

我是一个新手VBA程序员,我已经搜索了这个,但无法找到一个解决方案,完全符合我的需要。

我有一个代码来ping客户的IP地址,但我只需要对ping超时的电子邮件通知。

ping结果在电子表格的D列中,电子邮件在E列中。如果有任何帮助,我将不胜感激。

提前谢谢。

代码语言:javascript
运行
复制
Dim OutlookApp
Dim objMail
Dim x As Long
Dim PingResults As range


lastrow = Sheets("Ping").Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lastrow

Set PingResults = range("d2:D250")
Set OutlookApp = CreateObject("Outlook.Application")
Set objMail = OutlookApp.CreateItem(olMailItem)

If PingResults.Cells.Value = "Request timed out." Then

objMail.To = Cells(x, 5).Value

With objMail
.Subject = Cells(x, 1) & " " & "-" & " " & Cells(x, 2) & " " & "-" & " " & Cells(x, 3)
.Body = "Run Diagnostics.  Customer's broadband appears to have issues" & vbCrLf & Cells(x, 4)
.Display
.Save
End With

SendKeys "%{s}", True

ElseIf PingResults.Cells.Value = "" Then


Set OutlookApp = Nothing
Set objMail = Nothing
End If
End Sub
EN

回答 2

Stack Overflow用户

发布于 2017-04-07 21:29:55

你很可能是在追求这个:

代码语言:javascript
运行
复制
Option Explicit

Sub main()
    Dim pingResults As Range, cell As Range

    With Sheets("Ping")
        With .Range("D1", .Cells(.Rows.Count, "D").End(xlUp))
            .AutoFilter Field:=1, Criteria1:="Request timed out."
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then Set pingResults = .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible)
        End With
        .AutoFilterMode = False
    End With

    If Not pingResults Is Nothing Then
        With CreateObject("Outlook.Application")
            For Each cell In pingResults
                With .CreateItem(0) '<--| olMailItem is an item of an OutLook enumeration whose value is "zero"
                    .Display
                    .to = cell.Offset(, 1).Value
                    .Subject = cell.Offset(, -3) & " " & "-" & " " & cell.Offset(, -2) & " " & "-" & " " & cell.Offset(, -1)
                    .Body = "Run Diagnostics.  Customer's broadband appears to have issues" & vbCrLf & cell.Value
                    .Save
                End With
                SendKeys "%{s}", True
            Next
            .Quit
        End With
    End If    
End Sub
票数 1
EN

Stack Overflow用户

发布于 2017-04-07 21:30:26

这应该可以做到:

代码语言:javascript
运行
复制
Dim OutlookApp
Dim objMail
Dim x As Long
Dim PingResults As Range

Set OutlookApp = CreateObject("Outlook.Application")
lastrow = Sheets("Ping").Cells(Rows.Count, 1).End(xlUp).Row
Set PingResults = Range("d1:D" & lastrow)

For x = 2 To lastrow

    If PingResults.Cells(x, 1).Value = "Request timed out." Then
        Set objMail = OutlookApp.CreateItem(olMailItem)

        With objMail
            .To = Cells(x, 5).Value
            .Subject = Cells(x, 1) & " " & "-" & " " & Cells(x, 2) & " " & "-" & " " & Cells(x, 3)
            .Body = "Run Diagnostics.  Customer's broadband appears to have issues" & vbCrLf & Cells(x, 4)
            .Display
            .Save
        End With

        SendKeys "%{s}", True
        Set objMail = Nothing

    End If
Next x
Set OutlookApp = Nothing
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43278662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档