我在一个文件夹(和子文件夹)中有 3,000 张图像,我想在不同的 excel 列中导入以下属性
-
文件名
-
文件地址
-
图像宽度(以像素为单位)
-
图像高度
-
链接到图像
如何使用VBA执行此操作?
谢谢。
这将为您提供文件名,地址和图像链接的列表。
Sub Macro1()
Path = "C:Pictures"
FileName = Dir(Path & "*.*")
Do While Len(FileName) > 0
Filename = Dir
ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = Filename
ActiveSheet.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Value = Path & Filename
ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Range("B" & Rows.Count).End(xlUp), Address:="""" & Path & Filename & """", TextToDisplay:=Path & Filename
Loop
End Sub
以下代码将所有图片从文件夹带到 excel 工作表...
Sub Macro2()
Dim Pic As Picture
Dim Path As String
Dim FileName As String
Path = "C:Pictures"
FileName = Dir(Path & "*.*")
Do While Len(FileName) > 0
Sheets.Add After:=Sheets(ActiveWorkbook.Sheets.Count)
xFileName = Replace(FileName, ".png", "") 'or jpg
ActiveSheet.Name = xFileName
Set Pic = ActiveSheet.Pictures.Insert(Path & FileName)
FileName = Dir
Loop
End Sub
以下代码获取图像尺寸...
Sub Makro3()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Select
Dim shp As Shape
For Each shp In ws.Shapes
MsgBox "Height: " & shp.Height & " Width: " & shp.Width
Next shp
Next
End Sub