Question: I'm creating a bunch of emails using Word 2013 mail merge (not sure about the English term; I'm working with German version). Outlook is set to *offline mode*, so that I can modify the mails before sending out. So far, everything is working.

I want to add an attachment to each of the emails and am trying to write some Outlook VBA script to do so. The following code is failing with code 80070005 (insufficient permission) at the `.Attachment.Add...` statement **but** only if the mail does not have an attachement yet. If there is an attachment, it will add the attachment to this **and** the next mail, then fails again with 80070005. I repeated running the script and it adds the attachment to one more mail at each run.

What might cause the 80070005?

```
Sub addAttachmentsToMailsInOutbox()

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 ("d:\temp\test.txt")
            .Save
        End With
    End If
Next

MsgBox ("done")
End Sub

```

*Note* that this is the smallest working -- well failing -- example. I tried other code I found on the internet, but all show that strange behaviour.

Answer: 
The suggestion from @HackSlash didn't work either, but it prompted another idea: Separate the modifications into two loops, and voilà, this works:

```
Sub addAttachmentsToMailsInOutbox()

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

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

For Each olItem In olOutbox.Items
    If olItem.Class = olMail Then
        With olItem
            .BodyFormat = olFormatHTML
            .Save
        End With
    End If
Next

For Each olItem In olOutbox.Items
    If olItem.Class = olMail Then
        With olItem
            .Attachments.Add ("d:\temp\test.txt")
            .Save
        End With
    End If
Next

MsgBox ("done")
End Sub

```

If more than one file needs to be attached, simply repeat the line `.Attachments.Add(....)` for each file.
I was unable to reproduce your problem using a newly created email. This suggests that it comes down to the properties of the mail item you are editing. For example, if the `BodyFormat` is not `olFormatHTML` then you might not be able to add attachments as expected.

If the code I supply here works then that was your problem. Please let me know if this works for you.

```
Public Sub addAttachmentsToMailsInOutbox()

    Dim olNs As Outlook.NameSpace
    Set olNs = GetNamespace("MAPI")

    Dim olOutbox As Outlook.MAPIFolder
    Set olOutbox = olNs.GetDefaultFolder(olFolderOutbox)

    Dim olItem As Object
    For Each olItem In olOutbox.Items
        If olItem.Class = olMail Then
            With olItem
                .BodyFormat = olFormatHTML
                .Save
                .Attachments.Add ("d:\temp\test.txt")
                .Save
            End With
        End If
    Next

    MsgBox ("done")
End Sub

```

NOTE: The [documentation](https://learn.microsoft.com/en-us/office/vba/api/outlook.attachments.add) states

> 
> To ensure consistent results, always save an item before adding or removing objects in the Attachments collection.
> 
> 
>