Working with Outlook Public Folders from Excel

Can anyone point me in the direction of some example code Instead of sending an e-mail which has a large attachment to a distribution group, I want to post it to an existing Outlook folder. The code will be placed inside an excel workbook, which creates the attachment file.

Thanks in advance



Answer this question

Working with Outlook Public Folders from Excel

  • adavis2

    Here's the latest from the engineer:

    I understand that you are having some trouble creating and posting Outlook items programmatically using vba.

    Here is some example code that should help:

    Sub PostToPublicFolder()
    Dim OL As Outlook.Application
    Dim NS As NameSpace
    Dim APFs As MAPIFolder      'All Public Folders
    Dim MYPF1 As MAPIFolder  'My Pub Folder 1
    Dim NewItem As PostItem
      
    Set OL = CreateObject("Outlook.Application")
    Set NS = OL.GetNamespace("MAPI")
    Set APFs = NS.Folders("Public Folders").Folders("All Public Folders")
     
     ' Enter a valid Public Folder Name below
    Set MYPF1 = APFs.Folders("MyTestFolder")
      
    'Items.Add will create a new item
    'in the folder based on the default
    'form.  If you wish to use a custom
    'form you can change the MessageClass.
    Set NewItem = MYPF1.Items.add
    NewItem.Subject = "Posted using Items.Add"
     NewItem.Post
      
    'CreateItem will create a new PostItem
    'with the Inbox set as the default folder.
    'You can then use Move to move the item

    'to the folder of your choice.

    Dim NPost As PostItem

    Set NPost = OL.CreateItem(olPostItem)

    NPost.Subject = "This was posted using Move"

    NPost.Post

    NPost.Move MYPF1

    End Sub

    -brenda (ISV Buddy Team)



  • never.1981.m.ph

    Many thanks Brenda,

    I had got as far as creating the item in my Inbox which was driving me mad,


  • Brad Alexander

    Hi ADG,

    Can you please provide a more detailed example for the engineer If you want to send a screenshot you can email me at budsup@microsoft.com.

    -brenda (ISV Buddy Team)



  • nedde

    thanks for the additional info... I've forwarded it to the engineer!

    -brenda (ISV Buddy Team)



  • GaryHend

    From the object browser help text I have got this far:

    Public Function PostOutlookFolder(strFileName)
    Dim myOlApp As Outlook.Application
    Dim myNameSpace As Outlook.NameSpace
    Dim myPost As Outlook.PostItem
    Dim myAttach As Outlook.Attachment

    Set myOlApp = CreateObject("Outlook.Application")
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myPost = myOlApp.CreateItem(olPostItem)
    Set myAttach = myPost.Attachments
    myAttach.FileName = strFileName
    myPost.Subject = "Material Result - " & Format(Now() - 1)

    myPost.post

    End Function

    Cannot find where I set the property for the postitem folder, which will be "Public Folders\UK\Finance Reports\Material Results" (assuming that \ is used to split up the sub folders).


  • _anna

    Hi Brenda

    Not sure that I made my self clear. I have a Spreadsheet, say Book1.xls, which I create with VBA from Access. I want to put this Spreadsheet in a Discusion topic in an Outlook public folder called "UK Finance\stock reports". I have seen a number of examples of how to create an e-mail with an attachment, but none for posting to Public Folders.

    Regards

    ADG


  • Working with Outlook Public Folders from Excel