none
Outlook VBA will not allow me to attach a file to an e-mail i create and send RRS feed

  • Question

  • The same code that works in Outlook 2010 on my XP machine does not work on my win 7. It stops when I get to the line to add the attachment. Error is "You don't have appropriate permission to perform this operation."

    I am admin on both the XP and win7 machines.

    code snippet:

        Dim olApp As Outlook.Application
        Dim objMail As Outlook.MailItem
        Set olApp = Outlook.Application
        'Create e-mail item
        Set objMail = olApp.CreateItem(olMailItem)

        With objMail
           'Set body format to HTML
           .HTMLBody = strTotal
           .To = g_Customer.cE_address
           .CC = "support@princetoncenter.com"
           .Subject = strSubject
           If strFileAttach <> "" Then
            .Attachments.Add strFileAttach, olByValue, 1, "Product License"

    Please help. I have been at this for three hours and it should be simple. I suspect it is an Outlook setting but don't know which one.

    Thanks.


    karl

    Sunday, February 10, 2013 10:43 PM

Answers

  • Hmmm... As as experiment, can you try to install Redemption from http://www.dimastr.com/redemption/download.htm and run the following code, either from the VBA editor or from OutlookSpy (click Script button, paste the script, click Run)?

    I hope Redemption will provide a more meaningful error message...

    strFileAttach = "C:\Users\Kelly\My Documents\publisher.lic"
    set Session = CreateObject("Redemption.RDOSession")
    Session.MAPIOBJECT = Application.Session.MAPIOBJECT
    set Msg = Session.GetDefaultFolder(olFolderDrafts).Items.Add
    Msg.Attachments.Add strFileAttach, olByValue, 1, "Product License"


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by BitsnPCES Monday, February 11, 2013 10:50 PM
    Monday, February 11, 2013 9:36 PM

All replies

  • What is the value of the strFileAttach variable?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, February 11, 2013 12:46 AM
  • The value of strFileAttach is this string:

    "C:\Users\Kelly\My Documents\publisher.lic"

    The file publisher.lic is a tiny text file. I have tried the same code changing the string and the filename to publisher.txt thinking it could be a security issue, but I get the same result. This exact code works in XP. This leads me to believe it is a permissions error and I do not know what to do since I am the only user (and I am admin) on this Win 7 machine.


    karl

    Monday, February 11, 2013 8:29 PM
  • use process monitor to verify that your code can actually access that files and does not get access denied somewhere along the path
    Monday, February 11, 2013 8:56 PM
  • Is "Kelly" the name of the current (parent process wise) user?

    Where is your code running (COM addin, GUI app, service)?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, February 11, 2013 8:59 PM
  • That's a good question because when I moved the target file out of my "My Documents" folder and on to a network drive the code worked. How would I know who is the parent of the process? It is a chunk of code in the VBAProject.otm file that I run from Outlook. If Kelly is not the process owner, how would I make it so?

    karl

    Monday, February 11, 2013 9:13 PM
  • So it is Outlook that runs your code, right? Who was the currently logged in user who launched Outlook?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Monday, February 11, 2013 9:16 PM
  • Kelly

    karl

    Monday, February 11, 2013 9:20 PM
  • Hmmm... As as experiment, can you try to install Redemption from http://www.dimastr.com/redemption/download.htm and run the following code, either from the VBA editor or from OutlookSpy (click Script button, paste the script, click Run)?

    I hope Redemption will provide a more meaningful error message...

    strFileAttach = "C:\Users\Kelly\My Documents\publisher.lic"
    set Session = CreateObject("Redemption.RDOSession")
    Session.MAPIOBJECT = Application.Session.MAPIOBJECT
    set Msg = Session.GetDefaultFolder(olFolderDrafts).Items.Add
    Msg.Attachments.Add strFileAttach, olByValue, 1, "Product License"


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by BitsnPCES Monday, February 11, 2013 10:50 PM
    Monday, February 11, 2013 9:36 PM
  • OK. The redemption helped me figure it out: even though the file appears in "My Documents"  its actual path is "C:\Users\Kelly\Documents\publisher.lic"  The function I was using to get the path was faulty as it assumed the path included "my documents." Doh!

    Thanks Dmitry


    karl

    Monday, February 11, 2013 10:50 PM