Consulting

Results 1 to 3 of 3

Thread: Solved: Namespace

  1. #1
    VBAX Regular
    Joined
    Mar 2008
    Posts
    16
    Location

    Solved: Namespace

    Hi Folks.
    i've cobbled together some code that should save xls email attachments.. but it doesn't seem to work.
    When I run it, it highlights the first line in yellow and gives me the
    "Compile error: User-defined type not defined" error box.

    ...but it looks like the standard opening for saving email attachments from Outlook (2003)... any idea why this is happening/how to fix?!
    Many thanks, Lijon.


    [VBA] Option Explicit
    Sub saveXLSattachments()

    Dim ns As Namespace <<<----- THIS IS WHERE IT GETS STUCK! Lijon.
    Dim Inbox As MAPIFolder
    Dim SubFolder As MAPIFolder
    Dim Item As Object
    Dim Atmt As Attachment
    Dim FileName As String
    Dim MyDocPath As String
    Dim I As Integer
    Dim wsh As Object
    Dim fs As Object

    On Error GoTo ThisMacro_err

    Set ns = GetNamespace("MAPI")
    Set Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set SubFolder = Inbox.Folders("myfolder")

    I = 0
    ' Check subfolder for messages and exit of none found
    If SubFolder.Items.Count = 0 Then
    MsgBox "There are no messages in this folder : " & OutlookFolderInInbox, _
    vbInformation, "Nothing Found"
    Set SubFolder = Nothing
    Set Inbox = Nothing
    Set ns = Nothing
    Exit Sub
    End If

    'Create DestFolder if DestFolder = ""
    If DestFolder = "" Then
    Set wsh = CreateObject("WScript.Shell")
    Set fs = CreateObject("Scripting.FileSystemObject")
    MyDocPath = wsh.SpecialFolders.Item("mydocuments")
    DestFolder = MyDocPath & "\" & Format(Now, "dd-mmm-yyyy hh-mm-ss")
    If Not fs.FolderExists(DestFolder) Then
    fs.CreateFolder DestFolder
    End If
    End If

    If Right(DestFolder, 1) <> "\" Then
    DestFolder = DestFolder & "\"
    End If

    ' Check each message for attachments and extensions
    For Each Item In SubFolder.Items
    For Each Atmt In Item.Attachments
    If LCase(Right(Atmt.FileName, Len(ExtString))) = LCase(ExtString) Then
    FileName = DestFolder & Item.SenderName & " " & Atmt.FileName
    Atmt.SaveAsFile FileName
    I = I + 1
    End If
    Next Atmt
    Next Item


    End Sub[/VBA]

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Because it is for .NET.

    You probably want to use Application.Outlook. Search for links on this site or see:
    http://www.mvps.org/dmcritchie/excel/email.htm
    http://www.rondebruin.nl/mail/add-in.htm

  3. #3
    VBAX Tutor Mavyak's Avatar
    Joined
    Jul 2008
    Posts
    204
    Location
    While namespace is a .NET object, it is also an Outlook object. If you are declaring a variable of type Namespace then you are using early binding and will need to set a reference to the Microsoft Outlook Object Library. Do so by going to (in the VBE) Tools-->References and putting a checkmark next to Microsoft Outlook xx.x Object Library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •