Automatically Set Excel Font type, size, emphasis with AutoHotkey
Excel files are a go-to standard form many business people. Often people will export a file from their “tool” and then spend time making it “pretty” by adjusting font size, type, cell color, formatting, etc. When I do this, I automate the re-doing of applying formatting over and over which greatly streamlines the process and makes things more consistent.
Tutorial how to use the below AutoHotkey functions
The following AutoHotkey functions are demonstrated in the below above video. They make it a breeze to update settings in a given Excel range.
AutoHotkey functions to Set Excel Font type, Size and format
RControl::
Browser_Back::
;************************************************************
XL_Handle(XL,1) ;get handle to current Excel file
;~ XL_Format_Font(XL,RG:="A1:B1",Font:="Book Antiqua",Size:=20) ;Arial, Arial Narrow, Calibri,Book Antiqua
XL_Format_Format(XL,RG:="A1:B1",Bold:=0,Italic:=0,Underline:=1) ;Underline 1 thru 4
;***********************set size, type, ********************************.
XL_Format_Font(PXL,RG="",Font="Arial",Size="11"){
PXL.Application.ActiveSheet.Range(RG).Font.Name:=Font
PXL.Application.ActiveSheet.Range(RG).Font.Size:=Size
}
;***********************bold, normal, italic, Underline********************************.
XL_Format_Format(PXL,RG="",Bold=0,Italic=0,Underline=0){
PXL.Application.ActiveSheet.Range(RG).Font.Bold:= bold
PXL.Application.ActiveSheet.Range(RG).Font.Italic:=Italic
(Underline="0")?(PXL.Application.ActiveSheet.Range(RG).Font.Underline:=-4142):(PXL.Application.ActiveSheet.Range(RG).Font.Underline:=Underline+1)
}
;*******************************************************.
XL_Handle(ByRef PXL,Sel){
ControlGet, hwnd, hwnd, , Excel71, ahk_class XLMAIN ;identify the hwnd for Excel
IfEqual,Sel,1, Return, PXL:= ObjectFromWindow(hwnd,-16).application ;Handle to Excel Application
IfEqual,Sel,2, Return, PXL:= ObjectFromWindow(hwnd,-16).parent ;Handlle to active Workbook
IfEqual,Sel,3, Return, PXL:= ObjectFromWindow(hwnd,-16).activesheet ;Handle to Active Worksheet
}
;***********adapted from ACC.ahk*******************
ObjectFromWindow(hWnd, idObject = -4){
(if Not h)?h:=DllCall("LoadLibrary","Str","oleacc","Ptr")
If DllCall("oleacc\AccessibleObjectFromWindow","Ptr",hWnd,"UInt",idObject&=0xFFFFFFFF,"Ptr",-VarSetCapacity(IID,16)+NumPut(idObject==0xFFFFFFF0?0x46000000000000C0:0x719B3800AA000C81,NumPut(idObject==0xFFFFFFF0?0x0000000000020400:0x11CF3C3D618736E0,IID,"Int64"),"Int64"), "Ptr*", pacc)=0
Return ComObjEnwrap(9,pacc,1)
}
RAlt::
Browser_Forward::Reload

