我目前正在处理一个宏,该宏应根据输入到工作表上相关行的输入,在电子邮件正文中创建包含可变收件人和详细信息的电子邮件。它应该每行生成一个电子邮件,其中“待办事项”出现在P列中。
我目前有一个子编码,包含我认为我需要的所有内容,当我运行宏时没有出现错误,遗憾的是它也没有按预期打开任何模板电子邮件。
我承认我的VBA充其量是基本的,但下面的任何帮助都会很棒。
Sub Sendmail()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim ClientEmail As Range
Dim PlannerName As String
Dim Salutation As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("F").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value = "Planner1 Initials" And _
LCase(Cells(cell.Row, "P").Value) = "To do" Then
Set ClientEmail = LCase(Cells(cell.Row, "H").Value)
PlannerName = "Planner1 Name"
Set Salutation = LCase(Cells(cell.Row, "D").Value)
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "Planner1@email.com"
.Subject = "Annual Review"
.Body = "send email to" & vbNewLine & vbNewLine & _
"Dear" & xClientSaluation & vbNewLine & vbNewLine & _
"body" & vbNewLine & _
"Best wishes" & vbNewLine & vbNewLine & _
"" & PlannerName
.Display
End With
ElseIf cell.Value = "Planner2 Initials" And _
LCase(Cells(cell.Row, "P").Value) = "To do" Then
Set ClientEmail = LCase(Cells(cell.Row, "H").Value)
PlannerName = "Planner2 Name"
Set Salutation = LCase(Cells(cell.Row, "D").Value)
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "Planner2@email.com"
.Subject = "Annual Review"
.Body = "send email to" & vbNewLine & vbNewLine & _
"Dear" & xClientSaluation & vbNewLine & vbNewLine & _
"body" & vbNewLine & _
"Best wishes" & vbNewLine & vbNewLine & _
"" & PlannerName
.Display
End With
ElseIf cell.Value = "Planner3 Initials" And _
LCase(Cells(cell.Row, "P").Value) = "To do" Then
Set ClientEmail = LCase(Cells(cell.Row, "H").Value)
PlannerName = "Planner3 Name"
Set Salutation = LCase(Cells(cell.Row, "D").Value)
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "Planner3@email.com"
.Subject = "Annual Review"
.Body = "send email to" & vbNewLine & vbNewLine & _
"Dear" & xClientSaluation & vbNewLine & vbNewLine & _
"Body " & vbNewLine & _
"Best wishes" & vbNewLine & vbNewLine & _
"" & PlannerName
.Display
End With
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
感谢您可能不需要像我上面所做的那样为每个不同的首字母组重复,但很难设置一个没有对象错误的范围。
上下文
的工作表列中保存的数据F =客户所有者姓名缩写
P =如果需要发送电子邮件,请输入No,To Do,Yes或n / a
D =客户的称呼
谢谢,
BIG TWON
感谢下面的Krib,我现在在下面一行得到了Object required错误,我想这会在整个过程中重复:
Set ClientEmail = LCase(Cells(cell.Row, "H").Value)
答案 0 :(得分:1)
我想我可以看到三个问题:
LCase(Cells(cell.Row, "P").Value) = "To do" Then
LCase将会看到" t o"而不是" T o"所以你的主要区块永远不会运行。
将它们更改为:
LCase(Cells(cell.Row, "P").Value) = "to do" Then
另外,根据您最近的更新,行:
Set ClientEmail = LCase(Cells(cell.Row, "H").Value)
正在尝试将.Value
个单元格加载到ClientEmail
很遗憾,您已将其声明为Range
:
Dim ClientEmail As Range
从代码的外观来看,您应该声明它As String
。但请记住,你不要Set
字符串。你只需加载它们,例如:
ClientEmail = LCase(Cells(cell.Row, "H").Value)
除此之外,您还可以:
Dim Salutation As Range
和
Set Salutation = LCase(Cells(cell.Row, "D").Value)
应该是:
Dim Salutation As String
和..
Salutation = LCase(Cells(cell.Row, "D").Value)
所以结束..
您的声明应如下所示:
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Dim ClientEmail As String
Dim PlannerName As String
Dim Salutation As String
您的IF语句应如下所示(每次更改PlannerX):
If cell.Value = "Planner1 Initials" And LCase(Cells(cell.Row, "P").Value) = "to do" Then
您的变量设置应如下所示(再次,每次更改PlannerX):
ClientEmail = LCase(Cells(cell.Row, "H").Value)
PlannerName = "Planner1 Name"
Salutation = LCase(Cells(cell.Row, "D").Value)
答案 1 :(得分:0)
也许这可以帮助您...
Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim cell As Range
Dim FileCell As Range
Dim rng As Range
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set sh = Sheets("Sheet1")
Set OutApp = CreateObject("Outlook.Application")
For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)
'Enter the path/file names in the C:Z column in each row
Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")
If cell.Value Like "?*@?*.?*" And _
Application.WorksheetFunction.CountA(rng) > 0 Then
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = cell.Value
.Subject = "Testfile"
.Body = "Hi " & cell.Offset(0, -1).Value
For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
If Trim(FileCell) <> "" Then
If Dir(FileCell.Value) <> "" Then
.Attachments.Add FileCell.Value
End If
End If
Next FileCell
.Send 'Or use .Display
End With
Set OutMail = Nothing
End If
Next cell
Set OutApp = Nothing
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
使用:在Sheets(“ Sheet1”)中创建列表:
In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)