【发布时间】:2015-06-26 14:52:12
【问题描述】:
我希望创建一个 VBA 宏(来自 Excel),用于搜索正文中包含帐号的电子邮件,并删除该电子邮件中的所有其他表格元素,但我指定的元素所在的行除外。
例如,请参阅下面的附件。假设我搜索的号码是“222222222222”号码。我需要 VBA 代码来保留表格标题,删除所有不是“22222222222”的帐户(所以所有 1111 帐户),同时还保留持有量。是否有代码可以删除“2222222222”之后的所有文本但在“Term”一词处停止删除?在那个令牌上,它是否可以在“Deadline”一词之后开始删除,但在到达“222222...”帐户时停止删除?
如果有什么方法我可以专注于实现这一目标,请告诉我。
谢谢!
(P.S. 这些邮件的长度不一,每天都会收到很多。另外,我已经有了可以通过搜索文本来搜索和打开邮件的代码。我只是不知道如何删除这个我不是在寻找无关的东西。)
我已附上我的相关代码以搜索并打开下面的电子邮件:
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Outlook.MailItem
Dim i As Integer
Dim olMsg As Outlook.MailItem
Dim r As Range
Dim strLocation As String
Dim o As Outlook.Application
Dim strbody As String
Dim objInspector As Object
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Select
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders(" Notifications Macro")
i = 1
For Each olMail In Fldr.Items
If InStr(olMail.body, ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value) <> 0 Then
olMail.display
If InStr(olMail.body, "Mandatory Event: No Responses Required for this") Then
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Team,<br><br>" & _
"Please see the notice below regarding " & _
ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-2).Value & _
".<br><br> This is for informational purposes and no action is required.<br><br>" & _
"Thank you!"
With olMail.Forward
.To = ActiveCell.Offset(ColumnOffset:=-1)
.display
SendKeys ("%")
SendKeys ("7")
'Call Sleep
Application.Wait (Now + TimeValue("0:00:03"))
.HTMLBody = strbody & "<br>" & .HTMLBody
.HTMLBody = Replace(.HTMLBody, ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value, "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value & "</FONT>")
End With
End If
If InStr(olMail.body, "Warning: Response Required") Then
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Team,<br><br>" & _
"Please see the notice below regarding " & _
ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-2).Value & _
".<br><br> If the client wishes to make an election, they will need to call the corresponding team before the deadline indicated on the notice.<br><br>" & _
"Thank you!"
With olMail.Forward
.To = ActiveCell.Offset(ColumnOffset:=-1)
.display
SendKeys ("%")
SendKeys ("7")
'Call Sleep
Application.Wait (Now + TimeValue("0:00:03"))
.HTMLBody = strbody & "<br>" & .HTMLBody
.HTMLBody = Replace(.HTMLBody, ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value, "<FONT style=" & Chr(34) & "BACKGROUND-COLOR: yellow" & Chr(34) & ">" & ActiveCell.Offset(rowOffset:=0, ColumnOffset:=-3).Value & "</FONT>")
End With
End If
End If
Next
End Sub
代码可能看起来令人困惑,但它所做的只是在特定的 Outlook 文件夹中搜索包含特定值的邮件(在单元格 A1 中......在这种情况下...... 222222222222)然后打开电子邮件,转发电子邮件,并突出显示电子邮件中的帐号 (2222222222)。
理想情况下,除了可以删除“2222222”号码之后的文本直到碰到“学期”。问题是我还需要保持持有的数量,因为这个数字真的可以是任何东西,我很难想到如何做到这一点。 任何指导将不胜感激!
-- 编辑--
这里是“表格”的代码,尽管它实际上并没有显示表格标签。这是帐号出现的地方。我分别将它们更改为“111111”和“222222”。
</table>
<div class="normalHeader xsmall" style="color:'CE0000'; background-color: white;">
Mandatory Event: No Responses Required for this event <br/>
</div>
<table width="98%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td>
</td>
</tr>
<tr bgcolor="EEEEEE">
<td class="normalHeader medium" align="left" colspan="7">
Account Details
</td>
</tr>
<tr bgcolor="EFEFEF">
<td class="normalHeader xsmallAlternate" style="background-color: #EFEFEF;">
Account
</td>
<td width="100">
</td>
<td align="center" class="normalHeader xsmallAlternate" width="20%" nowrap="true" style="background-color: #EFEFEF;" colspan="3">
Holding Quantity
</td>
<td width="100">
</td>
<td class="normalHeader xsmallAlternate" nowrap="true" style="background-color: #EFEFEF;">
Account Deadline
</td>
</tr>
<tr class="xsmall">
<td nowrap="true">
11111111111
</td>
<td> </td>
<td width="100">
</td>
<td align="right" nowrap="true">
25,000
</td>
<td width="100">
</td>
<td> </td>
<td style="font-weight: bold;">
</td>
</tr>
<tr class="xsmall">
<td nowrap="true">
11111111111
</td>
<td> </td>
<td width="100">
</td>
<td align="right" nowrap="true">
50,000
</td>
<td width="100">
</td>
<td> </td>
<td style="font-weight: bold;">
</td>
</tr>
<tr class="xsmall">
<td nowrap="true">
222222222222
</td>
<td> </td>
<td width="100">
</td>
<td align="right" nowrap="true">
50,000
</td>
<td width="100">
</td>
<td> </td>
<td style="font-weight: bold;">
</td>
</tr>
<tr class="xsmall">
<td nowrap="true">
111111111111
</td>
<td> </td>
<td width="100">
</td>
【问题讨论】: