将网页图像插入Excel单元格

我是一个Excel VBA的新手,所以我将不胜感激任何帮助。 我在网上find了下面的代码来把图像转换成电子表格。 这样的代码可以得到的图像,并将该图像放在activesheet,但我需要有一个单元格中的图像。 那可能吗? 下面是我目前的macros。

因此,列A将是getImage2将使用的值,而列B理想地应该具有各自的图像。

 Public Function getImage2(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 ActiveSheet.Shapes.AddPicture imgURL, msoFalse, msoTrue, 100, 100, 250, 250 Else ' End If End Function 

干得好:

 Public Function getImage2(ByVal name As String) As String Dim imgURL As String Dim x&, y&, wdth&, hght& ' using "&" is the same as "As Long" Dim cellFunctionRunsOn As Range Dim img Set cellFunctionRunsOn = Columns(1).Find(what:=name) 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 x = Range(cellFunctionRunsOn.Offset(0, 1).Address).Left y = Range(cellFunctionRunsOn.Offset(0, 1).Address).Top wdth = Range(cellFunctionRunsOn.Offset(0, 1).Address).width hght = Range(cellFunctionRunsOn.Offset(0, 1).Address).height Set img = ActiveSheet.Shapes.AddPicture(Filename:=imgURL, linktofile:=msoFalse, savewithdocument:=msoTrue, Left:=x, Top:=y, _ width:=wdth, height:=hght) img.Placement = xlMoveAndSize Else End If End Function 

这将得到你的形象,然后把它“放入”单元格。

注意:正如@TimWilliams所提到的那样,你不能在技术上把它放在单元格内。 上面所做的是,它把你的形象,并调整到相同的高度和宽度的单元格。 所以,如果你想要一个更大的图像,那么使单元尺寸更大。