excel - 从 Outlook 电子邮件主题中提取部分内容,然后在 Excel 电子表格中找到它

标签 excel vba outlook

我的电子邮件主题如下所示: 回复:Blah blah - Blah blah,1234-56,Blah blah blah

我想知道如何从电子邮件主题中提取 1234-56

我尝试过:

If myemail.Subject Like "****-**" Then

但这只能告诉我真/假,我想要实际数据,1234-56

此外,我希望使用电子表格上的查找功能来查找数据所在的位置。

例如,由于电子邮件主题包含 1234-56,我将在电子表格中使用查找功能查找 1234-56

我尝试过:

Cells.Find( _
    What := myemail.Subject, _
    After := ActiveCell, _
    LookIn := xlValues, _
    LookAt := xlPart, _
    SearchOrder := xlByRows, _
    SearchDirection := xlNext, _
    MatchCase := False, _
    SearchFormat := False).Activate

但我认为这不是正确的语法。
预先感谢您的帮助!

最佳答案

您可以使用正则表达式来完成此任务。下面的示例展示了如何在随机字符串中找到类似于 ####-##(四位数字、破折号、两位数字)的模式。

Option Explicit

Public Sub RegEx()
    'this is your subject
    Const Subject As String = "RE: Blah blah - Blah blah, 1234-56, Blah blah blah"

    Dim objRegEx As Object
    Set objRegEx = CreateObject("vbscript.regexp")

    With objRegEx
        .Global = True
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "[0-9]{4}-[0-9]{2}" 'this is the pattern to find ####-## where # is a number from 0-9

        Dim objMatches As Object
        Set objMatches = .Execute(Subject)
    End With

    'print all matches in the intermediate window
    Dim objMatch As Object
    For Each objMatch In objMatches
        Debug.Print objMatch
    Next
End Sub

要测试和构建模式,您可以轻松使用 regex101.com

关于excel - 从 Outlook 电子邮件主题中提取部分内容,然后在 Excel 电子表格中找到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811134/

相关文章:

excel - 单击 USPS 网站上的按钮查找 9 位数邮政编码

java - 将表示 Excel 中日期的数字转换为 Java Date 对象

vba - 使用 VBA 隐藏/取消隐藏 Excel 中的特定对象

vba - 尝试将 Excel 图表复制到 Power Point 演示文稿时出现下标超出范围错误

c# - 在 Outlook 2013 C# VSTO 项目中,为什么 Explorer SelectionChange 事件会触发两次

r - 将大量行转换为一列?

c# - 为 VBA 添加引用对话框公开时,为 .NET (COM) tlb 库选择自定义引用名称

macos - 使用 AppleScript 移动和合并文件夹和文件(第 2 部分,需要帮助修改)

excel - 使用 Excel 中的 VBA 打开 Outlook 邮件 .msg 文件

excel - 将 Excel Range 发布到 HTML 时文本被截断