在Excel工作表中搜索Outlook电子邮件中突出显示的文本

在Excel工作表中搜索Outlook电子邮件中突出显示的文本,excel,vba,outlook,find,Excel,Vba,Outlook,Find,每天我都会收到写有项目编号的电子邮件。我通常突出显示并复制电子邮件中的项目编号,单击我的excel电子表格(WI_Design_Tracker),ctrl+F(查找)并将项目编号粘贴到“查找”字段中,然后单击“查找下一个”。我正在尝试创建一个宏,它将缩短这个过程,因为我每天要做一百次。我发现一个宏的作用正好相反(在Excel中查找突出显示的数字,并通过Outlook搜索以查找该电子邮件。我试图对其进行修改以符合我的目的,但这超出了我的技能水平。如有任何帮助,将不胜感激。以下是我试图转换的代码,用

每天我都会收到写有项目编号的电子邮件。我通常突出显示并复制电子邮件中的项目编号,单击我的excel电子表格(WI_Design_Tracker),
ctrl+F
(查找)并将项目编号粘贴到“查找”字段中,然后单击“查找下一个”。我正在尝试创建一个宏,它将缩短这个过程,因为我每天要做一百次。我发现一个宏的作用正好相反(在Excel中查找突出显示的数字,并通过Outlook搜索以查找该电子邮件。我试图对其进行修改以符合我的目的,但这超出了我的技能水平。如有任何帮助,将不胜感激。以下是我试图转换的代码,用于在Excel工作表中搜索我在Outlook电子邮件中突出显示的项目编号

'Code:
Option Explicit

Public Sub Search_Outlook_Emails()

    Dim outApp As Outlook.Application
    Dim outNs As Outlook.Namespace
    Dim outStartFolder As Outlook.MAPIFolder
    Dim foundEmail As Outlook.MailItem

    Set outApp = New Outlook.Application
    Set outNs = outApp.GetNamespace("MAPI")

    'Start at Inbox's parent folder
    Set outStartFolder = outNs.GetDefaultFolder(Outlook.olFolderInbox).Parent

    'Or start at folder selected by user
    'Set outStartFolder = outNs.PickFolder


    If Not outStartFolder Is Nothing Then

        Set foundEmail = Find_Email_In_Folder(outStartFolder, ActiveCell.Value)

        If Not foundEmail Is Nothing Then

            If MsgBox("Email subject: " & foundEmail.Subject & vbNewLine & vbNewLine & _
                      "Folder: " & foundEmail.Parent.FolderPath & vbNewLine & vbNewLine & _
                      "Open the email?", vbYesNo, "'" & ActiveCell.Value & "' found") = vbYes Then

                foundEmail.Display

            End If

        Else

            MsgBox "", vbOKOnly, "'" & ActiveCell.Value & "' not found"

        End If

    End If

End Sub

Private Function Find_Email_In_Folder(outFolder As Outlook.MAPIFolder, findText As String) As Outlook.MailItem

    Dim outItem As Object
    Dim outMail As Outlook.MailItem
    Dim outSubFolder As Outlook.MAPIFolder
    Dim i As Long

    Debug.Print outFolder.FolderPath

    Set Find_Email_In_Folder = Nothing

    'Search emails in this folder

    i = 1
    While i <= outFolder.Items.Count And Find_Email_In_Folder Is Nothing

        Set outItem = outFolder.Items(i)

        If outItem.Class = Outlook.OlObjectClass.olMail Then

            'Does the findText occur in this email's body text?

            Set outMail = outItem
            If InStr(1, outMail.Body, findText, vbTextCompare) > 0 Then Set Find_Email_In_Folder = outMail

        End If

        i = i + 1

    Wend

    DoEvents

    'If not found, search emails in subfolders

    i = 1
    While i <= outFolder.Folders.Count And Find_Email_In_Folder Is Nothing

        Set outSubFolder = outFolder.Folders(i)

        'Only check mail item folders

        If outSubFolder.DefaultItemType = Outlook.olMailItem Then Set Find_Email_In_Folder = Find_Email_In_Folder(outSubFolder, findText)

        i = i + 1

    Wend

End Function
”代码:
选项显式
公共子搜索\u Outlook\u电子邮件()
将outApp设置为Outlook.Application
将outNs设置为Outlook.Namespace
将OutpartFolder暗显为Outlook.MAPIFolder
将电子邮件设置为Outlook.MailItem
Set-outApp=新建Outlook.Application
Set-outNs=outApp.GetNamespace(“MAPI”)
'从收件箱的父文件夹开始
设置outpartfolder=outNs.GetDefaultFolder(Outlook.olFolderInbox).Parent
'或从用户选择的文件夹开始
'设置outpartfolder=outNs.PickFolder
如果不是OutpartFolder,那么它什么都不是
Set foundEmail=Find_Email_In_Folder(outpartfolder,ActiveCell.Value)
如果找不到电子邮件,那就什么都不是了
如果MsgBox(“电子邮件主题:”&foundEmail.subject&vbNewLine&vbNewLine&_
“文件夹:”&foundEmail.Parent.FolderPath&vbNewLine&vbNewLine&_
“打开电子邮件?”,vbYesNo,“”&ActiveCell.Value&“found”)=vbYes然后
foundEmail.Display
如果结束
其他的
MsgBox“”、vbOKOnly“”、&ActiveCell.Value&“未找到”
如果结束
如果结束
端接头
私有函数在文件夹(outFolder为Outlook.MAPIFolder,findText为String)中作为Outlook.MailItem查找电子邮件
将项目作为对象
将outMail设置为Outlook.MailItem
将outSubFolder暗显为Outlook.MAPIFolder
我想我会坚持多久
Debug.Print outFolder.FolderPath
设置在文件夹中查找电子邮件=Nothing
'在此文件夹中搜索电子邮件
i=1
而我0则设置在文件夹中查找电子邮件=outMail
如果结束
i=i+1
温德
多芬特
'如果未找到,请在子文件夹中搜索电子邮件
i=1

虽然我感谢Tim提供的链接。这非常简单。我以前见过该代码,但无法使其正常工作,因此我再次尝试。以下是我的最终结果。它仍然可以使用一些调整和错误处理,但这就是目前的工作:

Sub FindOutlookValue()

Dim OutApp As Object
Dim OutMail As Object
Dim olInsp As Object
Dim WdDoc As Object
Dim strText As String

On Error Resume Next

'Get Outlook if it's running
Set OutApp = GetObject(, "Outlook.Application")
Set OutMail = OutApp.ActiveExplorer.Selection.Item(1)

With OutMail
    Set olInsp = .GetInspector
    Set wdDoc = olInsp.WordEditor
    strText = WdDoc.Application.Selection.Range.Text
End With

'Find strText in Excel
Dim cl As Range
With Worksheets("MyWorksheet").Cells
    Set cl = .Find(strText, After:=.Range(A1), LookIn:=xlValues)
    If Not cl Is Nothing Then
       cl.Select
    End If
End With

End Sub

你发布的代码在这里并不重要:如果有什么可能会让人们想知道为什么-如果你有这样的代码-你不能从你描述的开始。这里是一个很好的开始-你可能想发布你的电子表格和电子邮件的示例数据