Excel -> Outlook embedded image

ReeveCZE

New Member
Joined
Jun 8, 2014
Messages
7
Hi guys,

I'm having some hard time with the following.
I have a For - Next loop in Excel that generates an Outlook MailItem drafts with embedded image using ContentID.

Code:
Dim outApp as Object
Dim outMail as Object

Set outApp = CreateObject("Outlook.Application")  
    
    For i '..... blah blah some logic
    
        Set outMail = outApp.CreateItem(0)
        
        With outMail
            .SentonBehalfofName = "something@gmail.com"
            .BodyFormat = 2 'HTML format
            .To = "something@gmail.com"
            .Subject = "something"
[COLOR=#ff0000]            .Attachments.Add "C:\wm_logo.png", olByValue, 0 'aka create a copy of the file and hide it[/COLOR]
            .HTMLBody = "something" & "img src=""cid:wm_logo.png"">" 'I'm ommiting the < intentionally :-D
            .GetInspector 'otherwise I get "The linked image cannot be displayed" frame with red cross...
            .Close olSave
        End With
        
        Set outMail = Nothing
    Next i

Set outApp = Nothing

Works perfectly but the procedure always crashes when generating the 200th mail at the Attachments.Add with the following:


Any ideas? Is there any limit for attachments in drafts? :biggrin:
When I manually create a new mail in Outlook after this error and try to attach the same image file, I get the same error.
When I copy the image file, rename it and try to attach the renamed copy, it works.
When I restart Outlook, manually create a new mail in Outlook and try to attach the same image file, it works.

I know I could use a signature from AppData, but let's assume I can't and I'm forced to "create" my own signature.
Thanks in advance, I'm kinda lost
 
Last edited:

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

ReeveCZE

New Member
Joined
Jun 8, 2014
Messages
7
After some investigaiton I found out the problem is .GetInspector.
If I comment that line I can generate 600 mails with attachments and embedded images, however, the image is not displayed properly.
Am I closing the Inspector in a wrong way? It seems to me I'm not allowed to have more than 200 Inspectors open :)
 

ReeveCZE

New Member
Joined
Jun 8, 2014
Messages
7
Well, there seems to be a difference between MailItem.Close and MailItem.GetInspector.Close.
I'm afraid I don't understand it exactly, but the the following works:
Code:
With outMail
    .SentonBehalfofName = "something@gmail.com"
    .BodyFormat = 2 'HTML format
    .To = "something@gmail.com"
    .Subject = "something"
    .Attachments.Add "D:\X_Temporary\Kanta_Ondrej\wm_logo.png", olByValue, 0
    .HTMLBody = bodyString & chr(60) & "img src=""cid:wm_logo.png"">"
[COLOR=#008000][B]    .GetInspector.Close olSave[/B][/COLOR]
End With

If someone is able to explain the difference, I'm eager to know. ;)
 

Watch MrExcel Video

Forum statistics

Threads
1,113,774
Messages
5,544,142
Members
410,595
Latest member
Tatum2020
Top