【问题标题】:Excel VBA - insert bulk images in sheetExcel VBA - 在工作表中插入批量图像
【发布时间】:2017-03-06 18:02:15
【问题描述】:

我正在使用下面的 vba 代码在 excel 工作表中获取图像,但此代码将工作表中的图像作为链接添加,所以当我将工作表发送到另一台电脑时,该人获取图像位置未找到错误。

如何在工作表中添加附加图像而不是图像链接???

Sub AddOlEObject()
    Dim mainWorkBook As Workbook

    Set mainWorkBook = ActiveWorkbook
    Sheets("Object").Activate
    Folderpath = "C:\phoenix"
    Set fso = CreateObject("Scripting.FileSystemObject")
    NoOfFiles = fso.GetFolder(Folderpath).Files.Count
    Set listfiles = fso.GetFolder(Folderpath).Files

    For Each fls In listfiles
        strCompFilePath = Folderpath & "\" & Trim(fls.name)
        If strCompFilePath <> "" Then
            If (InStr(1, strCompFilePath, ".jpg", vbTextCompare) > 1) Then
                counter = counter + 1
                Sheets("Object").Range("A" & counter).Value = fls.name
                Sheets("Object").Range("B" & counter).ColumnWidth = 50
                Sheets("Object").Range("B" & counter).RowHeight = 150
                Sheets("Object").Range("B" & counter).Activate
                Call insert(strCompFilePath, counter)
                Sheets("Object").Activate
            End If
        End If
    Next

End Sub

Function insert(PicPath, counter)
    'MsgBox PicPath
    With ActiveSheet.Pictures.insert(PicPath)
        With .ShapeRange
            .LockAspectRatio = msoTrue
            .Width = 100
            .Height = 150
        End With
        .Left = ActiveSheet.Range("B" & counter).Left
        .Top = ActiveSheet.Range("B" & counter).Top
        .Placement = 1
        .PrintObject = True
    End With
End Function

【问题讨论】:

标签: vba excel


【解决方案1】:

该图像是您保存在您经常使用的个人目录中的单个图像吗?图像是否还保存为 .JPEG?

为什么不使用下面的简单 VBA 代码?

Sub CALLPICTURE()


Worksheets("SHEET1").Shapes.AddPicture Filename:="I:\Control\DECOMP\ Images\Zebra.jpg", linktofile:=msoFalse, _
            savewithdocument:=msoCTrue, Left:=0, Top:=0, Width:=632, Height:=136


End Sub

您可以添加任意数量的图像。

【讨论】:

  • 不工作,因为我需要在工作表中获取批量图像
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多