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 / March 2004

Tip: Looking for answers? Try searching our database.

Outlook 2003 vba macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Grant - 25 Mar 2004 10:49 GMT
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
Sue Mosher [MVP-Outlook] - 25 Mar 2004 13:01 GMT
Show the relevant section of your code. Looping through the items in a
folder is independent of a folder's current view.

Signature

Sue Mosher, Outlook MVP
Author of
    Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> My macro loops through all the messages in the inbox and moves them to a
> folder in Public Folders.
[quoted text clipped - 8 lines]
> Thanks,
> Grant
Grant - 25 Mar 2004 23:50 GMT
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)

> Show the relevant section of your code. Looping through the items in a
> folder is independent of a folder's current view.
[quoted text clipped - 14 lines]
> > Thanks,
> > Grant
Sue Mosher [MVP-Outlook] - 31 Mar 2004 14:27 GMT
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

Signature

Sue Mosher, Outlook MVP
Author of
    Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> 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
[quoted text clipped - 69 lines]
> > > Thanks,
> > > Grant
 
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



©2008 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.