Re: Outlook 2003 vba macro

From: Sue Mosher [MVP-Outlook] (suemvp_at_outlookcode.com)
Date: 05/25/04


Date: Tue, 25 May 2004 02:41:07 -0400

Repeating: Don't try to delete or move items inside a For Each ... Next
loop.

-- 
Sue Mosher, Outlook MVP
Author of
     Microsoft Outlook Programming - Jumpstart for
     Administrators, Power Users, and Developers
     http://www.outlookcode.com/jumpstart.aspx
"Willy" <anonymous@discussions.microsoft.com> wrote in message
news:25AF0D45-38F4-423B-9301-82EAE4216F63@microsoft.com...
> Another way of doing it is to use a for each loop
>
> Dim msg as mailItem
> For each msg in objMailItem
>
> 'your code
>
> next
>
>      ----- Sue Mosher [MVP-Outlook] wrote: -----
>
>      Don't move or delete items in a For Each loop! The index is reset
each time,
>      which means you'll move only half. There are several different
correct
>      approaches. One is a countdown loop:
>
>      'Move the email Item to the backup folder
>      intCount = objInbox.Items.Count
>      For i = intCount to 1 Step -1
>          Set objMailItem = objInbox.Items(i)
>          objMailItem.Move objPersonalInbox
>      If Err.Number = 438 Then
>          GoTo skip
>      End If
>
>      "Grant" <gpsnett@hotmail.com> wrote in message
>      news:ungzkurEEHA.624@TK2MSFTNGP10.phx.gbl...
>      > Heres the code (Im new to vba and this code is just me learning the
>      outlook
>      > object model)  - the error catch is there because the loop doesnt
like
>      > 'undeliverable' items. The loop works fine but I have to run it for
each
>      > group - ie 'today'. 'yesterday', 'last week'.
>      >> Dim objApp As Application
>      > Dim objNS As NameSpace
>      > Dim objInbox As MAPIFolder
>      > Dim objPersonalFolder As MAPIFolder
>      > Dim objPersonalInbox As MAPIFolder
>      > Dim strHandyString As String
>      > Dim strMovedEmailItems As String
>      >>> strMovedEmailItems = "The following mail items were moved:" &
vbCrLf &> vbCrLf
>      >> Set objApp = CreateObject("outlook.application")
>      > Set objNS = objApp.GetNamespace("mapi")
>      > Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
>      >> Set objMailItem = objInbox.Items
>      >> 'Set the personal folder object
>      > For Each objfoldername In objNS.Folders
>      >     If objfoldername = "Personal Folders" Then
>      >         Set objPersonalFolder = objfoldername
>      >     End If
>      >> Next
>      >> 'Set the backup inbox folder object
>      > For Each objPersFolderName In objPersonalFolder.Folders
>      >     strHandyString = strHandyString & vbCrLf & objPersFolderName
>      >         If objPersFolderName = "Backup Inbox" Then
>      >             Set objPersonalInbox = objPersFolderName
>      >         End If
>      > Next
>      >> 'Move the email Item to the backup folder
>      > For Each objMailItem In objInbox.Items
>      >     objMailItem.Move objPersonalInbox
>      > If Err.Number = 438 Then
>      >     GoTo skip
>      > End If
>      > Err.Clear
>      >     strMovedEmailItems = strMovedEmailItems & vbCrLf &
objMailItem.Subject
>      &> _
>      >     " - " & objMailItem.SenderName
>      > skip:
>      > Next
>      >>> 'Show the form with the results
>      > 'FrmResultsShow (strHandyString)
>      > FrmResultsShow (strMovedEmailItems)
>      >>> "Sue Mosher [MVP-Outlook]" <suemvp@outlookcode.com> wrote in
message
>      > news:OZ6eZJmEEHA.3576@tk2msftngp13.phx.gbl...
>      >> Show the relevant section of your code. Looping through the items
in a
>      >> folder is independent of a folder's current view.
>      >>>> -- 
>      >> Sue Mosher, Outlook MVP
>      >> Author of
>      >>      Microsoft Outlook Programming - Jumpstart for
>      >>      Administrators, Power Users, and Developers
>      >>      http://www.outlookcode.com/jumpstart.aspx
>      >>>>>> "Grant" <gpsnett@hotmail.com> wrote in message
>      >> news:u91RH6kEEHA.3864@TK2MSFTNGP12.phx.gbl...
>      >>> My macro loops through all the messages in the inbox and moves
them to
>      a
>      >>> folder in Public Folders.
>      >>> The problem is the inbox view is set to "View by date' and 'Show
in
>      >> groups'
>      >>> so the macro copies all mail items from a certain day and then
stops.
>      >>>>>> For example at the moment I have mail items from 'Today' and
>      'Yesterday'
>      >> and
>      >>> 'Last week' etc. I will need to run the macro 3 times to move
items
>      from
>      >>> each group. Instead of changing the view is there a way around
this
>      >> grouping
>      >>> thing in Outlook 2003?
>      >>>>>> Thanks,
>      >>> Grant
>      >>>>>>>>>>>>


Relevant Pages

  • Re: HTML Item properties vs. Regular item properties
    ... Why would Outlook record different times for the HTTP ... and the default folder? ... Dim app As Outlook.Application ... "Dmitry Streblechenko" wrote: ...
    (microsoft.public.outlook.program_vba)
  • Re: HTML Item properties vs. Regular item properties
    ... I don't use the MSN provider, but AFAIK the date in the default store's is ... OutlookSpy - Outlook, CDO ... as well as the Sent Items folder on the MSN server. ... Dim app As Outlook.Application ...
    (microsoft.public.outlook.program_vba)
  • Re: Copying contents of a folder
    ... Viele Gruesse / Best regards ... Keep your Outlook categories organized! ... Dim olApp As New Outlook.Application ... At the time it is being run, the Contacts Folder has the 330 odd members ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: Outlook 2003 vba macro
    ... Heres the code (Im new to vba and this code is just me learning the outlook ... The loop works fine but I have to run it for each ... Dim objApp As Application ... 'Set the backup inbox folder object ...
    (microsoft.public.outlook.program_vba)
  • Re: Outlook Automation Problem
    ... Dim objNS As Outlook.NameSpace ... > The error is not coming when I loop through the contacts and add each one ... > is used as the Recipient for Outlook email. ... > Not having access to a network or exchange server makes it a little ...
    (microsoft.public.vb.general.discussion)

Loading