Share via

VBA Code

Anonymous
2019-08-19T14:07:13+00:00

I have the following VBA Code and i am trying to figure out why it is not working. There are no error messages when i run the code but i get none of the informaiton that I am trying to pull from outlook when i know there are plenty of emials in there. I have watched videos and read a few different formulas like this one and i am wondering if i need to put my email address in the code somewhere so it knows what email in outlook to pull from. Any advice would be appreciated.

Sub getDataFromOutlook()

Dim OutlookApp As Outlook.Application

Dim OutlookNameSpace As Namespace

Dim Folder As MAPIFolder

Dim OutlookMail As Variant

Dim i As Integer

Set OutlookApp = New Outlook.Application

Set OutlookNameSpace = OutlookApp.GetNamespace("MAPI")

Set Folder = OutlookNameSpace.GetDefaultFolder(olFolderInbox).Folders("DDQ")

i = 1

For Each OutlookMail In Folder.Items

If OutlookMail.ReceivedTime >= Range("Email_Receipt_Date").Value Then

Range("email_subject").Offset(i, 0) = OutlookMail.Subject

Range("email_subject").Offset(i, 0).Columns.AutoFit

Range("email_subject").Offset(i, 0).VerticalAlignmant = xlTop

Range("email_Date").Offset(i, 0).Value = OutlookMail.ReceivedTime

Range("email_Date").Offset(i, 0).Columns.AutoFit

Range("email_Date").Offset(i, 0).VerticalAlignment = xlTop

Range("email_Sender").Offset(i, 0).Value = OutlookMail.SenderName

Range("email_Sender").Offset(i, 0).Columns.AutoFit

Range("email_Sender").Offset(i, 0).VertricalAlignment = xlTop

Range("email_Body").Offset(i, 0).Value = OutlookMail.Body

Range("email_Body").Offset(i, 0).Columns.AutoFit

Range("email_Body").Offset(i, 0).VertricalAlignment = xlTop

i = i + 1

End If

Next OutlookMail

Set Folder = Nothing

Set OutlookNameSpace = Nothing

Set OutlookApp = Nothing

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2019-08-20T04:15:04+00:00

    You misunderstood me, I said debug your macro. That does not mean run the macro.

    Debugging means execute the macro line by line, use the Watch window to explore variables and/or use the Immediate window to show (Debug.Print) what values some variables have at some point.

    Have a look into this tutorial, section "Debugging in VBA"

    https://www.wiseowl.co.uk/blog/s161/online-exce...

    It's worth working through the example shown there, then you'll be able to spot the problem with your code very easily.

    Andreas.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2019-08-19T20:59:16+00:00

    There are emails in the folder and they are all within the date range. When i run the debugger there are no errors

    Was this answer helpful?

    0 comments No comments
  3. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2019-08-19T14:38:58+00:00

    There are two possible reasons for this.

    A) The folder contains no mails
    B) All mails are older as the date in Range("Email_Receipt_Date")

    Debug your code and see what happens.
    https://www.excel-easy.com/vba/examples/debuggi...

    Disclaimer: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.

    Andreas.

    Was this answer helpful?

    0 comments No comments