
텍스트상자에서 특정 부분의 색상을 바꿀 때 빠르게 단축키로 변경하고 싶은 경우입니다.
마우스로 움직이는 것보다는 단축키가 직접적이고 빠릅니다.
원하는 색상이 몇가지 있고 특정 단축키를 지정해놓고 빠르게 변경하는 것이 목표입니다.
여기서는 빨간색, 녹색, 파란색, 노란색 등 대표적인 몇 색상에 Ctrl+1, Ctrl+2, Ctrl+3, Ctrl+4 등의 키를 지정하겠습니다.
1. VBA로는 아래와 같이 현재 선택된 텍스트에 #FF0000 빨간색을 적용할 수 있습니다.
Sub Macro()
    ActiveWindow.Selection.TextRange.Font.Color.RGB = RGB(255, 0, 0)
    
End Sub장점은 원하는 색상으로 수정이 아주 편한 점이고
단점은 매크로 파일을 열어야 한다는 귀찮은 점과 Alt+<숫자키>만 사용할 수 있다는 점입니다.
2. 오토핫키를 이용한다면 지난 번에 만들었던 OfficeAutohotKey 로 단축키를 지정할 수도 있습니다.
https://konahn.tistory.com/entry/OfficeAutoHotKey

차례대로 Alt-h,fc,{TAB},{TAB},{RIGHT},{ENTER} 를 입력하면 빨간색으로 지정하기 때문에
파일명을 "Alt-hfc{TAB}{TAB}{RIGHT}{RIGHT}{ENTER}.exe"로 수정해서 실행하고 F3을 눌러주면 됩니다.

