以下是 IsPictureInCell 和 CopyCellPictureToClipboard vba 函数。如果您能对这两个函数提出修改建议,让它们能够正确地处理锁定在单元格中的图片,我将不胜感激。
' Checks if a picture exists in the specified cell
Function IsPictureInCell(Cell As Range) As Boolean
Dim shp As Shape, shpRange As Range
For Each shp In Cell.Parent.Shapes
Set shpRange = Cell.Parent.Range(shp.TopLeftCell, shp.BottomRightCell)
If Not Intersect(shpRange, Cell) Is Nothing Then
IsPictureInCell = True
Exit Function
End If
Next
End Function
' Copies pic to clipboard if the Cell contains a picture
Function CopyCellPictureToClipboard(Cell As Range) As Boolean
On Error Resume Next
Dim shp As Shape
For Each shp In Cell.Parent.Shapes
If Not Intersect(shp.TopLeftCell, Cell) Is Nothing Then
shp.Copy
CopyCellPictureToClipboard = True
Exit Function
End If
Next
CopyCellPictureToClipboard = False
End Function
据我所知,没有特殊属性可以确定单元格是否包含图像作为内容(在单元格中,而不是形状对象中)。您只能使用属性Range.HasRichDataType,在本例中等于True。但此属性主要用于识别链接数据类型,因此还应排除链接数据。总的来说,我们将得到这个函数:
Function HasImage(rng As Range) As Boolean
HasImage = rng.HasRichDataType And rng.LinkedDataTypeState = 0
End Function
同时,它可以一直使用,直到 Excel 的创建者想出新的东西。
将单元格中的图像转换为单元格上的图像(对象)时,此函数会立即做出反应,而检查单元格中是否存在对象的IsPictureInCell函数则需要重新计算工作表(Ctrl+Alt+F9)。
对图像的解释: PRAWDA = TRUE , FAŁSZ = FALSE