2

编辑:在帖子底部添加了更接近我试图操作的实际字符串的片段。这不是逐字删除的关键信息,而是我从我要转发的电子邮件中复制和粘贴的。只是试图在中间和前面提取表格。

我想删除我收到的电子邮件中的特定文本,删除超链接,保留格式,然后转发到特定地址。

基本上,电子邮件的结构如下:

“亲爱的用户,

请在下面找到相关信息

数据库:记录点击这里在线查看

  • 表头
  • 列标题
  • 记录 1
  • 记录 2
  • 记录 3
  • ...
  • 最终记录

如果您在使用上面的链接时遇到问题,请将此链接粘贴到您的地址栏中以访问在线数据库:

somewebsite.database.net/tr/vuioahdf

这是一封自动发送的电子邮件……”

我需要删除表格前的文字,删除超链接,保留格式,然后转发。

我尝试了以下。

Sub RemoveExpression()

Dim Insp As Inspector
Dim obj As Object

    Set Insp = Application.ActiveInspector
    Set obj = Insp.CurrentItem

    obj.Body = Replace(obj.Body, "Dear user, please find the relevant information below database: records click here to view this online - Table header " & _
                                    vbNewLine & "If you have trouble using the link above, paste this link in your address bar to go to the online database http://somewhere.database.net/tr/vuioahdf This is an automated email...", "")

    Set obj = Nothing
    Set Insp = Nothing

End Sub

它给了我一个关于未定义对象的错误,但这与我想要做的事情一致。删除表格前后的一些文字,删除超链接,然后转发到特定地址。删除超链接可能不是完全必要的,因为它主要位于表格前后的文本中,但表格标题中有一个超链接。

我试图操纵的电子邮件:

Dear User,<br>
<br>
Please find below drawing received last week for project
<br> <br>
<b><u></u>Document Control: Projects<u></u></b><br>
Click <a href="somehwere.net" target="_blank">here</a> to see this report on the database.
<br> <br>
<div style="font-size:70%;font-family:Verdana,Helvetica,Geneva,sans-serif">
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr style="background-color:#c4defb">
<td style="border-top:1px solid #55a0ef;border-bottom:1px solid #55a0ef">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td style="width:200px;padding:4px 8px;font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;color:#505050;font-weight:normal;font-size:9pt">
<a style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;color:#505050;font-weight:normal;font-size:9pt;text-decoration:none" href="somewhere.net" title="See this report" target="_blank">Full Report</a>
| <a style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;color:#505050;font-weight:normal;font-size:9pt;text-decoration:none" href="somewhere.net" title="Edit all the Drawings shown below" target="_blank">Grid Edit</a>
</td>
<td style="width:200px;padding:4px 8px;font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;color:#505050;font-weight:normal;font-size:9pt" align="right">
<span><label>Date Created</label> is during <label> &#39;last 2 wk&#39; </label> AND ...</span>    <span>11 Drawings</span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width="100%" cellspacing="0" cellpadding="0">
<thead style="display:table-header-group">
<tr>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"> </td>
<td align="right" style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><div></div><span>Date Created</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Project Name</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Sub Project</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Project Description</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Drawing Number</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Revision</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Title</span><span></span></td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;background-color:#f6f9fd;padding:3px 4px;font-weight:bold;font-size:10pt;color:#111111"><span>Transmittal</span><span></span></td>
</tr>
</thead>
<tbody style="display:table-row-group">
<tr>
<td nowrap style="border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111"> </td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111" align="right">02-18-2016</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">Rig xxx</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">120</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">Structure</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">00813-121-000-001WM</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">0</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">REAR FRAME</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">02182016</td>
</tr>
<tr style="background-color:#f2f2f2">
<td nowrap style="border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111"> </td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111" align="right">02-18-2016</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">Rig xxx</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">120</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">Structure</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">00813-121-000-000</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">0</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">REAR ASSEMBLY</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">01282016</td>
</tr>
<tr>
<td nowrap style="border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111"> </td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111" align="right">02-18-2016</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">Rig xxx</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">120</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">Structure</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">00813-121-000-001</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">0</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">REAR FRAME</td>
<td style="font-family:Helvetica Neue,Helvetica,Tahoma,Arial,sans-serif;border-left:1px solid #ccc;border-bottom:1px solid #ccc;padding:3px 4px;font-size:10pt;color:#111111">01282016</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</div>
</div>
<br>
If you have trouble using the link above, copy and paste the following Web address into the address bar of your browser:
<br>   <a href="somewhere.net" target="_blank">somewhere.net</a>
<br> <br>
This is an automated email from an online application. If you feel that you received this email in error, please contact the application's manager,
<a href="mailto:someone@projects.net" target="_blank">someone@projects.net</a>
</td></tr></table>
</div>
</font></div></table></table></div></div></body></html>

4

1 回答 1

1

您收到的 a的Body属性MailItem是只读的。这可能就是您收到错误的原因。您需要转发消息,获取转发的消息,然后对其进行操作Body

如果您收到一封电子邮件,其中包含一堆东西,包括一个表格,并且您只想转发表格,您可以使用这样的代码

Public Sub ForwardTableOnly()

    Dim olMail As MailItem
    Dim lTblStart As Long, lTblEnd As Long

    Set olMail = ActiveInspector.currentItem.Forward

    lTblStart = InStr(1, olMail.HTMLBody, "<table")
    lTblEnd = InStr(lTblStart, olMail.HTMLBody, "</table")

    olMail.HTMLBody = Mid$(olMail.HTMLBody, lTblStart, lTblEnd - lTblStart + 1)
    olMail.Display

End Sub

它找到桌子在HTMLBody财产中的位置并摆脱其他一切。这行得通,但比这更复杂。如果您想保留表格和其他一些东西,您将有更复杂的字符串操作。如果格式对您很重要,您将遇到另一个问题。表格的格式很可能是使用进一步定义的样式HTMLBody以及一堆其他样式。那是更多的字符串操作。但是字符串操作如果不乏味和无聊的话就足够简单了。

相关点是:调用 Forward 方法并将生成的 MailItem 分配给对象变量。然后将该对象的HTMLBody属性设置为您想要的任何内容。在此示例中,我使用该.Display属性来显示消息。你会想要使用.Toand.Send可能而不是.Display.

于 2016-03-02T16:37:01.397 回答