Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i get following code for Outlook, Recognise Email, Save Attachment, Move Email To A Subfolder

VB
Option Explicit
 
Sub GetAttachments_From_Inbox()
 
    On Error GoTo GetAttachments_err
    ' Declare variables
    Dim appOl As New Outlook.Application
    Dim ns As Outlook.NameSpace
    Dim Inbox As Outlook.MAPIFolder
    Dim myDestFolder As Outlook.MAPIFolder
    Dim Item As Object
    'Dim Item As Outlook.Items
    Dim Atmt As Outlook.Attachment
    Dim FileName As String
    Dim i As Integer
    Dim sender As String
    Dim ext As String
    Dim Items As Outlook.Items
    Dim oc As Object
    Set ns = appOl.GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set Item = Inbox.Items
    Set myDestFolder = Inbox.Folders("Personal Mail")
    
  
    'Set oc = Application.ActiveInspector.CurrentItem
    i = 0
    
' Check Inbox for messages and exit if none found
    If Inbox.Items.Count = 0 Then
        MsgBox "There are no messages in the Inbox.", vbInformation, _
               "Nothing Found"
        Exit Sub
    End If
    
' Check each message for attachments
    For Each Item In Inbox.Items
' Save any attachments found
        For Each Atmt In Item.Attachments
        ' This filename path must exist! Change folder name as necessary.
            sender = Atmt.Parent.SenderEmailAddress
            sender = Right(sender, Len(sender) - InStrRev(sender, "="))
            
            ext = Atmt.FileName
            ext = Right(ext, Len(ext) - InStrRev(ext, ".") + 1)
            FileName = "S:\Loans\Data\For\Outlook\" & get_bank(sender) & ext
            'Atmt.SaveAsFile FileName
            
            If get_bank(sender) <> "unknown" Then
            Atmt.SaveAsFile FileName
            Item.Move myDestFolder
              i = i + 1
            'Set Item = Item.FindNext
            End If
           
         Next Atmt
    Next Item
    
  ' Show summary message
    If i > 0 Then
        MsgBox "I found " & i & " attached files." _
        & vbCrLf & "I have saved them into the C:\Email Attachments folder." _
        & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!"
    Else
        MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!"
    End If
' Clear memory
GetAttachments_exit:
    Set Atmt = Nothing
    Set Item = Nothing
    Set ns = Nothing
    Set appOl = Nothing
    Exit Sub
' Handle errors
GetAttachments_err:
    MsgBox "An unexpected error has occurred." _
        & vbCrLf & "Please note and report the following information." _
        & vbCrLf & "Macro Name: GetAttachments" _
        & vbCrLf & "Error Number: " & Err.Number _
        & vbCrLf & "Error Description: " & Err.Description _
        , vbCritical, "Error!"
    Resume GetAttachments_exit
End Sub

Function get_bank(sender As String) As String         
Select Case sender 
        Case "ABC.DEF@COMPANYNAME.com"
        get_bank = "ABC"        
	Case Else
        get_bank = "unknown"        
    End Select   
End Function


then import following assemblies
Imports Microsoft.Office.Interop.Outlook
Imports System.Net.Mime.MediaTypeNames
Imports Microsoft.Office.Interop

but still getting following errors



Error 1 'Application' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook'. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 16 26 WindowsApplication1
Error 2 'NameSpace' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook'. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 17 19 WindowsApplication1
Error 3 'MAPIFolder' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook'. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 18 22 WindowsApplication1
Error 4 'MAPIFolder' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook'. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 19 29 WindowsApplication1
Error 5 'Attachment' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook'. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 22 21 WindowsApplication1
Error 6 'Items' is ambiguous in the namespace 'Microsoft.Office.Interop.Outlook'. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 27 22 WindowsApplication1
Error 7 Name 'olFolderInbox' is not declared. D:\MIT\project\Outlook 2007\WindowsApplication1\WindowsApplication1\Form1.vb 30 37 WindowsApplication1


i am using 12.0 office library
pls tell me y is that and how can i prevent it
Posted
Updated 12-Jun-10 23:27pm
v3

1 solution

Try fully qualifying Outlook.Application in this line:
Dim appOl As New Outlook.Application

To say:
Dim appOl As New Microsoft.Office.Interop.Outlook.Application
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900