简体   繁体   中英

how to save the outlook instant search result emails in hard drive folder

I am trying to save all the emails, resulting out of instant text search into the hard drive folder. the below code is able to perform the search but giving me an error at selectallitems line while selecting each mail and saving them in HD. code is in excel vba;

Dim OlApp As Outlook.Application
Set OlApp = CreateObject("Outlook.Application")


Dim fldrpath As String
fldrpath = "\\mydata\EMAILS\

Check subfolder for messages and exit of none found
txtsearch = "abc@xyz.com, received:4/1/2017..4/30/2017"
OlApp.ActiveExplorer.Search txtsearch, olSearchScopeAllFolders

Dim myitem As Outlook.MailItem
Dim objitem As Object
Set myitem = OlApp.ActiveExplorer.SelectAllItems
Set objitem = myitem
objitem.SaveAs fldrpath & "test" & ".msg", olMSG

Any other alternative code to get the emails saved will also be appreciated. Thanks in advance !! looking for a quick solution

Saving search results appears to be more easily achieved a different way.

From Outlook, not Excel.

Sub SearchForStr_Save()

    Dim strSearch As String
    Dim strDASLFilter As String
    Dim strScope As String

    Dim objItem As Object

    Dim objSearch As search
    Dim srchFolder As folder
    Dim fldrpath As String

    strSearch = "abc@xyz.com"
    strDASLFilter = "urn:schemas:httpmail:textdescription LIKE '%" & strSearch & "%'"

    strScope = "'Inbox'"

    Set objSearch = AdvancedSearch(Scope:=strScope, filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")

    Set srchFolder = objSearch.Save(strSearch)

    'fldrpath = "\\mydata\EMAILS\"
    fldrpath = "h:\test\"

    For Each objItem In srchFolder.Items
        'Debug.Print objItem.subject
        If objItem.Class = olMail Then
            objItem.SaveAs fldrpath & "test" & ".msg", olMsg
        End If
    Next

ExitRoutine:
    Set objSearch = Nothing
    Set srchFolder = Nothing

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM