在电子邮件正文中使用Excel VBA IF THEN语句
我试图在我的电子邮件的.body中使用if-then语句,但当我运行它时,它没有任何作用。下面是我注意到的邮件正文开始的地方,我被卡住了在电子邮件正文中使用Excel VBA IF THEN语句,vba,email,Vba,Email,我试图在我的电子邮件的.body中使用if-then语句,但当我运行它时,它没有任何作用。下面是我注意到的邮件正文开始的地方,我被卡住了 Sub Email_From_Excel_Basic() ' This identifies what means what Dim emailApplication As Object Dim emailItem As Object Dim mymsg As String Dim cell As Range
Sub Email_From_Excel_Basic()
' This identifies what means what
Dim emailApplication As Object
Dim emailItem As Object
Dim mymsg As String
Dim cell As Range
Application.ScreenUpdating = False
Set emailApplication = CreateObject("Outlook.Application")
' Now we build the email.
On Error GoTo cleanup
For Each cell In Worksheets("owvr").Columns("S").Cells
Set emailItem = emailApplication.CreateItem(0)
If cell.Value Like "?*@?*.?*" And _
Cells(cell.Row, "T") = "Yes" Then
With emailItem
' This is who the email is being sent to
.To = Cells(cell.Row, "S").Value
.CC = Cells(cell.Row, "S").Value
' This is the subject of the email
.Subject = "Status update"
' This is the body of the email based on cell references
mymsg = "Dear " & Cells(cell.Row, "A").Value & " team," & vbNewLine & vbNewLine
这里
建议?让IF填充变量字符串,然后将其连接到正文中
Sub Email_From_Excel_Basic()
' This identifies what means what
Dim emailApplication As Object
Dim emailItem As Object
Dim mymsg As String
Dim cell As Range
Application.ScreenUpdating = False
Set emailApplication = CreateObject("Outlook.Application")
' Now we build the email.
On Error GoTo cleanup
For Each cell In Worksheets("owvr").Columns("S").Cells
Set emailItem = emailApplication.CreateItem(0)
If cell.Value Like "?*@?*.?*" And _
Cells(cell.Row, "T") = "Yes" Then
With emailItem
' This is who the email is being sent to
.To = Cells(cell.Row, "S").Value
.CC = Cells(cell.Row, "S").Value
' This is the subject of the email
.Subject = "Status update"
' This is the body of the email based on cell references
mymsg = "Dear " & Cells(cell.Row, "A").Value & " team," & vbNewLine & vbNewLine
Dim stts As String
If Cells(cell.Row, 4).Value = "1. New Order" Then
stts = "Your order has been received and will processed."
ElseIf Cells(cell.Row, 4).Value = "2. Shipped" Then
stts "Status: Your order has been shipped" & vbNewLine
Else
End If
mymsg = mymsg & "Status: " & stts
mymsg = mymsg & "Deposit invoice: " & Cells(cell.Row, "K").Value & vbNewLine
mymsg = mymsg & "Additional invoice: " & Cells(cell.Row, "M").Value & vbNewLine
.Body = mymsg
.Send
' This part brings the loop back up to the top, so this way it goes to the next email in the column
End With
On Error GoTo 0
Set emailItem = Nothing
End If
Next cell
cleanup:
Set emailApplication = Nothing
Set emailItem = Nothing
Set mymsg = Nothing
Application.ScreenUpdating = True
End Sub
让IF填充变量字符串,然后将其连接到正文中
Sub Email_From_Excel_Basic()
' This identifies what means what
Dim emailApplication As Object
Dim emailItem As Object
Dim mymsg As String
Dim cell As Range
Application.ScreenUpdating = False
Set emailApplication = CreateObject("Outlook.Application")
' Now we build the email.
On Error GoTo cleanup
For Each cell In Worksheets("owvr").Columns("S").Cells
Set emailItem = emailApplication.CreateItem(0)
If cell.Value Like "?*@?*.?*" And _
Cells(cell.Row, "T") = "Yes" Then
With emailItem
' This is who the email is being sent to
.To = Cells(cell.Row, "S").Value
.CC = Cells(cell.Row, "S").Value
' This is the subject of the email
.Subject = "Status update"
' This is the body of the email based on cell references
mymsg = "Dear " & Cells(cell.Row, "A").Value & " team," & vbNewLine & vbNewLine
Dim stts As String
If Cells(cell.Row, 4).Value = "1. New Order" Then
stts = "Your order has been received and will processed."
ElseIf Cells(cell.Row, 4).Value = "2. Shipped" Then
stts "Status: Your order has been shipped" & vbNewLine
Else
End If
mymsg = mymsg & "Status: " & stts
mymsg = mymsg & "Deposit invoice: " & Cells(cell.Row, "K").Value & vbNewLine
mymsg = mymsg & "Additional invoice: " & Cells(cell.Row, "M").Value & vbNewLine
.Body = mymsg
.Send
' This part brings the loop back up to the top, so this way it goes to the next email in the column
End With
On Error GoTo 0
Set emailItem = Nothing
End If
Next cell
cleanup:
Set emailApplication = Nothing
Set emailItem = Nothing
Set mymsg = Nothing
Application.ScreenUpdating = True
End Sub
这是有帮助的,没有错误,但当我运行它时,电子邮件不会发送尝试编辑,我只是发布。另外,删除错误转到清理时的行
,并查看错误的位置。这很有帮助,没有错误,但当我运行它时,电子邮件不会发送尝试编辑,我只是发布。还要删除错误转到清理时的行,并查看它在哪里出错。似乎您试图将mymsg
与If
连接起来。它甚至不应该运行(If
应该在单独的一行上)。似乎您试图将mymsg
与If
连接起来。它甚至不应该运行(如果
应该在单独的行上)。