Results 1 to 2 of 2

Thread: Copy a URL from a specific subject mail email body into excel worksheet

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009

    Copy a URL from a specific subject mail email body into excel worksheet

    I want to copy the URL/Hyperlink from an outlook email which is received daily and consists of a 2 URL/hyperlinks and I want the second URL that starts with certain text like: http://mytkportal.si/sff/kp/sfsd=21133, the part highlighted in bold is same everyday but the last number keeps changing, So I want the macro to pull that link and paste into excel worksheet cell. I have got the below code which gets the entire email body but i need only the URL/hyperlink as mentioned above.

    Sub CopyEmailtoExcel()
    Dim olApp As Outlook.Application
    Dim olNs As Namespace
    Dim Fldr As MAPIFolder
    Dim olMi As Variant
    Dim i As Integer
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("Test")
    Set Fldr = Fldr.Folders("MyFolder")
    Set inboxItems = Fldr.Items
    pnldate = Format((Date - 1), "mm/dd/yyyy")
    Set inboxItems = Fldr.Items
    inboxItems.Sort "[ReceivedTime]", True
    For i = 1 To Fldr.Items.Count Step 1
        Set olMi = Fldr.Items(i)
            If Format(olMi.ReceivedTime, "mm/dd/yyyy") = pnldate Then
                Debug.Print olMi.ReceivedTime
                Debug.Print olMi.Subject
                If InStr(1, olMi.Subject, "Breakdown") > 0 Then
                    Sheets("Sheet1").Range("A1") = olMi.Body
                    GoTo AllDone
                End If
            End If
    Next i
    End Sub

  2. #2
    Join Date
    Dec 2004

    Re: Copy a URL from a specific subject mail email body into excel worksheet

    try like
    mylink = "http://mytkportal.si/sff/kp/sfsd="
    pos = instr(olMi.Body, mylink)
    if pos > 0 then
        Sheets("Sheet1").Range("A1") = mid(olMi.body, pos, len(mylink)+5)
    end if
    if the length of the numeric can change from 5 digits you would have to find the end position, using instr again to find the end of the link
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed

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


Click Here to Expand Forum to Full Width