1

I can insert manually a picture into an outlook 2010 new email using "Insert picture" and the “insert link to file” feature. In the File field I enter the link: http://www.example.com/image.php?s1=song1.net & c1=composer

The link returns an image and I can see it in the body of the email.

I need to enter this URL using VBA. I wrote the code below and it does not work. When I tried to run it came with the following message: Run-time error ‘4198’: Command failed. It highlights the line that includes the link.

My code:

Sub insertHTMLFile()
Dim Insp As Inspector
Set Insp = ActiveInspector
If Insp.IsWordMail Then
Dim wordDoc As Word.Document
Set wordDoc = Insp.WordEditor
wordDoc.Application.Selection.InsertFile "http://www.example.com/image.php?s1=song1.net & c1=composer ", , False, False,   False
End If
End Sub

I would appreciate it if you can show me how I can use VBA to insert the image as I did it manually. Unfortunately Outlook does not have a macro recorder which could show me the instructions how to do it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Menachem
  • 265
  • 1
  • 4
  • 17

2 Answers2

0

Try to use %20 to replace/encode spaces in the URL string.

Also I'd suggest recording a VBA macro in Word to see the exact line of code required to get the job done using the Word object model. Word provides the Macro recorder which allows to do the job manually and get the code generated for you in the background. See Record or run a macro for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • In my original code I did not have spaces therefore this will not help. – Menachem Aug 16 '15 at 20:24
  • I iwll try the word suggestion – Menachem Aug 16 '15 at 20:25
  • It is a creative idea to look at the recorded instructions in Word. I am afraid that it did not help me in this case. I recorded the instructions in Word and the recorded macro worked in Word fine and did not work in Outlook. However when I tried to use it in Outlook it crated the following error: Run-time error ‘91’: Object variable or With block variable not set. Here is the code Word generated: Selection.InlineShapes.AddPicture FileName:= _ "http://www.example.com/image.php? s1=song1.net&c1=composer” _ , LinkToFile:=True, SaveWithDocument:=False Any ideas to make it work? – Menachem Aug 16 '15 at 22:26
0

The following script worked fine for me in Outlook:

Set wordDoc = Application.ActiveInspector.WordEditor
wordDoc.Application.Selection.InlineShapes.AddPicture "http://www.dimastr.com/redemption_logo.png", true, false
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78