+ Reply to Thread
Results 1 to 2 of 2

outlook vba withevents, automating attachment saveas

Hybrid View

  1. #1
    Registered User
    Join Date
    05-24-2014
    Posts
    6

    outlook vba withevents, automating attachment saveas

    i can't get this to work for the life of me. i'm trying to automate saving an attachment when specific emails come in. I haven't even added the boolean language yet.... sheesh. any guidance with this would definitely be helpful.

    Option Explicit
    Private WithEvents olInboxItems As Items
    Private Sub Application_NewMail()
    Dim objNS As Outlook.NameSpace
    Dim X As Integer
     
    Set objNS = GetNamespace("MAPI")
    Set olInboxItems = Session.GetDefaultFolder(olFolderInbox).Items
    End Sub
    Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
    Dim NS As NameSpace
    Dim MYINBOX As mapifolder
    Dim MYITEM As MailItem
    
    Set NS = GetNamespace("MAPI")
    Set MYINBOX = NS.GetDefaultFolder(olFolderInbox)
    
    If TypeOf Item Is MailItem Then
    Set MYITEM = Item
    If MYITEM.Attachments.Count = 1 Then
    MYITEM.Attachments.Item(1).SaveAsFile "C:\Users\jvaughn\Desktop\Test\" & MYITEM.Attachments.Item(1).DisplayName
    End If
    End If
    Set NS = Nothing
    Set MYINBOX = Nothing
    End Sub

  2. #2
    Forum Contributor
    Join Date
    08-27-2006
    Posts
    136

    Re: outlook vba withevents, automating attachment saveas

    Do not use both NewMailEx and ItemAdd. It is one or the other.
    See an example of NewMailEx here.
    http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    This is untested code.

    Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
        Dim varEntryIDs
        Dim objItem
        Dim i As Integer
        varEntryIDs = Split(EntryIDCollection, ",")
        For i = 0 To UBound(varEntryIDs)
            Set objItem = Application.Session.GetItemFromID(varEntryIDs(i))
            Debug.Print "NewMailEx " & objItem.Subject
    
            If TypeOf objItem Is MailItem Then
                Set MYITEM = objItem
                    If MYITEM.Attachments.Count = 1 Then
                        MYITEM.Attachments.Item(1).SaveAsFile "C:\Users\jvaughn\Desktop\Test\" & _
                          MYITEM.Attachments.Item(1).DisplayName
                    End If
            End If
        Next
    End Sub
    To mark "Solved" go to Thread Tools.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Automating outlook calendar invitations
    By K G in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-15-2014, 10:00 PM
  2. [SOLVED] Automating the Outlook VBA code.
    By ciapul12 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-05-2014, 08:17 PM
  3. [SOLVED] VBA Macro to print to Pdf format and place as attachment in Outlook (Excel & Outlook 2007)
    By Webman1012 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-29-2013, 01:25 PM
  4. Automating Outlook with Excel
    By mppotter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-05-2011, 11:16 AM
  5. Preventing FALSE.xls when automating SaveAs
    By ljsmith in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-11-2007, 09:48 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1