How to insert a picture programatically

S

Southern at Heart

Via a custom tool bar button, I want to use vba to insert a picture into the
body of my email (not an attachment). The picture's URL is strURL. Does
anyone know how to do this?
 
S

Sue Mosher [MVP]

The appropriate technique depends on the Outlook version and, in older
versions, whether Word is the email editor. Please always include such
details when you post Outlook questions.
 
S

Southern at Heart

Oh yes, sorry,
I have Outlook 2003, and I use Word 2003 as my email editor...
many thanks,
 
S

Sue Mosher [MVP]

In that case, you can return a Word.Document using Inspector.WordEditor and
then use Word methods to perform the insertion, something like this, where
objMsg is the message you want to modify:

strFile = "C:\Pictures\logo.gif"
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
Set objShape = objSel.InlineShapes.AddPicture _
(strFile, False, True)
objDoc.Hyperlinks.Add objShape.Range, strURL
End If

This technique would also work in Outlook 2007, which always uses Word as
the email editor.
 
S

Southern at Heart

I don't understand everything I know about this!

In Outlook 2003, using Word 2003 as my editor, I click on the new message
button to start a new email. It won't let me 'customize' the toolbar, but I
see custom buttons up there that I made in Word. So I cut my code out of
'ThisOutlookSession' and opened up Word and pasted it in for Normal.dot, then
I added a button on Word's toolbar to run it. I close Word, go back to
Outlook, click New Email, and now I see my custom button! Hurray, I'm
getting somewhere. But now, it doesn't work. It seems like I am missing the
part that defines objMsg? How do I do this?
Also, I don't understand the reference of 'strFile = "C:\Pictures\logo.gif"
because the picture I'm trying to paste is located on the web at strURL
Thanks for your patience with SouthernAtHeart!

Here's my code:
Sub InsertPicture()

strFile = "C:\Pictures\logo.gif"
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
Set objShape = objSel.InlineShapes.AddPicture _
(strFile, False, True)
objDoc.Hyperlinks.Add objShape.Range, strURL
End If
 
S

Sue Mosher [MVP]

I'm sorry, but I may have misinterpreted what you wanted to do. I thought
that you wanted to insert an image you already had and use it to link to
some location with strURL. But now it seems that you want to insert an image
that only exists on the Internet. Is that correct? In general, this is not a
good practice in email messages, because Outlook and many other mail
programs by default will not load content from the Internet, for security
and privacy reasons. Best practice is to embed the image into the message
from a file that already exists locally, as shown in the code I posted.

If you insist on using a web-based image, then you would need to modify the
HTMLBody property of the message to insert an <img> tag at the desired
location, just the same as you would with a web page. The code sample at
http://www.outlookcode.com/codedetail.aspx?id=1358 shows how to insert an
HTML content string into the current cursor position, but I don't know
whether it will work if Word is the email editor. You'll have to try it and
see.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Southern at Heart said:
I don't understand everything I know about this!

In Outlook 2003, using Word 2003 as my editor, I click on the new message
button to start a new email. It won't let me 'customize' the toolbar, but
I
see custom buttons up there that I made in Word. So I cut my code out of
'ThisOutlookSession' and opened up Word and pasted it in for Normal.dot,
then
I added a button on Word's toolbar to run it. I close Word, go back to
Outlook, click New Email, and now I see my custom button! Hurray, I'm
getting somewhere. But now, it doesn't work. It seems like I am missing
the
part that defines objMsg? How do I do this?
Also, I don't understand the reference of 'strFile =
"C:\Pictures\logo.gif"
because the picture I'm trying to paste is located on the web at strURL
Thanks for your patience with SouthernAtHeart!

Here's my code:
Sub InsertPicture()

strFile = "C:\Pictures\logo.gif"
Set objInsp = objMsg.GetInspector
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
Set objShape = objSel.InlineShapes.AddPicture _
(strFile, False, True)
objDoc.Hyperlinks.Add objShape.Range, strURL
End If
 
S

Southern at Heart

Hi,
Let me explain more simply what I want to do:
I have strURL that is the location of a picture, say for example:
strURL = http://www.google.com/intl/en_ALL/images/logo.gif

....I just want to 'paste' the image in the email body as a picture. I don't
want the email to contain a link to the web based picture, but I want to
simply paste the picture just as if it were a picture on my PC that I wanted
to paste into the email. I don't want the picture to be an attachment, just
pasted in the body of the email.
Thank much! Sorry I'm not able to explain myself very well sometimes.
 
S

Sue Mosher [MVP]

I'm confused. Do you have only the URL for the picture? Or do you have the
picture on the clipboard, ready to paste?
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Southern at Heart said:
Hi,
Let me explain more simply what I want to do:
I have strURL that is the location of a picture, say for example:
strURL = http://www.google.com/intl/en_ALL/images/logo.gif

...I just want to 'paste' the image in the email body as a picture. I
don't
want the email to contain a link to the web based picture, but I want to
simply paste the picture just as if it were a picture on my PC that I
wanted
to paste into the email. I don't want the picture to be an attachment,
just
pasted in the body of the email.
Thank much! Sorry I'm not able to explain myself very well sometimes.
 
S

Southern at Heart

I have the string strURL, which is the address of the picture. Just like the
example I gave:

strURL = http://www.google.com/intl/en_ALL/images/logo.gif

I don't know how to make this any clearer. I just want to paste a picture
into the body of my email, a picture that's located at the address strURL. I
don't have the picture on the clipboard, I don't have the picture on my
computer, I don't have the picture period. But it's located on the web, at
the address strURL. I don't want a link in my email that refers to the
picture, I want the picture itself. Maybe I'm asking something that is not
possible. Sorry, to be so confusing.
 
S

Sue Mosher [MVP]

If you don't have the picture on your clipboard, then you can't paste it, by
definition.

If all you have is the web URL, then you have two choices:

1) Download the picture to your computer so you can use the Word method
proposed to you. I cannot help you with the code to download the picture
programmatically, because I don't know how to do that.

2) Insert the picture as an <img> tag, using the HTMLEditor method proposed
to you.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
S

Southern at Heart

Is this possible to do?
thanks

Southern at Heart said:
I have the string strURL, which is the address of the picture. Just like the
example I gave:

strURL = http://www.google.com/intl/en_ALL/images/logo.gif

I don't know how to make this any clearer. I just want to paste a picture
into the body of my email, a picture that's located at the address strURL. I
don't have the picture on the clipboard, I don't have the picture on my
computer, I don't have the picture period. But it's located on the web, at
the address strURL. I don't want a link in my email that refers to the
picture, I want the picture itself. Maybe I'm asking something that is not
possible. Sorry, to be so confusing.
 
S

Southern at Heart

Thanks, I didn't see that post. I try finding out how to get the picture
onto the clipboard...
cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top