AutoHotKey

도형(개체)을 표의 셀 한 가운데 위치시키기

쵸코난 2025. 4. 24. 05:29

 

현재 도형을 원하는 셀의 정확히 가운데로 위치시키거나

셀에 가득차게 자동으로 조절해주는 기능을

파워포인트 자체적으로 지원하지는 않습니다.

해당 셀이 표의 가운데라면 도형을 이동시킬 때 스마트 가이드기능이 켜져 있다면

셀의 중심을 점선으로 표시해줍니다.

표의 모든 셀의 경계선에 안내선(가이드선)을 추가하는 방법도 있겠습니다.

https://konahn.tistory.com/entry/DrawGuidesAlongTableBorders

 

테이블(표) 윤곽선 따라 가이드 선 자동 추가

관련: 지식인 엑셀은 셀이 작아서 Alt키를 누른 채로 마우스로 도형을 움직이면 자석처럼 맞춰줍니다. 하지만 파워포인트는 테이블(표)의 크기가 다양해서 그런지 표의 경계에 맞게 이동시켜주

konahn.tistory.com

 

 

여기서는 VBA를 이용하는 방법과 오토핫키를 이용하는 방법을 소개드립니다.

1. VBA를 이용

Alt+F11창에서 삽입 > 모듈 추가한 후에 아래와 같은 VBA코드를 붙여 넣고

원하는 도형 왼쪽 상단 꼭지점을 원하는 셀 안에 위치시킨 상태에서

Alt+F8누르고 CenterInCell을 실행하면 현재 도형을 셀 가운데로 이동시켜주고,

FitToCell을 실행하면 셀 안에 가득차게 조정합니다.

 

더보기
Option Explicit
'by konahn, 2025

'현재 도형을 특정셀의 가운데로 이동
Sub CenterInCell()
    
    Dim shp As Shape, sshp As Shape, cel As Cell
    Dim sld As Slide
    Dim r%, c%
    
    On Error Resume Next
    Set sshp = ActiveWindow.Selection.ShapeRange(1)
    On Error GoTo 0
    If sshp Is Nothing Then MsgBox "도형(개체)를 선택하세요.": Exit Sub
    Set sld = sshp.Parent
    
    Set cel = FindCell(sld, sshp)
    
    If cel Is Nothing Then
        MsgBox "현재 도형(개체)를 포함하는 셀(표)를 발견하지 못했습니다." & vbNewLine & vbNewLine & _
            "현재 도형의 왼쪽 상단 꼭지점을 원하는 셀 안으로 이동하세요.", vbExclamation
    Else
        With cel.Shape
            sshp.Left = .Left + .Width / 2 - sshp.Width / 2
            sshp.Top = .Top + .Height / 2 - sshp.Height / 2
        End With
    End If

End Sub

' 현재 도형이 들어 있는 셀 찾기
Function FindCell(oSld As Slide, oShp As Shape) As Cell
    Dim shp As Shape, cel As Cell
    
    For Each shp In oSld.Shapes
        If shp.Type = msoTable Then
            With shp.Table
                For r = 1 To .Rows.Count
                    For c = 1 To .Columns.Count
                        With .Cell(r, c).Shape
                            If oShp.Left >= .Left And oShp.Left <= .Left + .Width And _
                                oShp.Top >= .Top And oShp.Top <= .Top + .Height Then
                                Set cel = shp.Table.Cell(r, c): Exit For
                            End If
                        End With
                    Next c
                    If Not cel Is Nothing Then Exit For
                Next r
            End With
        End If
        If Not cel Is Nothing Then Exit For
    Next shp
    If Not cel Is Nothing Then Set FindCell = cel
End Function

'현재 도형을 특정셀에 가득차게 조정
Sub FitToCell()
    
    Dim shp As Shape, sshp As Shape, cel As Cell
    Dim sld As Slide
    Dim r%, c%
    
    On Error Resume Next
    Set sshp = ActiveWindow.Selection.ShapeRange(1)
    On Error GoTo 0
    If sshp Is Nothing Then MsgBox "도형(개체)를 선택하세요.": Exit Sub
    Set sld = sshp.Parent
    
    Set cel = FindCell(sld, sshp)
    
    If cel Is Nothing Then
        MsgBox "현재 도형(개체)를 포함하는 셀(표)를 발견하지 못했습니다." & vbNewLine & vbNewLine & _
            "현재 도형의 왼쪽 상단 꼭지점을 원하는 셀 안으로 이동하세요.", vbExclamation
    Else
        sshp.LockAspectRatio = msoFalse
        With cel.Shape
            sshp.Width = .Width
            sshp.Height = .Height
            sshp.Left = .Left
            sshp.Top = .Top
        End With
    End If

