Consulting

Results 1 to 5 of 5

Thread: Save Attachments

  1. #1
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    2
    Location
    Quote Originally Posted by Killian
    OK, I think I've got this worked out. All the code below goes into the ThisOutlookSession module. The rationale is, that you need to declare an object "WithEvents" - in this case an items collection. In the app_startup code, refer your chosen collection (in my example, the items in folder "Temp" in my personal folders) to this object. Now the Item_add event for this object will fire when a new mail arrives in that folder. Then you're free to do as you please with it!

    Hope this helps
    K :-)
    [VBA]'expose the items in the target folder to events
    Dim WithEvents TargetFolderItems As Items

    Private Sub Application_Startup()
    'some startup code to set our "event-sensitive" items collection
    Dim ns As Outlook.NameSpace

    Set ns = Application.GetNamespace("MAPI")
    Set TargetFolderItems = ns.Folders.Item( _
    "Personal Folders").Folders.Item("Temp").Items

    Set ns = Nothing

    End Sub

    Private Sub TargetFolderItems_ItemAdd(ByVal Item As Object)
    'when a new item is added to our "watched folder" we can process it
    Dim olAtt As Attachment

    'you may want to run some tests on the item/attachment(s)
    If Item.Attachments.Count = 1 Then
    Set olAtt = Item.Attachments(1)
    olAtt.SaveAsFile "C:\temp\" & olAtt.FileName
    End If

    'pass the filepath to the print routine
    PrintAtt ("C:\temp\" & olAtt.FileName)

    Set olAtt = Nothing

    End Sub

    Sub PrintAtt(file As String)

    Dim xlApp As Object
    Dim wb As Object

    'in the background, create an instance of xl then open, print, quit
    Set xlApp = CreateObject("Excel.Application")
    Set wb = xlApp.Workbooks.Open(file)
    wb.PrintOut
    xlApp.Quit

    'tidy up
    Set wb = Nothing
    Set xlApp = Nothing

    End Sub[/VBA]
    I'm using this and having a problem. The only way I can get it to work is manualy run the Application_Startup section. Also when I open Outlook I don't get the usual Enable Macro notice. Thanks for a reply.

  2. #2
    VBAX Newbie
    Joined
    Mar 2008
    Posts
    2
    Location
    Thanks for your help...I'm pretty new to this, hope you have patience. I've put the code in Module1 (the only module) but the "Dim WithEvents TargetFolderItems As Items" turns red. Is some of this code supposed to be in the screen for "ThisOutLookSession?

  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Admin note: Split to new thread from here.

  4. #4
    VBAX Newbie
    Joined
    Apr 2008
    Posts
    1
    Location
    put it all in ThisOutLookSession? this is where it has worked for me

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yes, it goes in the ThisOutlookSession module.

Posting Permissions

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