How to reference items in the Inbox..

HI,
Can someone show me how to reference items in Inbox? I would like to have the option to select the items or
all( meaning it would loop to all items in the inbox) without selection.
zachvaldezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David LeeCommented:
Hi zachvaldez,

> show me how to reference items in Inbox
Reference them how, in code?  If yes, then here's code fragment showing how:

Dim olkInbox as Outlook.Items, _
    olkItem as Object
Set olkInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
For Each olkItem in olkInbox
    'Code for whatever it is you want to do
Next

Cheers!
zachvaldezAuthor Commented:
'Code for whatever it is you want to do>> thanks can you show me some examples that can be done in this space...
David LeeCommented:
zachvaldez,

> can you show me some examples that can be done in this space
It'd probably be more useful if you gave me some ide of what you want to do rather than me just tossing out possibilities.  That aside, here are a couple of examples.

    'Flag all items with a red flag (Outlook 2003)
    olkItem.FlagIcon = olRedFlagIcon
    olkitem.Save

    'Set the category
    olkItem.Categories = "My Category"
    olkItem.Save

    'Print the item
    olkItem.PrintOut
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

zachvaldezAuthor Commented:
say, save the attachment if any of an item in the inbox toi"C:\Temp". is this possible?
David LeeCommented:
Sure.

    If olkItem.Attachments.Count > 0 Then
        For Each olkAttachment In olkItem.Attachments
            olkAttachment.SaveAsFile "C:\Temp\" & olkAttachment.FileName
        Next
    End If
zachvaldezAuthor Commented:
Set olkInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items>>> it is ot hitting the inbox that I want to read...
David LeeCommented:
The the "inbox that I want to read" must not be the default inbox.  When someone says "Inbox" I assume they mean the default inbox.  It's not a problem though, we can fix the code to handle any folder.  You'll need to add the code below and the change this line to

    Set olkInbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items

to

    Set olkInbox = OpenMAPIFolder("\Folder Name\Inbox")

Replace "Folder Name" with the name of the mailbox or PST as it appears in Outlook (i.e. not the name of the file on disk).


