首页 > 解决方案 > 如何将边框应用于使用 VBA 在 Excel 工作表中作为图片元素(Dim var as Picture)插入的图像?

问题描述

我想使用变量将边框应用于插入到 excel 工作表中的图像:Dim Pic as Picture来自本地目录(在此处下载后。)

我曾尝试在线搜索许多网站以寻求帮助,但没有任何帮助,因为其中大多数是针对 Shape 变量而不是针对 Picture 变量。

我是否也必须使用形状变量,或者有没有办法在我键入“Pic”时应用边框。然后我可以看到 Pic.border 的选项可用,但我不知道如何使用它。请帮助解决这个问题。

.....
URLDownloadToFile 0, imgsrc, dlpath & code + ".jpg", 0, 0

Dim PicPath As String, Pic As Picture, ImageCell As Range

PicPath = dlpath & unique_code & ".jpg"

Set ImageCell = Cells(i, "C").MergeArea

Set Pic = ActiveSheet.Pictures.Insert(PicPath)
Rows(i).RowHeight = 160

With Pic
    .ShapeRange.LockAspectRatio = msoTrue
    .Left = ImageCell.Left
    .Top = ImageCell.Top
    .Width = ImageCell.Width
    .Height = ImageCell.Height
End With
.....

需要对这些图像应用边框。

我想用细边框包围图片。截至目前,它们正在覆盖它们所属的单元格的边界

标签: excelvbaimageborder

解决方案


要添加宽度为 1 的边框,请With按如下方式修改您的代码部分:

With Pic
    .ShapeRange.LockAspectRatio = msoTrue
    With .ShapeRange.Line
        .Visible = msoTrue
        .Weight = 1
    End With
    .Left = ImageCell.Left
    .Top = ImageCell.Top
    .Width = ImageCell.Width
    .Height = ImageCell.Height
End With

您可以在嵌套的With.


推荐阅读