Code Explanation

J

John

i am trying to automatically save attachments to the c
drive from an email attachment by pressing a button in MS
Access however i am having trouble i was wondering if
someone could explain the code to me

Sub SaveAttachment()
Dim myOlApp As Outlook.Application
Dim myInspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Set myOlApp = CreateObject("Outlook.Application")
Set myInspector = myOlApp.ActiveInspector
If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments
'Prompt the user for confirmation
Dim strPrompt As String
strPrompt = "Are you Sure"
If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then
myAttachments.Item(1).SaveAsFile "C:\" & _
myAttachments.Item(1).DisplayName
End If
Else
MsgBox "The item is of the wrong type."
End If
End If
End Sub

I would particularly like to know what the following
lines mean

If Not TypeName(myInspector) = "Nothing" Then
If TypeName(myInspector.CurrentItem) = "MailItem" Then
Set myItem = myInspector.CurrentItem
Set myAttachments = myItem.Attachments


Thanking you in advance
John
..
 
A

Andrew Cushen

John:

If I understand the code correctly, here is what it's
doing:

If Not TypeName(myInspector) = "Nothing" Then
-This line is checking that the Inspector item that
myInspector points to actually exists.

If TypeName(myInspector.CurrentItem) = "MailItem" Then
-This line seems to be checking that the current item is
an email, not another type of Outlook object. This is
necessary because you can have many different types of
items in any Outlook folder.

Set myItem = myInspector.CurrentItem
-This is setting the "myItem" variable to the item
currently open in Outlook.

Set myAttachments = myItem.Attachments
- This is setting the "myAttachments" variable to the
attachments of the "myItem" variable.


HTH,

-Andrew
============================================
 

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