Feb 11 2014
Retrieve images of chemical structures using Excel VBA
The below is from an answer I posted on stackoverflow.
You can retrieve the chemical structure of an image using the following:
Sub Run() getImage ("iron") End Sub Public Function getImage(ByVal name As String) As String Dim imgURL As String Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP") XMLhttp.setTimeouts 1000, 1000, 1000, 1000 imgURL = "http://cactus.nci.nih.gov/chemical/structure/" + name + "/image" XMLhttp.Open "GET", imgURL, False XMLhttp.send If XMLhttp.Status = 200 Then 'It exists so get the image Sheets(1).Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 250, 250 Else ' End If End Function
This can further simplified to simply only use
Sheets(1).Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 300, 300
Instead of downloading the image the twice, and simply using an error handler to catch when image not found.
Reference:
- Can I use VBA to import images (gifs) from the web into Excel?
- http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture.ASPX