Insert image from URL in Excel: error for various methods - image

I am trying to insert a picture from a website into an excel sheet, but I get an error message, no matter which method I try.
The URL I use ("MyURL") is in the format:
https:// x.x.x.x:pppp/chart.png?id=10...apid=secretkey
But if I use any other picture url, it works... even if it has parameters, eg.:
http:// www.mrexcel.com/forum/avatars/[personsname].gif?dateline=2007
MyUrl works fine in any browser, but not in Excel.
I have used these methods:
A) Pictures.Insert
ActiveSheet.Pictures.Insert ("MyURL")
gives error: "Runtime error 1004: Unable to get the Insert property of the Pictures class"
B) Shapes.AddPicture
wsht.Shapes.AddPicture "MyURL", msoFalse, msoTrue, 0, 0, 100, 100
gives error: "Runtime error 1004: The specified file was not found"
C) Insert > Pictures > Paste URL in file name
gives error: "An error occurred while importing this file"
D) Convert MyUrl into a short URL
gives error:
A - "Insert method of picture class failed"
B - "The specified file was not found"
C - "An error occurred while importing this file"
My system:
Windows 8, Excel 2013
What is the problem?

Thanks for the help.
I found a work-around to the problem (not in Excel)... using VBscript to save the image, and then import it into Excel. The problem was with the security certificate. With the script the error is ignored.
dim xHttp: Set xHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xHttp.setOption 2, 13056
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "MyURL", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\BI\SyncScript\graph.png", 2 '//overwrite
end with

It may me related to your Internet Settings and/or Security Settings.............If you run this tiny macro with an empty sheet active:
Sub PictureGrabber()
With ActiveSheet.Pictures
.Insert ("http://www.dogbreedinfo.com/images26/PugPurebredDogFawnBlackMax8YearsOld1.jpg")
End With
End Sub
What happens??

Related

Microsoft Access 2019 Report. Automatically Resize Image Control

Insert and resize a picture in an access report
Solution by Ms Isabel Smit aborts at first line of code.
My Report textbox [P1] populated by query has links to *.jpg files.
But code, Me.Image46.Picture = Me.P1.Value does not work. Get error msg Run-time error '94': Invalid use of Null.
Neither does Me!Image46.Picture = Me![P1] Run-time error '13': Type mismatch
Neither does Me!Image46.Picture = Me![P1].Value
Works if Me!Image46.Picture = hard coded filename but it defeats objective of each record to link to different filename.
Thank you
Updated Solution with validation:
I built your form and tested it. The only time I got it to fail was when the path to the image was invalid. Please try this:
Dim strFilename As String
strFilename = Me![P1].Value
If Len(Dir(strFilename)) > 0 Then
Me![Image7].Picture = strFilename
Me![Image7].Height = 9666 * Me![Image7].ImageHeight / Me![Image7].ImageWidth
Me![Image7].Width = 9666
Else
MsgBox "Image not found"
End If

Vbscript GetObject Returns Unspecified Error

I'm new to vbscript and i'm writing an application to:
open internet explorer
insert search text
click search button
get result count text
So far i have:
Dim obj
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "www.google.com"
WScript.Sleep 4000
IE.Document.getElementById("lst-ib").value = "Test"
IE.Document.Forms(0).Submit()
WScript.Sleep 4000
Set IE = GetObject("","InternetExplorer.Application")
obj = IE.Document.All.item("resultStats").InnerText
MsgBox obj
However, if i run this i get the following error:
Error: Unspecified error
Code: 80004005
I don't know what could be wrong.
Can anyone help me?
Thanks

easiest way to download a file

There are many examples on the Internet and the simplest/easiest for a beginner seems to be:
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "http://bla.com/ea_csv_160126.csv", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "c:\myFolder\ea_csv_160126.csv", 2 '//overwrite
end with
This returns:
Write to file failed code: 800A0BBC source ADODB.Stream
I would like to debug this one, there are people complaining about this error but no fix.
Also, If possible, I would like to define a variable in order to hold the value for the date. So the script should be able to download the file from the website adapting to the changed value in the URL 160126 will be something else.
Note: on the same website, there is a file which has almost the same name ea_csv_update_160126.csv, the one with update shouldn't be taken.
EDIT:
If you know a simpler or equally easy solution which works, please post it.

Download and save a webpage using vbscript

