Consulting

Results 1 to 7 of 7

Thread: Please help me find my UID!!

  1. #1
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    5
    Location

    Please help me find my UID!!

    Hi everyone - looking to accomplish a specific action in Outlook. Me: taught myself Batch and BASIC and took some C++ in college, but never went much further. So I understand programming, but struggle a bit with object oriented programming. The extent of my VBA is having created a UserForm to ask for some information and plug it into a form letter my office uses.

    What I'd like to accomplish is simple conceptually ... while I have an email selected in the main/Mail pane, I'd like ... I want to execute a macro that:

    1) finds a unique identifier in the subject field of the email; and then
    2) searches the UID in subject/body in my Inbox and all subfolders (basic search, not Advanced).

    Does the code really have to get into DASL/DAV Searching and Locating and MAPI?!

    Outlook doesn't have a Record Macro function so the best I can even jump off with is a lame search macro from Word ... I'm sure this doesn't come close to the differences between searching an email vs of a Word document (for step 1), never mind the complexities of searching Outlook versus as opposed to one Word document. But I have to imagine the Search field in Outlook is an object that should make it simple enough?!?! (famous last words)

    I've been in and out of dozens of Microsoft help pages and forums and finally decided to just ask.

    Sub ForOutlook()
    '
    ' ForOutlook Macro
    '
    '
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "<the UID would go here>"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindAsk
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    End Sub
    Last edited by pebcak; 02-22-2021 at 07:07 AM.

  2. #2
    The concept may seem simple, but there are some issues that need explanation.
    How is the UID identified initially? If it's a constant value, then why bother with the original message, when you could just look for the UID in the inbox? If not, how is it determined?
    What do you want to do with the messages that contain the UID?
    Because of the processes involved, this could take a long time to run if you have a lot of stored messages.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    5
    Location
    Quote Originally Posted by gmayor View Post
    The concept may seem simple, but there are some issues that need explanation.
    How is the UID identified initially? If it's a constant value, then why bother with the original message, when you could just look for the UID in the inbox? If not, how is it determined?
    What do you want to do with the messages that contain the UID?
    Because of the processes involved, this could take a long time to run if you have a lot of stored messages.
    Thank you so much for your response ... I suppose the searching part is a bit more ambitious than I anticipated.

    What about a macro that just searches the subject field of the currently selected email to find the UID and copy that into the clipboard?

    The UID always follows the format of four numbers a hyphen and six numbers. The first four numbers are a year, the last six numbers are a serial number. So it's safe to say the first two numbers of the year will always start with a 20 and the first number of the serial will start with a 0, e.g., 20##-0#####. Case numbers look like 2019-012345, 2020-029415 and 2021-000325.

  4. #4
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    5
    Location
    Quote Originally Posted by pebcak View Post
    Thank you so much for your response ... I suppose the searching part is a bit more ambitious than I anticipated.

    What about a macro that just searches the subject field of the currently selected email to find the UID and copy that into the clipboard?

    The UID always follows the format of four numbers a hyphen and six numbers. The first four numbers are a year, the last six numbers are a serial number. So it's safe to say the first two numbers of the year will always start with a 20 and the first number of the serial will start with a 0, e.g., 20##-0#####. Case numbers look like 2019-012345, 2020-029415 and 2021-000325.

    OK so I've almost got it to set a string as the subject line.

    Sub FindMatterNumber ()

    Dim strSubject as String
    Dim myItem as Outlook.MailItem

    strSubject = myItem.Subject

    MsgBox "Subject is: " & strSubject

    End Sub


    But it's breaking on the strSubect = my item.Subject ... I don't know how to identify the Subject

  5. #5
    You have to tell the macro which message myItem refers to. If it is a selected item then:
    Sub FindMatterNumber()
    Dim strSubject As String
    Dim myItem As Outlook.MailItem
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set myItem = ActiveInspector.currentItem
            Case olExplorer
                Set myItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        strSubject = myItem.Subject
        MsgBox "Subject is: " & strSubject, vbInformation
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    5
    Location
    Quote Originally Posted by gmayor View Post
    You have to tell the macro which message myItem refers to. If it is a selected item then:
    Sub FindMatterNumber()
    Dim strSubject As String
    Dim myItem As Outlook.MailItem
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set myItem = ActiveInspector.currentItem
            Case olExplorer
                Set myItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        strSubject = myItem.Subject
        MsgBox "Subject is: " & strSubject, vbInformation
    lbl_Exit:
        Exit Sub
    End Sub
    This works great!!! NOW ... I'm going to reverse-engineer it , try to work out the rest of what I want little by little, fail miserable, and be back with more questions.

    But in the meantime THANK YOU!!

  7. #7
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    5
    Location
    Quote Originally Posted by gmayor View Post
    You have to tell the macro which message myItem refers to. If it is a selected item then:
    Sub FindMatterNumber()
    Dim strSubject As String
    Dim myItem As Outlook.MailItem
        On Error Resume Next
        Select Case Outlook.Application.ActiveWindow.Class
            Case olInspector
                Set myItem = ActiveInspector.currentItem
            Case olExplorer
                Set myItem = Application.ActiveExplorer.Selection.Item(1)
        End Select
        strSubject = myItem.Subject
        MsgBox "Subject is: " & strSubject, vbInformation
    lbl_Exit:
        Exit Sub
    End Sub
    Fiddling around a bit in the VBE and running the macro a bunch of times - it seems like the Case is always olExplorer, so why the distinction?

    Thought, to be blunt, I'm not sure what the difference between olInspector and olExplorer is (I read the Microsoft Docs article, still confused ... I really need a primer on this but not back to the basics, any recommendations? This OOP stuff is killing me.)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •