2

I'm trying to attach a file to all the mails in my Outbox folder using Outlook. I ran the following code:

Public Sub ABT()

Dim olNs As Outlook.NameSpace
Dim olOutbox As Outlook.MAPIFolder
Dim olItem As Object
Dim olEmail As Outlook.MailItem


Set olNs = GetNamespace("MAPI")
Set olOutbox = olNs.GetDefaultFolder(olFolderOutbox)


For Each olItem In olOutbox.Items
    If olItem.Class = olMail Then
        Set olEmail = olItem
        With olEmail
            .Attachments.Add "C:\Users\Augustin\Pictures\znakovi\blah.jpg"
            .Save
            .Send
        End With
    End If
Next
End Sub

But when I run the code, it tells me that I don't have the appropriate permissions to perform this operation.

I started Outlook in Administrator mode, so I think this error shouldn't happen and I would be very thankful if someone could point me to the solution.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
J. Doe
  • 1,544
  • 1
  • 11
  • 26
  • Verify you can do this manually. If not, on the ribbon - Actions > Edit Message. – niton Feb 05 '18 at 16:54
  • I'm not sure I follow you. I can add the attachment to the message manually by double clicking the message and then choosing attach from the ribbon. However, the problem is that I have more than 100 messages in my outbox and I don't feel like doing this manually. – J. Doe Feb 05 '18 at 17:04
  • I was confirming your Outbox mail can be edited. Assuming the line with the error is .Attachments.Add, try a file not in c drive. – niton Feb 05 '18 at 17:37
  • Try it on 2 or 3 emails before you run it on 100's emails- – 0m3r Feb 05 '18 at 22:27
  • It works with a single mail... If there's more than one mail, it doesn't work. I tried to attach a file from D:\ and the same problem is happening. – J. Doe Feb 06 '18 at 06:40
  • There is a bug in For Each, when the collection is reduced, that you can address to see if there is an impact. You could edit the question with a reverse count loop regardless of no impact on the permission error. You will need it anyway. Here is one example of the many questions about this. https://stackoverflow.com/questions/10725068/for-each-loop-some-items-get-skipped-when-looping-through-outlook-mailbox-to-de – niton Feb 06 '18 at 14:19
  • Do the attachment first then send the mail out with second loop – 0m3r Feb 08 '18 at 23:57

1 Answers1

0

Messages in the Outbox folder are being submitted, they cannot be touched - they are owned by the spooler.

If yo want to process outgoing messages, trap the Application.ItemSend event and modify the message there.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78