End Sub

 

 

현재 선택된 개체는 도형, 그림, 미디어, 표 등이어도 됩니다.

현재 개체의 왼쪽 상단 꼭지점을 기준으로 하기 때문에

현재 개체의 왼쪽 상단 꼭지점을 원하는 셀 안에 위치시켜야 이동시킬 대상 셀을 찾아 이동시킵니다.

필요할 때 마다 이 VBA파일을 열어놓아야 하므로

자주 사용한다면 이 VBA를 따로 추가기능으로 만들어 놓아야 겠습니다.

아니면 빠른 실행에 추가해서 Alt+숫자키로 실행할 수도 있습니다.

2. AutoHotKey를 이용

F3키와 F4키를 누를 때

추가기능 대신 필요할 때마다 첨부한 ToCellCenter.exe를 실행하고

F3키와 F4키를 눌러서 똑같은 기능을 수행할 수 있습니다.

 

더보기
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
 
	WinSetTitle, ToCellCenter

	MsgBox ,0x40, ToCellCenter, Hit <F3> to move the current shape(object) to the center of a specific cell or hit <F4> to make it fit the size of the cell.`n`nHit <Win+X> to quit
	if (%A_IsCompiled%)  {
		Menu, Tray, Icon, %A_ScriptName%, 1
	}
	Menu, Tray, NoStandard
	Menu, Tray, Add, Exit, ExitMenu
 
return
 

#IfWinActive, ahk_class PPTFrameClass
~F3::
~F4::	
	try {
		pApp := ComObjActive("PowerPoint.Application")
		sshp := pApp.ActiveWindow.Selection.ShapeRange(1) 
		sld := pApp.ActiveWindow.Selection.SlideRange(1) 		;sshp.Parent
		
		i := 0
		cl := ""
		Loop, % sld.Shapes.Count {
			i := i + 1
			shp := sld.Shapes(i)
			;MsgBox, % shp.Name 			
			if (shp.Type = 19 and shp.Id !=sshp.Id) {

				Loop, % shp.Table.Rows.count	{
					r := A_Index
					Loop, % shp.Table.Columns.Count {
						c := A_Index
						sh := shp.Table.Cell(r, c).Shape
						;msgbox % sh.Width
						if ( sshp.Left >= sh.Left and sshp.Top >= sh.Top and sshp.Left <= sh.Left + sh.Width and sshp.Top <= sh.Top + sh.Height ) {
							cl := sh
							break, 3 
						}
					}
				}
			}
		}
		
		if(!cl) {
			MsgBox, 현재 도형(개체)를 포함하는 셀이 없습니다.`n`n현재 도형 왼쪽 상단 꼭지점을 원하는 셀 안으로 이동시키세요.
		}
		else {
			hkey := SubStr(A_Thishotkey, 2, 2)
			;MsgBox, % hkey
			if( hkey = "F4" ) {
				;MsgBox, F4 pressed
				sshp.LockAspectRatio := 0
				sshp.Width := cl.Width
				sshp.Height:= cl.Height
				sshp.Left := cl.Left
				sshp.Top := cl.Top 
			}
			else {
				sshp.Left := cl.Left + cl.Width/2  - sshp.Width/2
				sshp.Top := cl.Top + cl.Height/2  - sshp.Height/2
			}
		}
	}
	catch e {
		;no ppt
		if (!sshp) {
			MSgBox, 먼저 도형(개체)를 선택하세요.
		}
		else {
			MsgBox, % e.Message
		}	
	}
 
return

#IfWinActive
ExitMenu:
#x::
exitapp

 

실행화면:

 

 

 

 

Autohotkey 코드파일 및 실행파일과 VBA 매크로가 포함된 pptm샘플파일 첨부합니다.

 

 

ToCellCenter1.ahk
0.00MB