I am trying to give users the option to download and save a webpage to where ever they want. I have been looking all over for a solution but nothing seems to be working. I am using vbscript in classic asp.
This is what I tried last
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", "" &Session("ServerURL") & "/idautomation/IDAutomationStreamingLinear.aspx?D=MAPS$"&request.QueryString("catcode")&"%25"&request.QueryString("typecode")&"&X=0.09&BH=3&S=0&CC=T&LM=5&TM=7.5&ST=F", False
xHttp.Send
with bStrm
.type = 1 '//binary
.open
.write xHttp.responseBody
.savetofile "d:\DownloladPdf.pdf", 2 '//overwrite
end with
but its throwing a "Write to file failed. " on the .savetofile line.
I want the user to be able to chose where to save it to...
You can't save to the users computer from server side code VBscript directly as this would be a major security issue allowing drive by compromises of people browsing a web page. The reason possibly that it is throwing an error is that it is trying to save it server side, but there is no D: on the server.
Instead you want to serve the PDF to the browser using Response and let the browser display or save the PDF.
The below example is using a bytes array rather than a stream, but it should be similar. If you want to force it to download rather than show in the browser, change the content-disposition to attachment. You can also change the filename to whatever you like.
if Len(pdfBytes) > 0 then
Response.Clear()
Response.ContentType = "application/pdf"
Response.Charset = ""
Response.AddHeader "Cache-Control", "public, max-age=1" ' Setting Cache-Control to max-age=1 rather than no-cache due to IE8 bug
Response.AddHeader "content-disposition","inline; filename=filename.pdf"
Response.Buffer = True
Response.Expires = 0
Response.BinaryWrite(pdfBytes)
Response.Flush
Response.End
Response.Close
end if

VBScript and loadXML: Invalid at the top level of the document. How to fix it?

This is my fort post on stackoverflow. I have searched many similiar Q&A's on this site but my conditions seem a bit different. here is my vbscript code:
------------ code snippet ---------------
xmlurl = "songs.xml"
set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML(xmlurl)
if xmlDoc.parseError.errorcode<>0 then
'error handling code
msgbox("error! " & xmlDoc.parseError.reason)
end if
------------ end code snippet ---------------
XML:
<?xml version="1.0" encoding="UTF-8"?>
<nowplaying-info-list>
<nowplaying-info mountName="CKOIFMAAC" timestamp="1339771946" type="track">
<property name="track_artist_name"><![CDATA[CKOI]]></property>
<property name="cue_title"><![CDATA[HITMIX]]></property>
</nowplaying-info>
<nowplaying-info mountName="CKOIFMAAC" timestamp="1339771364" type="track">
<property name="track_artist_name"><![CDATA[AMYLIE]]></property>
<property name="cue_title"><![CDATA[LES FILLES]]></property>
</nowplaying-info>
<nowplaying-info mountName="CKOIFMAAC" timestamp="1339771149" type="track">
<property name="track_artist_name"><![CDATA[MIA MARTINA]]></property>
<property name="cue_title"><![CDATA[TOI ET MOI]]></property>
</nowplaying-info>
</nowplaying-info-list>
I also tried removing the first line in case maybe UTF-8 was not compatible with windows (saw some posts about this), but I still got the same error. I also tried unix2dos and vice versa in case there were carriage return issues (hidden characters embedded in the xml). I just can't seem to figure out what's wrong. It's such a simole XML file. I could parse it in a few minutes using perl regex but I need to run this script on windows so using vbscript. I use the same technique to parse XML from other sources without any issues. I cannot modify the XML unfortunately, it is from an external source.
I have this exact same error on both my Windows Vista home edition and Windows Server 2008. I am running the vbscript from the command line for testing so far (ie not in ASP).
Thanks in advance,
Sam
xmlDoc.loadXML() can load an XML string. It cannot retrieve a URL.
Use an XMLHTTPRequest object if you need to make an HTTP request.
Function LoadXml(xmlurl)
Dim xmlhttp
Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
xmlhttp.Open "GET", xmlurl, false
' switch to manual error handling
On Error Resume Next
xmlhttp.Send
If err.number <> 0 Then
WScript.Echo xmlhttp.parseError.Reason
Err.Clear
End If
' switch back to automatic error handling
On Error Goto 0
Set LoadXml = xmlhttp.ResponseXml
End Function
Use like
Set doc = LoadXml("http://your.url/here")
Three addition remarks:
(1) As .parseError.reason tends to be cryptic, it pays to include its .srcTxt
property (and the parameter to .loadXml):
Dim xmlurl : xmlurl = "song.xml"
Dim xmlDoc : Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = False
xmlDoc.loadXML xmlurl
If 0 <> xmlDoc.parseError.errorcode Then
WScript.Echo xmlDoc.parseError.reason, "Src:", xmlDoc.parseError.srcText
Else
WScript.Echo "surprise, surprise"
End if
output:
Invalid at the top level of the document.
Src: song.xml
Of course, writing a Function/Sub that takes all properties of .parseError
into account and using that always, would be even better.
(2) To load a file or URL, use .load:
Dim xmlDoc : Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Dim xmlurl
For Each xmlurl In Array("song.xml", "http://gent/~eh/song.xml", "zilch")
xmlDoc.async = False
if xmlDoc.load(xmlurl) Then
With xmlDoc.documentElement.firstChild
WScript.Echo xmlurl _
, .tagName _
, .firstChild.tagName _
, .firstChild.text
End With
Else
WScript.Echo xmlurl, xmlDoc.parseError.reason, "Src:", xmlDoc.parseError.srcText
End if
Next
output:
song.xml nowplaying-info property CKOI-ÄÖÜ
http://gent/~eh/song.xml nowplaying-info property CKOI-ÄÖÜ
zilch The system cannot locate the object specified.
Src:
(3) Using the DOM avoids all encoding problems (that's why I put some
german umlauts into 'your' file - which even made it to the DOS-Box output)
and makes RegExps (even Perl's) a second best choice.

Resources