[英]Save picture as a picture instead of picture link when using VBA in Excel
I have an excel file for label template with 6,300 items (each item has a parent ID which matches the picture name that suits the child item), I was able after a lot of searching to find a code that will run all the way through with out an error (when items are missing for example).我有一个包含 6,300 个项目的标签模板的 excel 文件(每个项目都有一个与适合子项目的图片名称相匹配的父 ID),经过大量搜索,我能够找到一个可以一直运行的代码出现错误(例如,当项目丢失时)。 However when I want to share the item it has the pictures saved as a link instead of an actual picture, and whoever receive that file will have a broken link message.但是,当我想共享该项目时,它会将图片保存为链接而不是实际图片,并且收到该文件的任何人都会收到断开的链接消息。 Can anyone help me change my code to save as picture?谁能帮我更改我的代码以另存为图片?
Sub Picture()
Dim pictname As String
Dim pastehere As Range
Dim pasterow As Long
Dim x As Long
Dim lastrow As Long
lastrow = Worksheets("sheet2").Range("b1").CurrentRegion.Rows.Count
x = 2
For x = 2 To lastrow
On Error GoTo errhandler:
Set pastehere = Cells(x, 1)
pasterow = pastehere.Row
Cells(pasterow, 1).Select 'This is where picture will be inserted
pictname = Cells(x, 3) 'This is the picture name
ActiveSheet.Pictures.Insert("C:\Users\BennyCohen\Pictures\Catalogue pics\" & pictname & ".jpg").Select 'Path to where pictures are stored
With Selection
.Left = Cells(pasterow, 1).Left
.Top = Cells(pasterow, 1).Top
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.Height = 140
.ShapeRange.Width = 80
.ShapeRange.Rotation = 0#
.linktofile = msoFalse
.savewithdocument = msoCTrue
End With
Next
errhandler:
Range("A" & x).Value = "Review"
Resume Next
End Sub
Thanks in advance, live long and prosper ;)提前致谢,长寿和繁荣;)
linktofile
and savewithdocument
are not picture properties and the error is masked by the Resume Next
in the errhandler, see here . linktofile
和savewithdocument
不是图片属性,错误处理程序中的Resume Next
掩盖了错误,请参见此处。 Use Shapes.addPicture()
.使用Shapes.addPicture()
。
Sub Picture()
Const FOLDER = "C:\Users\BennyCohen\Pictures\Catalogue pics\"
Dim wb As Workbook, ws As Worksheet
Dim lastrow As Long, r As Long, pictname As String
Dim n As Long, m As Long
Set wb = ActiveWorkbook ' or ThisWorkbook
Set ws = wb.Sheets("Sheet2")
lastrow = ws.Range("B1").CurrentRegion.Rows.Count
For r = 2 To lastrow
pictname = FOLDER & ws.Cells(r, 3) & ".jpg" 'This is the picture name
' check file exists
If Len(Dir(pictname)) > 0 Then
With ws.Shapes.AddPicture(pictname, _
linktofile:=msoFalse, savewithdocument:=msoTrue, _
Left:=ws.Cells(r, 1).Left, _
Top:=ws.Cells(r, 1).Top, _
Height:=140, Width:=80)
.LockAspectRatio = msoFalse
.Rotation = 0#
End With
n = n + 1
Else
ws.Cells(r, "A") = "Review"
m = m + 1
End If
Next
MsgBox n & " Pictures inserted " & _
m & " Pictures to review", vbInformation
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.