|
仅仅针对JPG格式的相片有效,请楼主试用下。
- Sub Insert_Picture()
- VarFileName = Application.GetOpenFilename("All Files (*.*), *.*", MultiSelect:=False)
- VarFileNameArr = Split(VarFileName, "")
- n = UBound(VarFileNameArr)
- Debug.Print n, VarFileNameArr(n)
- l = Len(VarFileNameArr(n))
- FilePath = Left(VarFileName, Len(VarFileName) - Len(VarFileNameArr(n)))
- Set tmpRange = ThisWorkbook.Sheets("Sheet1").Range("B1")
- Application.ScreenUpdating = False
- t = tmpRange.Top
- l = tmpRange.Left
- h = tmpRange.Offset(tmpRange.Rows.Count, 0).Top - tmpRange.Top
- w = tmpRange.Offset(0, tmpRange.Columns.Count).Left - tmpRange.Left
- On Error GoTo ErrTrap
- Debug.Print tmpRange.Parent.Name
- Set p = tmpRange.Parent.Pictures.Insert(FilePath & Range("A1").Value & ".JPG")
- With p
- .Top = t
- .Left = l
- .Height = h
- .Width = w
- End With
- Set tmpRange = Nothing
- Set p = Nothing
- exitsub:
- Application.ScreenUpdating = True
- Exit Sub
- ErrTrap:
- Application.ScreenUpdating = True
- ErrNumber = Err.Number
- If ErrNumber = 1004 Then
- MsgBox "The file selected is not a valid picture!", vbInformation
- End If
- Set tmpRange = Nothing
- Set p = Nothing
- GoTo exitsub
- End Sub
复制代码 |
|