'Credit where credit is due.
'The code below is not mine.  I found it somewhere on the internet but do
'not remember where or who the author is.  The original author(s) deserves all
'the credit for these functions.
Function OpenMAPIFolder(szPath)
    Dim app, ns, flr, szDir, i
    Set flr = Nothing
    Set app = CreateObject("Outlook.Application")
    If Left(szPath, Len("\")) = "\" Then
        szPath = Mid(szPath, Len("\") + 1)
    Else
        Set flr = app.ActiveExplorer.CurrentFolder
    End If
    While szPath <> ""
        i = InStr(szPath, "\")
        If i Then
            szDir = Left(szPath, i - 1)
            szPath = Mid(szPath, i + Len("\"))
        Else
            szDir = szPath
            szPath = ""
        End If
        If IsNothing(flr) Then
            Set ns = app.GetNamespace("MAPI")
            Set flr = ns.Folders(szDir)
        Else
            Set flr = flr.Folders(szDir)
        End If
    Wend
    Set OpenMAPIFolder = flr
End Function

Function IsNothing(Obj)
  If TypeName(Obj) = "Nothing" Then
    IsNothing = True
  Else
    IsNothing = False
  End If
End Function
zachvaldezAuthor Commented:
This is what Im trying to do... I have my own inbox(personal) but I have also access to a public folder named "Union"-This is the inbox I would like to read not my personal inbox.
I would like to initiate this from an Access application. Ive seen a code from outlookcode.com but only if you selected the item you want to save in your hard dreive.
It made use of if I remember .activeexplore and .selection. But that is not what i want. I want the flexibility of reading all the email with attachment and strip attachment and save to a file folder...
zachvaldezAuthor Commented:
Blue. that was outstanding thanks for the guy who provided the code. it is outstanding as well.
Is there a way to filter duplicates when it saves to the hard drive.
somewhere here maybe..

  If olkItem.Attachments.Count > 0 Then
        For Each olkAttachment In olkItem.Attachments
            olkAttachment.SaveAsFile "C:\Temp\" & olkAttachment.FileName
        Next
    End If
David LeeCommented:
Yep, that's doable.  Try this:

Dim olkInbox as Outlook.Items, _
    olkItem as Object, _
    olkAttachment as Object, _
    objFSO as Object, _
    strRootFolderPath as String, _
    strFilename as String, _
    intCount as Integer
strRootFolderPath = "C:\Temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set olkInbox = OpenMAPIFolder("\Folder Name\Inbox")
For Each olkItem in olkInbox
    If olkItem.Attachments.Count > 0 Then
        For Each olkAttachment In olkItem.Attachments
            strFilename = olkAttachment.FileName
            intCount = 0
            Do While True
                If objFSO.FileExists(strRootFolderPath & strFilename) Then
                    intCount = intCount + 1
                    strFilename = "Copy (" & intCount & ") of " & olkAttachment.FileName
                Else
                    Exit Do
                End If
            Loop
            olkAttachment.SaveAsFile strRootFolderPath & strFilename
        Next
    End If
Next

               
zachvaldezAuthor Commented:
Bkue.. what iss it doing here. reading it , it looks like it would add the label " copy 1 of attachment abc1112...." for duplicates
Is it possible for the routine to just ignore if it sees that it is a duplicate and not write the file. My reason is that I would like to import all the files saved in that directory and if they are all excel files, i do not need to import duplicate files to my access database?

One more if is not that too difficult too. Is it possible to strip the attachment from Outlook before or after it saves in he folder.


  Do While True
                If objFSO.FileExists(strRootFolderPath & strFilename) Then
                    intCount = intCount + 1
                    strFilename = "Copy (" & intCount & ") of " & olkAttachment.FileName
                Else
                    Exit Do
                End If
            Loop
            olkAttachment.SaveAsFile strRootFolderPath & strFilename
David LeeCommented:
Try this.

Dim olkInbox as Outlook.Items, _
    olkItem as Object, _
    olkAttachment as Object, _
    objFSO as Object, _
    strRootFolderPath as String, _
    strFilename as String, _
    intCount as Integer
strRootFolderPath = "C:\Temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set olkInbox = OpenMAPIFolder("\Folder Name\Inbox")
For Each olkItem in olkInbox
    If olkItem.Attachments.Count > 0 Then
        For intCount = olkItem.Attachments.Count to 1 Step -1
            Set olkAttachment = olkItem.Attacments(intCount)
            strFilename = olkAttachment.FileName
            If Not objFSO.FileExists(strRootFolderPath & strFilename) Then
                olkAttachment.SaveAsFile strRootFolderPath & strFilename
            End If
            olkAttachment.Delete
            olkItem.Save
        Next
    End If
Next

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zachvaldezAuthor Commented:
Blue..
   If Not objFSO.FileExists(strRootFolderPath & strFilename) Then
                olkAttachment.SaveAsFile strRootFolderPath & strFilename
            End If>>>>>>>>>> should this be an Else instead
            olkAttachment.Delete >>> what did it delete here?

Also after stripping the excel file attachment, will it save the outlook item again with no attachment because it was already removed?
David LeeCommented:
Yes.
zachvaldezAuthor Commented:
Blue..
Which did you answer from my last question? thanks
David LeeCommented:
Sorry, I was answering the last question "will it save the outlook item again with no attachment because it was already removed".  I hadn't noticed the other questions.  No, it shouldn't be an Else.  The attachment should be deleted either after it's saved or if a file with the same name already exists in the folder.  "What did it delete here?"  It deleted the attachment.
zachvaldezAuthor Commented:
Great! Now I want to load all the excel files I extracted to an access table.
the excel file has same format  
David LeeCommented:
Sorry, I'm not as good with Excel.  I recommend a separate question dealing with that issue.
zachvaldezAuthor Commented:
Thanks! I got it now..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.