VBA Save Attachments causes type mismatch on Next statement.

J

Jeff

Here is the code:

Set myolApp = Outlook.Application
Set myNameSpace = myolApp.GetNamespace("MAPI")
Set myFolder = myNameSpace.PickFolder
Dim oMailItem As MailItem

frmSaveStatus.Show

SaveFolder = "c:\Attachments\"
frmSaveStatus.lblItems.Caption = myFolder.Items.Count
myCounter = 0

For Each oMailItem In myFolder.Items
myCounter = myCounter + 1
frmSaveStatus.lblComplete.Caption = myCounter
If myFolder.Items.Count = 0 Then
' Do Nothing
Else
frmSaveStatus.lblPercentage = myCounter /
myFolder.Items.Count
End If
frmSaveStatus.Repaint

If oMailItem.Attachments.Count > 0 Then
For Each myattachment In oMailItem.Attachments
frmSaveStatus.lblFileName = myattachment.FileName
frmSaveStatus.lblFrom = oMailItem.Subject
frmSaveStatus.lblDate = oMailItem.Size
Select Case LCase(Right(myattachment.FileName, 3))
Case "msg":
GoTo EndFor1:
Case Else:
myattachment.SaveAsFile SaveFolder &
myattachment.FileName
End Select
EndFor1:
Next myattachment
End If
Next '<-Type Mismatch on Item #406 of 416!!!?

frmSaveStatus.Hide

Set objWord = Nothing
Set dlg = Nothing
Set objOL = Nothing
Set objOLWindow = Nothing
End Sub

OK, this has a lot of stuff in it that doesn't need to be there, but
after a certain number of executions (Say, mFolder.Items.Count=416, I
get the error at item #406), I get a Type Mismatch error on the Next
line indicated in the code.

I'm a newbie, so please help directly, and please assume (as Schultz
would say:) I know NOTHING!!!

Thanks,

Jeff
 
M

Michael Bauer

Hi Jeff,

the error doesn´t refer to any Attachment, but to the items in your
folder. Please use for the outer For Each loop a variable declared As
Object, and then check each one whether it´s a MailItem or not.
 
R

Ray

hi Michael and Jeff

This looks to be what I am wanting to do. I can't make any sense of your
code. I know even less than nothing. If you could be kind enough to post
your final working code I would find that most helpful. Sorry Michael I
didn't understand your comment which I am sure is exactly right. Please help
if I am not too late to catch your attention.

Many Thanks

Ray Brown
 
M

Michael Bauer

(Ray, my OE has lost your post so I´m not replying to your´s directly.)

Code for checking the object type (in addition to Jeff):

Dim obj as Object
.....
For Each obj In myFolder.Items
If TypeOf obj is Outlook.MailItem Then
Set oMailItem=obj
....
Endif
Next
 
R

Ray

Hi Michael

Hoping you are still watching this thread.

Thanks I think I have got the code right but now my problem is how do I use
it. Does "frmSaveStatus.lblItems" mean that I need a form with this label
etc or what? I tried to do something like that but keep getting messages
that I don't understand. I think I am missing something very basic. I have
tried a couple of things but am now totally confused and can't remember what
I have done and what occurred when.

I'm lost and have no compass. Please get me started if you can. Many thanks

Ray Brown
 
M

Michael Bauer

Hi Ray,

you don´t need the frmSaveStatus. The OP uses this for displaying how
much work is done already. You can comment out that lines. Otherwise if
you want to have this Form, too, then you can add a UserForm to your
project. Place all mentioned controls on it. The OP has given them good
names, the prefix "lbl" is surely for a Lable control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top