PDA

View Full Version : [SOLVED:] VBA: Limited # of open items. How to close?



Rexilion
09-09-2014, 11:54 AM
Hi all,

I think this is all fairly standard. New job, never coded in VBA. One of my first assignments was 'to make something to save all those attachments easier'. I believe this is almost 'the' standard problem when people enroll into VBA.

However, I have been banging my head against the wall because I'm receiving the following error when the # of messages becomes >>200.

Your server administrator has limited the number of items you can open simoultaneously. Try closing messages you have opened or removing attachments and images from unsent messages you are composing.

So, I tried various things:

- Enable explicit mode
- Declare variables outside the each loop
- Link the loop object to another object and then destroy the later object

Nothing appears to work. Below will demonstrate my problem. I distilled this small macro from my (by now) huge project (people tend to see more issue's when you solve the first one ... amazing!).

The macro will simply fill two string variables based on each mail selected. Eventually the script will end because of the limit. If I select # 500 messages, the amount of loops ran by this macro will never exceed 250. It will only exceed this number when I include mails that have no attachments.

Could anyone *please* shed some light on this? I'm at a loss here.

Environment:
Windows Server 2008
Microsoft Outlook 2010
Microsoft Exchange 2010 (I think ...)


Option Explicit

Sub Main()
Dim SELObject As Variant
Dim usr_name As String
Dim usr_counter As String
Dim index As Integer
index = 0


For Each SELObject In ActiveExplorer.Selection
If TypeName(SELObject) = "MailItem" Then
On Error GoTo EndSub
'the assumption of at least one attachment is safely satisfied
usr_name = SELObject.Attachments.Item(1).FileName
usr_counter = SELObject.Attachments.Count
On Error GoTo 0
SELObject.Close olSave
End If


index = index + 1


usr_counter = ""
usr_name = ""
Set SELObject = Nothing
Next


EndSub:
MsgBox index
End Sub

westconn1
09-10-2014, 01:43 AM
try doevents in your loop, see if it helps

Rexilion
09-10-2014, 09:05 AM
Thanks for the suggesion. I will give that a go.

I also found this, using your doevents suggestion as a keyword.

h.ttp://stackoverflow.com/a/13363980 # stackoverflow ftw
h.ttp://support.microsoft.com/kb/830836 # explains the 250 # limit

Well, then use the current folder with some category thingy I guess...

westconn1
09-10-2014, 02:30 PM
did you try doevents? as that may allow the iems to close, while the script is running

unfortunately outlook has no ability to sort or restrict by size of message item or by attachement
possibly you can save attachments as each email arrives, instead of looping all emails

if you use the stackoverflow example it would process the new emails first, try like
for msg = items.count to items.count - 245 step - 1
should only process the newest 245 items, if not the items collection can be sorted by received time

Rexilion
09-11-2014, 10:08 AM
bit.ly 1nOeYD3 # look here

Gave me the CurrentFolder.Items idea. Which works.
The DoEvents, which I do prefer, did not work. I placed like 3 pieces of them in a loop.

For now, I'll just go with the Currentfolder.Items and maybe use the Categories property of each mailitem to save the attachment looping over the entire folder. It ain't nice (that's for sure), but it's still an improvement over manually saving stuff.

Thank you for your time.