首先,看下 01 ancient.jpg 圖檔的圖形及屬性資料,可以知道它是個 197 * 195 像素的圖檔。
要在 Excel 中,將該圖形檔案插入進來,就需要執行:插入/圖片/從檔案 …
以下說明圖形處理過程,所需用到的程式碼:
- 記錄 Excel 檔案的存放路徑
strFilePath = Application.ActiveWorkbook.Path - 選取所有的圖片,並加以刪除
ActiveSheet.Shapes.SelectAll
Selection.Delete - 設定存放縮小圖片的儲存格高度
' set Row Height
Rows("2:23").Select: Selection.RowHeight = 40 - 逐一插入 22個字母、四種字型圖檔
For i = 1 To 22
ActiveSheet.Pictures.Insert(strFilePath + "\jpg\" + Format(i, "00") + " ancient.jpg").Select
ActiveSheet.Pictures.Insert(strFilePath + "\jpg\" + Format(i, "00") + " block.jpg").Select
ActiveSheet.Pictures.Insert(strFilePath + "\jpg\" + Format(i, "00") + " cursive.jpg").Select
ActiveSheet.Pictures.Insert(strFilePath + "\jpg\" + Format(i, "00") + " book.jpg").Select
Next - 設定圖片縮小後的尺寸
ActiveSheet.Shapes.SelectAll
Selection.ShapeRange.LockAspectRatio = msoFalse
' Selection.ShapeRange.LockAspectRatio = msoTrue
Selection.ShapeRange.Width = 38 ' (72 / 2.54) * 1.4
- 將縮小圖片,逐一放置到指定位置上;程式碼 Int(i / 4) 是在做圖素計算的精度修正!
' set Position
sPos = 18
For i = 1 To 22
ActiveSheet.Shapes("Picture " + Format((i - 1) * 4 + 1, "#")).Select: Selection.ShapeRange.Left = 227: Selection.ShapeRange.Top = sPos + 40 * (i - 1) - Int(i / 4)
ActiveSheet.Shapes("Picture " + Format((i - 1) * 4 + 2, "#")).Select: Selection.ShapeRange.Left = 295: Selection.ShapeRange.Top = sPos + 40 * (i - 1) - Int(i / 4)
ActiveSheet.Shapes("Picture " + Format((i - 1) * 4 + 3, "#")).Select: Selection.ShapeRange.Left = 363: Selection.ShapeRange.Top = sPos + 40 * (i - 1) - Int(i / 4)
ActiveSheet.Shapes("Picture " + Format((i - 1) * 4 + 4, "#")).Select: Selection.ShapeRange.Left = 431: Selection.ShapeRange.Top = sPos + 40 * (i - 1) - Int(i / 4)
Next
沒有留言:
張貼留言