장점은 파일명만 바꾸면 된다는 점이고
단점은 단축키 시퀀스가 길다는 것과 여러가지 색상을 적용하기 어렵다는 점입니다.
3. 그래서 Autohotkey 로 다시 색상을 바꾸는 기능을 만들어 보았습니다.
아래와 같은 코드가 주요 부분입니다.
SetFontRGB( R, G, B ) {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange
		}
		catch e {
			MsgBox Select a text range first.
		}
		tr.Font.Color.RGB := B * 65536 + G * 256 + R
		ppt := ""
	}
}
RGB색상을 3개씩 인수로 넘겨야 합니다.
Red, Navy 등 색상이름만 아는 경우는 불편하므로 색상이름과 RGB값을 연결하겠습니다.
SetFontColor( myColor ) {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange
		}
		catch e {
			MsgBox Select a text range first.
		}
		tr.Font.Color.RGB := getRGBFrom( myColor )
		ppt := ""
	}
}
getRGBFrom(colorName) {
    colorMap := { "Black": "0,0,0"
		, "White": "255,255,255"
		, "Red": "255,0,0"
		, "Green": "0,255,0"
		, "Blue": "0,0,255"
		, "Yellow": "255,255,0"
        , "Cyan": "0,255,255"
        , "Magenta": "255,0,255"
        , "Gray": "128,128,128"
        , "Silver": "192,192,192"
        , "Maroon": "128,0,0"
        , "Olive": "128,128,0"
        , "Navy": "0,0,128"
        , "Purple": "128,0,128"
        , "Teal": "0,128,128"
        , "Lime": "0,255,0"
        , "Aqua": "0,255,255"
        , "Fuchsia": "255,0,255"
        , "SkyBlue": "135,206,235"
		, "Pink": "255,192,203"
        , "Orange": "255,165,0"	}
    if (colorMap.HasKey(colorName)) {
		cName := colorMap[colorName]
		StringSplit, s, cName , `,
		colorCode :=  s3  * 65536 + s2  * 256 +  s1
		return colorCode
	}
    else {
        return (-1)
	}
}
오토핫키도 Object를 지원하기 때문에
이런 식으로 키와 값을 가진 Dictionary 타입의 데이터형식으로 만들 수 있습니다.
4. 폰트 색상뿐만 아니라 형광펜색 즉 하이라이트 색상도 빠르게 변경할 수 있게 해보았습니다.
SetHighlight( myColor ) {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange2
		}
		catch e {
			MsgBox Select a text range first.
		}
		tr.Font.Highlight.RGB := getRGBFrom( myColor )
		ppt := ""
	}
}
5. 하이라이트 색상을 지정했다가 없애는 것은 난관이 있었습니다.
'작동하지 않음
'ActiveWindow.View.Selection.TextRange2.Font.Hightlight.RGB = -1
'하이라이트 색상을 칠하거나 없애는 명령
CommandBars.ExecuteMSO "TextHighlightColorPickerLicensed"
위 명령을 수행하면 현재 선택된 형광펜 색상과 같은 경우에는 형광펜 하이라이트가 사라지지만
다른 경우에는 새로운 색상으로 칠해지게 됩니다.
그래서 Alt+H, TC, N 단축키를 보내는 것으로 우회하였습니다.
HighlightOff() {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange2
		}
		catch e {
			MsgBox Select a text range first.
		}
		Send,{Alt down}
		Sleep,150
		Send,"htcn"
		Send,{Alt up}
	}
}
전체 오토핫키 코드
#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
MsgBox, 0x40, ChangeFontColor, Press the <Ctrl+Num> key to change the font color of the selected text range,
<Ctrl+Shift+Num> key to highlight the text background or <Win+x> to quit.
	Menu, Tray, Icon,, %A_ScriptFullPath%, 1
	Menu, Tray, Tip, [Ctrl+Num] to Change the Font color`, [Ctrl+Shift+Num] for Highlight or [Win+X]: Exit
	Menu, Tray, NoStandard
	Menu, Tray, Add, Exit, ExitMenu
	return
#IfWinActive, ahk_exe POWERPNT.EXE
$^`::
	SetFontColor("White")
	return
$^1::
	SetFontColor( "Red" )
	return
$^2::
	SetFontColor( "Green" )
	return
$^3::
	SetFontColor( "Blue" )
	return
$^4::
	SetFontColor( "Yellow" )
	return
$^5::
	SetFontColor( "Purple" )
	return
$^6::
	SetFontColor( "Lime" )
	return
$^7::
	SetFontColor( "Navy" )
	return
$^8::
	SetFontRGB( 127, 127, 127 )
	return
$^9::
	SetFontColor( "Orange" )
	return
$^0::
	SetFontColor( "Black" )
	return
^+1::
	SetHighlight( "Yellow" )
	return
^+2::
	SetHighlight( "Lime" )
	return
^+3::
	SetHighlight( "Cyan" )
	return
^+4::
	SetHighlight( "Pink" )
	return
^+5::
	SetHighlight( "Blue" )
	return
^+6::
	SetHighlight( "Red" )
	return
^+7::
	SetHighlight( "Navy" )
	return
^+8::
	SetHighlight( "Green" )
	return
^+9::
	SetHighlight( "Silver" )
	return
^+0::
	SetHighlight( "Purple" )
	return
^+-::
	HighlightOff()
	return
#IF
ExitMenu:
#x::
	ExitApp
HighlightOff() {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange2
		}
		catch e {
			MsgBox Select a text range first.
		}
		Send,{Alt down}
		Sleep,150
		Send,"htcn"
		Send,{Alt up}
		;if(tr.Font.Hightlight.Type) {
		;	ppt.CommandBars.ExecuteMSO "TextHighlightColorPickerLicensed"
		;}
		ppt := ""
	}
}
SetHighlight( myColor ) {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange2
		}
		catch e {
			MsgBox Select a text range first.
		}
		tr.Font.Highlight.RGB := getRGBFrom( myColor )
		ppt := ""
	}
}
SetFontColor( myColor ) {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange
		}
		catch e {
			MsgBox Select a text range first.
		}
		tr.Font.Color.RGB := getRGBFrom( myColor )
		ppt := ""
	}
}
SetFontRGB( R, G, B ) {
  	ppt:=ComObjActive("PowerPoint.Application")
	ppt.Visible := True
	IfWinActive, ahk_exe POWERPNT.EXE
	{
		try {
			tr := ppt.ActiveWindow.Selection.TextRange
		}
		catch e {
			MsgBox Select a text range first.
		}
		tr.Font.Color.RGB := B * 65536 + G * 256 + R
		ppt := ""
	}
}
getRGBFrom(colorName) {
    colorMap := { "Black": "0,0,0"
		, "White": "255,255,255"
		, "Red": "255,0,0"
		, "Green": "0,255,0"
		, "Blue": "0,0,255"
		, "Yellow": "255,255,0"
        , "Cyan": "0,255,255"
        , "Magenta": "255,0,255"
        , "Gray": "128,128,128"
        , "Silver": "192,192,192"
        , "Maroon": "128,0,0"
        , "Olive": "128,128,0"
        , "Navy": "0,0,128"
        , "Purple": "128,0,128"
        , "Teal": "0,128,128"
        , "Lime": "0,255,0"
        , "Aqua": "0,255,255"
        , "Fuchsia": "255,0,255"
        , "SkyBlue": "135,206,235"
		, "Pink": "255,192,203"
        , "Orange": "255,165,0"	}
    if (colorMap.HasKey(colorName)) {
		cName := colorMap[colorName]
		StringSplit, s, cName , `,
		colorCode :=  s3  * 65536 + s2  * 256 +  s1
		return colorCode
	}
    else {
        return (-1)
	}
}
A. 실행 대화상자

B. 실행화면 캡쳐
| Ctrl~1~Ctrl+0 : 폰트 색상 Ctrl+` : 흰색 Ctrl+Shift+1 ~ Ctrl+Shift+0 : 하이라이트 색상 Ctrl+Shift+- : 하이라이트 없애기 | 

C. 소스와 실행파일
D.기타 아이디어
특정색상이 아니라 테마 강조색을 순서대로 적용하게 하는 것도 좋겠습니다.
관련: 지식인
'AutoHotKey' 카테고리의 다른 글
| 엑셀 페이지방향 바꾸는 단축키 (0) | 2025.04.12 | 
|---|---|
| 대화창이나 윈도우가 화면을 벗어났을 때 창위치 드래그 (0) | 2024.11.25 | 
| PPT편집창에서 더블클릭으로 링크된 파일 실행하기 (1) | 2024.07.15 | 
| 파워포인트 표 셀 이동 빠른 단축키 (0) | 2024.05.05 | 
| 발표자 보기 도구 창에서 '여러 슬라이드 보기' 유지하기 (0) | 2023.11.22 | 
| 파워포인트 원하는 슬라이드로 바로 이동하기 (0) | 2023.07.02 | 
| 파워포인트 다른 그림으로 바꾸기, 2가지 방법 (0) | 2023.07.02 | 
| 표(셀)에 맞게 사진 삽입하기 (2) | 2023.03.16 | 
 
                                     
                                     
                                     
                                     
                                    
최근댓글