PDA

View Full Version : Programmatically importing pictures from a URL



JorgeO90
06-26-2020, 02:40 AM
Hi!

I'm trying to automatically import a picture from an url. But I get the error message: Run time error 5152. This is not a valid file name.

It works fine if I do it manually through Insert -> Pictures. And pastes the URL into the file name field.
But if I try with the simple code:



Sub InsertPic()
'
' Test to import pic from url
'
'

Selection.InlineShapes.AddPicture FileName:= _
"https://removeofurl.com/imgapi/imgproxy/img?id=2459&width=1500px&fontsize=24px&zoom=100%25&unofficial=false&topleft=true&target=img-s1" _
, LinkToFile:=False, SaveWithDocument:=True

End Sub


I get the error. Is it that Word don't understand the url due to the &, = and % ? . Cause if you run with a direct link to a image it works as it should. We run a image creator at a webserver that creates the jpg.

Is it possible make Word understand that it is a jpg? Cause it works when you do it manually.

I'm running Word 365 (v 1905)

Thanks

BR
Jorge O

heedaf
07-01-2020, 04:16 PM
Try recording a macro while you import the url picture and see what it gives you.

JorgeO90
07-02-2020, 12:21 AM
Thanks, Tried that. Works fine when you record the macro and do it manual. But when you run the macro you get the Run time error 5152. Might be some authentication to our website but I also tried running Fiddler to see what happens and I just get 200 responses in the http traffic...

I have now solved it with some code that downloads the pictures to disc first and then adds it with Selection.InlineShapes.AddPicture