AutoHotKey
현재 셀에 일정 여백을 두고 정확히 그림 삽입하기
쵸코난
2023. 2. 26. 02:38
참고: 지식인
선택한 셀에 일정 여백을 두고 그림을 빠르게 삽입할 수 있게 AutoHotKey로 만들어 보았습니다.
여백은 exe 파일명 _뒤에 숫자를 넣어주면 됩니다.
XLinsertPicture_5.exe 이면 여백을 5px 둡니다. _이 없으면 기본 여백 5를 두고 삽입하고 XLinsertPicture_0.exe 이면 여백 없이 삽입하겠습니다.
첨부한 exe 파일(XLInsertPicture_5.exe )을 실행하고 엑셀 셀에서 F3을 누르면 여백 5px을 남기고 현재 셀에 선택한 그림파일을 정확히 삽입해줍니다. 해당 셀의 기존 그림은 삭제합니다. 그림도형의 이름은 파일명으로 지정합니다.
그림 폴더는 엑셀파일이 있는 폴더에서 시작하고 한 번 선택하면 해당 폴더에서 시작합니다.
실행화면입니다.

실행화일 실행하고 대화창에서 확인을 누르면 상태표시줄 Tray로 최소화됩니다.
Win + X 키를 누르거나 Tray의 아이콘을 우클릭하고 Exit를 누르면 완전히 종료합니다.
단축키는 F3인데 엑셀창이 떠 있을 때만 작동합니다.
AHK 코드:
더보기
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
CoordMode, Pixel, Screen
#SingleInstance
sName := % SubStr( A_ScriptName, 1, -4)
StringSplit, tmp, sName, _
if ( tmp2 = "") {
m := 5
}
else {
m := tmp2
}
MsgBox, 0x40, XLInsertPictureInCell, Press <F3> to insert a picture with margin( %m% ) in the current cell.`n`nPress <Win+x> to quit.
Menu, Tray, Icon, shell32.dll, 143
Menu, Tray, Tip, <F3> to insert a picture [Win+X]: Exit
Menu, Tray, NoStandard
Menu, Tray, Add, Exit, ExitMenu
return
$F3::
xl:=ComObjActive("Excel.Application")
xl.Visible := True
IfWinActive, ahk_exe EXCEL.EXE
{
;Select an image
path := xl.ActiveWorkbook.Path . xl.PathSeparator
;sFile := xl.GetOpenFilename("Image File, *.jpg;*.png;*.gif;*.emf;*.svg")
FileSelectFile, sFile, 1, %path%, Select image, Image File ( *.jpg;*.png;*.gif;*.emf;*.svg)
if (sFile = "") {
return
}
;delete old images if any
sht := xl.ActiveSheet
i := sht.Shapes.Count
Loop, %i%
{
If (sht.Shapes(i).TopLeftCell.Address = xl.ActiveCell.Address) {
sht.Shapes(i).Delete
}
i := i - 1
}
;calc location
rng := xl.ActiveCell.MergeArea
;m := 5
pLeft := rng.Left + m
pTop := rng.Top + m
pWidth := rng.Width - m * 2
pHeight := rng.Height - m * 2
;MsgBox, % pLeft "," pTop "," pWidth "," pHeight
;insert the image
shp := sht.Shapes.AddPicture(sFile, 0, 1, pLeft, pTop, pWidth, pHeight)
SplitPath, sFile, name
shp.Name := name
xl:=""
}
return
ExitMenu:
#x::
ExitApp
참고로 위 기능을 엑셀 자체 기능으로 하려면 아래와 같은 VBA코드로 가능합니다.
VBA코드:
더보기
Sub InsertPictureHere()
Dim sht As Worksheet
Dim rng As Range
Dim shp As Shape
Dim m As Single
Dim sFile As Variant
'ChDir ThisWorkbook.Path & Application.PathSeparator
'sFile = Application.GetOpenFilename("Image File, *.jpg;*.png;*.gif;*.emf;*.svg")
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Image File", "*.jpg;*.png;*.gif;*.emf;*.svg"
.InitialFileName = ThisWorkbook.Path & Application.PathSeparator
If .Show = -1 Then sFile = .SelectedItems(1)
End With
If sFile <> "" Then
Set rng = Application.ActiveCell.MergeArea
Set sht = rng.Parent
For i = sht.Shapes.Count To 1 Step -1
If sht.Shapes(i).TopLeftCell Is ActiveCell Then
sht.Shapes(i).Delete
End If
Next i
m = 5
sht.Shapes.AddPicture sFile, 0, 1, rng.Left + m, rng.Top + m, rng.Width - 2 * m, rng.Height - 2 * m
'Set shp = sht.Shapes.AddPicture2(sFile, 0, 1, rng.Left + m, rng.Top + m, rng.Width - 2 * m, rng.Height - 2 * m, msoPictureCompressDocDefault)
Set shp = sht.Shapes(sht.Shapes.Count)
shp.Name = sFile
End If
End Sub
XLInsertPicture_10.ahk
0.00MB
XLInsertPicture_5.exe
0.86MB
InserPicture11.xlsm
0.09MB