Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / October 2005

Tip: Looking for answers? Try searching our database.

Outlook.Items of mail items are empty

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Reiner - 17 Oct 2005 17:26 GMT
Hi,

I try to write a macro that deletes double (and more) emails. Take a
look on the code (you can input a folder structure with up to three
folders).

----------------------------------------------------------------
Sub DoppelteLoeschen()

  Dim olNameSpace As NameSpace
  Dim olInputBox As MAPIFolder
  Dim olFolderInbox As MAPIFolder
  Dim olFolderSent As MAPIFolder
  Dim InboxItems As Outlook.MailItem
  Dim ItemSearch As Outlook.MailItem
  Dim ItemFound As Outlook.MailItem
  Dim ItemsToSearch As Outlook.Items
  Dim ItemsRestricted As Outlook.Items
  Dim bDoNext As Boolean
  Dim intMails As Integer, i As Integer, j As Integer
  Dim szMajorFolder, szText, szTitle, szDefault, szFolder As String,
szSubFolder As String
  Dim deInDate1 As Date, deSendDate1 As Date, szSenderName1 As String,
szSubject1 As String
  Dim deInDate2 As Date, deSendDate2 As Date, szSenderName2 As String,
szSubject2 As String
  On Error Resume Next
  Set olNameSpace = Application.GetNamespace("MAPI")
  szMajorFolder = "Reiner"
  szTitle = "Ordnerbestimmung"
  szDefault = "Reiner"
  szText = "Bitte geben Sie den Namen des Hauptordners unter
<Persönliche Ordner> ein."
  szMajorFolder = InputBox(szText, szTitle, szDefault)
  If Len(szMajorFolder) = 0 Then
     szMajorFolder = "Reiner"
  End If

  szDefault = ""
  szText = "Bitte geben Sie den Namen des zu bearbeitenden Ordners
unter " + szMajorFolder + " ein."
  szFolder = InputBox(szText, szTitle, szDefault)
  If szFolder = "ENDE" Then
     i = MsgBox("Makro ohne Aktion beendet", vbOKOnly, "Makro
beendet")
     Exit Sub
  End If

  If Len(szFolder) > 0 Then
     szDefault = ""
     szText = "Bitte geben Sie den Namen des zu bearbeitenden Ordners
unter " + szMajorFolder + " und " + szFolder + " ein."
     szSubFolder = InputBox(szText, szTitle, szDefault)
     If szSubFolder = "ENDE" Then
        i = MsgBox("Makro ohne Aktion beendet", vbOKOnly, "Makro
beendet")
        Exit Sub
     End If
  End If

  If szMajorFolder = "Posteingang" Then
     Set olInputBox = olNameSpace.GetDefaultFolder(olFolderInbox)
  ElseIf szMajorFolder = "Gesendete Objekte" Then
     Set olInputBox = olNameSpace.GetDefaultFolder(olFolderSent)
  ElseIf Len(szFolder) = 0 Then
     Set olInputBox =
Application.GetNamespace("MAPI").Folders("Persönliche
Ordner").Folders(szMajorFolder)
  ElseIf Len(szSubFolder) = 0 Then
     Set olInputBox =
Application.GetNamespace("MAPI").Folders("Persönliche
Ordner").Folders(szMajorFolder).Folders(szFolder)
  Else
     Set olInputBox =
Application.GetNamespace("MAPI").Folders("Persönliche
Ordner").Folders(szMajorFolder).Folders(szFolder).Folders(szSubFolder)
  End If
  'olInputBox.Items.Sort "[SentOn]" + "[Subject]" + "[SenderName]",
True
  olInputBox.Items.Sort "[SentOn]", False

  Dim sFilter As String
  iCount = olInputBox.Items.Count
  Dim sNothing As String
  sNothing = "Nothing"
  Set ItemsToSearch = olInputBox.Items
  For Each ItemSearch In olInputBox.Items
     With ItemSearch
        deInDate1 = .ReceivedTime
        deSendDate1 = .SentOn
        szSenderName1 = .SenderName
        szSubject1 = .Subject
'         sFilter = "[ReceivedTime] = '" & Format(.ReceivedTime,
"yyyymmddhhmmss") & "' And [SentOn] = '" _
'                      & Format(.SentOn, "yyyymmddhhmmss") & "' And
[SenderName] = '" & .SenderName _
'                      & "' And [Subject] = '" & .Subject & "'"
        ' No seconds allowed?
        sFilter = "[ReceivedTime] = '" & Format(.ReceivedTime,
"yyyymmddhhmm") & "' And [SentOn] = '" _
                     & Format(.SentOn, "yyyymmddhhmm") & "' And
[SenderName] = '" & .SenderName _
                     & "' And [Subject] = '" & .Subject & "'"
        sFilter = "[Subject] = 'Projektangebote ID Netz'"
     End With
     'ItemFound = olInputBox.Items.Find(sFilter)
     ItemFound = ItemsToSearch.Find(sFilter)
     ItemsRestricted = olInputBox.Items.Restrict(sFilter)
     While TypeName(ItemFound) <> sNothing
        If ItemFound.EntryID <> ItemSearch.EntryID Then
           ItemFound.Delete
        End If
        'ItemFound = olInputBox.Items.FindNext
        ItemFound = ItemsToSearch.FindNext
     Wend
  Next ItemSearch

End Sub
----------------------------------------------------------------
The problem is that the items of olInputBox (seen at the Watcher) are
all empty. In "Item X" you can read "<No Variables>". But ItemSearch is
not empty.

Does anybody knows what's wrong with my code?

Asking greetings

Reiner
Michael Bauer - 18 Oct 2005 09:18 GMT
Am 17 Oct 2005 09:26:21 -0700 schrieb Reiner:

Hallo Reiner,

do you have any reason not to use the PickFolder function? With it you could
save the first half of the code :-)

Your questions at the bottom I don´t really understand. Despite of I´ve a
few tips for you.

1. Because you want to delete items from within a loop you must run the loop
backwards. Exp.:
    For i=Items.Count to 1 Step -1

There´re two loops for the same collection of items, so both loops have to
run backwards.

2. Because the mail folder can contain other object types than MailItems
only you should test the object type before using the MailItem variable.
Exp.:
    Dim obj as Object
    Dim oMail as Outlook.MailItem
    For i=
        Set obj=Items(i)
        If TypeOf obj is Outlook.MailItem Then
            Set oMail=obj

3. In the With ItemSearch block you´re building two filter criteria, and the
second overwrites the first one.

4. I´d use Restrict instead of Find and loop through it backwards if the
result contains items.

And please let me add my 2 cents: Short, clean code extremly increases your
chance that anybody takes the time to go through it.

Signature

Viele Gruesse / Best regards
Michael Bauer - MVP Outlook

> Hi,
>
[quoted text clipped - 124 lines]
>
> Reiner
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.