本帖最后由 zhaogang1960 于 2011-11-22 16:38 编辑
小结:
本题原来目的在于挖掘调用excel中的单元格或批注中图片更简单的方法,为了不限制大家的思路,取消了api技术的限制。使用api或图表技术调用excel单元格或批注中图片是成熟技术,论坛中有很多例子,大家有兴趣可以搜一下,下面链接是一个api技术调用excel中图片例子:
VBA调用excel中的图片
http://club.excelhome.net/thread-372773-1-1.html
使用另存为网页可以分离出excel单元格或批注中图片,7楼oyzhjr坛友使用的就是此法,Excel2003另存为网页可以得到jpg图片,默认情况下20072010分离单元格图片却是png图片,这时需要设定DefaultWebOptions对象的AllowPNG属性,可分离出gif图片: Application.DefaultWebOptions.AllowPNG= False
下面给出这种简单方法的代码和附件
1、Image控件调用工作表单元格图片(兼容20072010)
- Private Sub TextBox1_Change() '调用单元格图片
- Image1.Picture = LoadPicture("")
- If TextBox1.Text = "" Then Exit Sub
- Dim wb As Workbook, c As Range
- Set c = Sheet1.[a:a].Find(TextBox1.Text, , , xlWhole)
- If Not c Is Nothing Then
- Application.ScreenUpdating = False
- c.Offset(, 1).Select
- If Dir(ThisWorkbook.Path & "\Book1.htm") <> "" Then Kill ThisWorkbook.Path & "\Book1.htm"
- ThisWorkbook.PublishObjects.Delete
- Application.DefaultWebOptions.AllowPNG = False
- With ThisWorkbook.PublishObjects.Add(xlSourceRange, ThisWorkbook.Path & "\Book1.htm", ActiveSheet.Name, c.Offset(, 1).Address, xlHtmlStatic, "Book1", "")
- .Publish (True)
- .AutoRepublish = False
- End With
- If Application.Version <= 11 Then '2003
- Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Book1.files\Book1_image002.jpg")
- Else '20072010
- Image1.Picture = LoadPicture(ThisWorkbook.Path & "\Book1.files\Book1_image002.gif")
- End If
- Image1.PictureSizeMode = 1
- Cells(1, 1).Select
- Application.ScreenUpdating = True
- End If
- End Sub
复制代码
2、Image控件调用工作表批注图片
- Private Sub TextBox1_Change()
- Image1.Picture = LoadPicture("")
- If TextBox1.Text = "" Then Exit Sub
- Dim wb As Workbook, c As Range, p$
- Set c = Sheet1.[a:a].Find(TextBox1.Text, , , xlWhole)
- If Not c Is Nothing Then
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- If Dir(ThisWorkbook.Path & "\Book1.htm") <> "" Then Kill ThisWorkbook.Path & "\Book1.htm"
- p = ThisWorkbook.Path & "\Book1.files"
- Set wb = Workbooks.Add(xlWBATWorksheet)
- c.Copy wb.Sheets(1).[a1]
- wb.SaveAs Filename:=ThisWorkbook.Path & "\Book1.htm", FileFormat:=xlHtml
- wb.Close False
- Image1.Picture = LoadPicture(p & Dir(p & "*.jpg"))
- Image1.PictureSizeMode = 1
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- End If
- End Sub
复制代码
|