ahk设置WPS办公自动化热键脚本(word/excel类似)

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

td3zzz
Posts: 18
Joined: 15 Aug 2014, 20:53

ahk设置WPS办公自动化热键脚本(word/excel类似)

17 Jan 2021, 05:40

由于众所周知的原因,近年来越来越多需要从MS office转向WPS办公,以前用autohotkey写的热键脚本也需要相应转换。为了减少一些弯路,现将个人自用的部分WPS热键脚本放上来,供大家修改参考。主要代码均是由word/excel转换过来的,大多均可通用。只是N久以前由于稳定性原因,excel没有使用ComObjActive内置命令,而是通过Excel_Get()函数实现,也不知道现在有没有改进。

主要功能就是通过ahk设置一些常用的功能组合热键,适用于日常办公,尤其是公文写作。具体包括:
WPS文字:
F1-3:设置一级、二级、三级标题,符合正式公文要求,含大纲级别,适合文档结构图,下同。
F4:设置大标题,即题目。
F7:设置正文格式。
F8:打印。建议可以另行添加代码根据具体打印机情况设置双面打印。
F10:行距28。
F11:查找当前值。

WPS表格:
F3:自动筛选。
F5:设置边框。
F6:设置两位小数。
F7:设置文本格式。
F8:设置百分数(两位小数)。
F9:设置首行标题格式。
等等
具体功能及相关代码可自行再修改。

Code: Select all

#IfWinActive ahk_class OpusApp ahk_exe wps.exe  ;{……………………WPS文字。加ahk_exe是为了与word区分,下同。
f1::  ;一级标题
	word := ComObjActive("kwps.Application")
	if word.selection.type =1  ;当前Word中未选择具体内容,为光标闪烁状态
	{
		word.Selection.MoveRight  ;向右移动1格光标
		word.Selection.MoveUp(unit := 4)  ;移动光标到段首(如果当前光标本来就在段首,那么会移动到上一段段首,故需要增加MoveRight代码,下同)
		word.Selection.MoveDown(unit := 4,,1)   ;选中光标到段尾的内容			
	}
	word.Selection.ClearFormatting  ;清除格式
	word.Selection.Range.HighlightColorIndex := 0  ;背景色突出显示,不能通过清除格式去掉(注意不是底纹)
	word.Selection.ParagraphFormat.CharacterUnitLeftIndent := 0    ;左缩进、右缩进、首行缩进(注意:必须中文设置在前,英文设置在后)
	word.Selection.ParagraphFormat.CharacterUnitRightIndent := 0
	word.Selection.ParagraphFormat.CharacterUnitFirstLineIndent := 2
	word.Selection.ParagraphFormat.LeftIndent := 0
	word.Selection.ParagraphFormat.RightIndent := 0
	word.Selection.ParagraphFormat.FirstLineIndent := 0
	word.Selection.Font.Name := "黑体"
	word.Selection.Font.Size := 16  ;三号
	word.Selection.ParagraphFormat.OutlineLevel := 1    ;大纲级别
	word.Selection.ParagraphFormat.LineSpacingRule := 4     ;行距规则,单倍行距多倍行距等,依据菜单项从0到5
	word.Selection.ParagraphFormat.LineSpacing := 28    ;行距具体值
	MouseMove, 2, 2 , , R      ;wps需要移动鼠标才会刷新,下同,word中不需要。
	word :=""
Return
f2::  ;(一)级标题
	word := ComObjActive("kwps.Application")
	if word.selection.type =1  ;当前Word中未选择具体内容,为光标闪烁状态
	{
		word.Selection.MoveRight  ;向右移动1格光标
		word.Selection.MoveUp(unit := 4)  ;移动光标到段首
		word.Selection.MoveDown(unit := 4,,1)   ;选中光标到段尾的内容
	}
	word.Selection.ClearFormatting  ;清除格式
	word.Selection.Range.HighlightColorIndex := 0  ;背景色突出显示,不能通过清除格式去掉(注意不是底纹)
	word.Selection.ParagraphFormat.CharacterUnitLeftIndent := 0    ;左缩进、右缩进、首行缩进(注意:必须中文设置在前,英文设置在后)
	word.Selection.ParagraphFormat.CharacterUnitRightIndent := 0
	word.Selection.ParagraphFormat.CharacterUnitFirstLineIndent := 2
	word.Selection.ParagraphFormat.LeftIndent := 0
	word.Selection.ParagraphFormat.RightIndent := 0
	word.Selection.ParagraphFormat.FirstLineIndent := 0
	word.Selection.Font.Name := "楷体_GB2312"
	word.Selection.Font.Bold :=True
	word.Selection.Font.Size := 16  ;三号
	word.Selection.ParagraphFormat.OutlineLevel := 2    ;大纲级别
	word.Selection.ParagraphFormat.LineSpacingRule := 4     ;行距规则,单倍行距多倍行距等,依据菜单项从0到5
	word.Selection.ParagraphFormat.LineSpacing := 28    ;行距具体值
	MouseMove, 2, 2 , , R
	word :=""
Return
f3::  ;1级标题
	word := ComObjActive("kwps.Application")
	if word.selection.type =1  ;当前Word中未选择具体内容,为光标闪烁状态
	{
		word.Selection.MoveRight  ;向右移动1格光标
		word.Selection.MoveUp(unit := 4)  ;移动光标到段首
		word.Selection.MoveDown(unit := 4,,1)   ;选中光标到段尾的内容
	}
	word.Selection.ClearFormatting  ;清除格式
	word.Selection.Range.HighlightColorIndex := 0  ;背景色突出显示,不能通过清除格式去掉(注意不是底纹)
	word.Selection.ParagraphFormat.CharacterUnitLeftIndent := 0    ;左缩进、右缩进、首行缩进(注意:必须中文设置在前,英文设置在后)
	word.Selection.ParagraphFormat.CharacterUnitRightIndent := 0
	word.Selection.ParagraphFormat.CharacterUnitFirstLineIndent := 2
	word.Selection.ParagraphFormat.LeftIndent := 0
	word.Selection.ParagraphFormat.RightIndent := 0
	word.Selection.ParagraphFormat.FirstLineIndent := 0
	word.Selection.Font.Name := "仿宋_GB2312"
	word.Selection.Font.Bold :=True
	word.Selection.Font.Size := 16  ;三号
	word.Selection.ParagraphFormat.OutlineLevel := 3    ;大纲级别
	word.Selection.ParagraphFormat.LineSpacingRule := 4     ;行距规则,单倍行距多倍行距等,依据菜单项从0到5
	word.Selection.ParagraphFormat.LineSpacing := 28    ;行距具体值
	MouseMove, 2, 2 , , R
	word :=""
Return
f4::  ;题目,大标题
	word := ComObjActive("kwps.Application")
	if word.selection.type =1  ;当前Word中未选择具体内容,为光标闪烁状态
	{
		word.Selection.MoveRight  ;向右移动1格光标
		word.Selection.MoveUp(unit := 4)  ;移动光标到段首
		word.Selection.MoveDown(unit := 4,,1)   ;选中光标到段尾的内容
	}
	word.Selection.ClearFormatting  ;清除格式
	word.Selection.Range.HighlightColorIndex := 0  ;背景色突出显示,不能通过清除格式去掉(注意不是底纹)
	word.Selection.ParagraphFormat.CharacterUnitLeftIndent := 0    ;左缩进、右缩进、首行缩进(注意:必须中文设置在前,英文设置在后)
	word.Selection.ParagraphFormat.CharacterUnitRightIndent := 0
	word.Selection.ParagraphFormat.CharacterUnitFirstLineIndent := 0
	word.Selection.ParagraphFormat.LeftIndent := 0
	word.Selection.ParagraphFormat.RightIndent := 0
	word.Selection.ParagraphFormat.FirstLineIndent := 0
	word.Selection.Font.Name := "方正小标宋简体"
	word.Selection.Font.Size := 22  ;二号
	word.Selection.ParagraphFormat.Alignment := 1   ;0左对齐,1居中,2右对齐,3两端对齐,4分散对齐
	word.Selection.ParagraphFormat.LineSpacingRule := 4     ;行距规则,单倍行距多倍行距等,依据菜单项从0到5
	word.Selection.ParagraphFormat.LineSpacing := 28    ;行距具体值
	MouseMove, 2, 2 , , R
	word :=""
Return
f7::  ;正文
	word := ComObjActive("kwps.Application")
	word.Selection.ClearFormatting  ;清除格式
	word.Selection.Range.HighlightColorIndex := 0  ;背景色突出显示,不能通过清除格式去掉(注意不是底纹)
	word.Selection.ParagraphFormat.CharacterUnitLeftIndent := 0    ;左缩进、右缩进、首行缩进(注意:必须中文设置在前,英文设置在后)
	word.Selection.ParagraphFormat.CharacterUnitRightIndent := 0
	word.Selection.ParagraphFormat.CharacterUnitFirstLineIndent := 2
	word.Selection.ParagraphFormat.LeftIndent := 0
	word.Selection.ParagraphFormat.RightIndent := 0
	word.Selection.ParagraphFormat.FirstLineIndent := 0
	word.Selection.Font.Name := "仿宋_GB2312"
	word.Selection.Font.Size := 16  ;三号
	word.Selection.ParagraphFormat.LineSpacingRule := 4     ;行距规则,单倍行距多倍行距等,依据菜单项从0到5
	word.Selection.ParagraphFormat.LineSpacing := 28    ;行距具体值
	MouseMove, 2, 2 , , R
	word :=""
Return
f8:: ;打印
	Send,^p
return
f9::  ;加粗,加底色。表格标题等
	word := ComObjActive("kwps.Application")
	word.Selection.Font.Bold :=True
	word.Selection.Cells.Shading.BackgroundPatternColor  := 0xC3D9DD   ;为RGB颜色的16进制数值(也可以为纯数字),可以通过Window Spy 获取
	word.Selection.range.Shading.BackgroundPatternColor  := 0xC3D9DD   ;为RGB颜色的16进制数值(也可以为纯数字),可以通过Window Spy 获取
	word :=""
	MouseMove, 2, 2 , , R
Return
f10::  ;行距
	word := ComObjActive("kwps.Application")
	word.Selection.ParagraphFormat.LineSpacingRule := 4     ;行距规则,单倍行距多倍行距等,依据菜单项从0到5
	word.Selection.ParagraphFormat.LineSpacing := 28    ;行距具体值
	word :=""
	MouseMove, 2, 2 , , R
Return
f11::  ;查找当前值
	word := ComObjActive("kwps.Application")
	word.Selection.Find.Execute(FindText:=word.Selection, Wrap := 1)  ;Wrap参数作用为到文档结尾后继续查找,但实际无效,原因未明。
	word :=""
Return

a & h::  ;仅粘贴文本
	word := ComObjActive("kwps.Application")
	word.Selection.PasteAndFormat(22)
	word :=""
Return

<!j::send,^{left}
<!l::send,^{right}
<!m::send,^+{right}
<!n::send,^+{left}
^+n::send,+{home}
^+m::send,+{end}
^+,::send,^+{Home}
^+.::send,^+{end}
!+j::send,^+{left}
!+l::send,^+{right}
;}

#IfWinActive ahk_class XLMAIN ahk_exe et.exe    ;{…………………………WPS表格
f3:: send ^+l   ;筛选
f4::     ;底色,突出显示
	ket := ComObjActive("Ket.Application")
	if OnOff := !OnOff
	{
		ket.Selection.Interior.Colorindex := 6 
	}
	else
	{
		ket.Selection.Interior.Colorindex :=  0
	}
	ket :=""
return
f5:: ;设置边框
	ket := ComObjActive("Ket.Application")
	if OnOff := !OnOff
	{
		ket.Selection.borders.linestyle := 1
	}
	else
	{
		ket.Selection.borders.linestyle := 0
	}
	ket :=""
return
f6:: ;设置两位小数数字格式
	ket := ComObjActive("Ket.Application")
	ket.Selection.NumberFormatLocal := "0.00_ "
	ket.Selection.HorizontalAlignment := 1   ;按数据类型对齐
	ket :=""
return
f7:: ;设置文本格式
	ket := ComObjActive("Ket.Application")
	ket.Selection.NumberFormatLocal := "@"
	ket.Selection.HorizontalAlignment := 1
	ket :=""
return
f8:: ;设置百分数(两位小数)格式
	ket := ComObjActive("Ket.Application")
	ket.Selection.NumberFormatLocal := "0.00%"
	ket.Selection.HorizontalAlignment := 1
	ket :=""
return
f9:: ;设置首行、尾行格式,底纹等
	ket := ComObjActive("Ket.Application")
	ket.Selection.Interior.Color := 0xC3D9DD  ;为RGB颜色的16进制数值(也可以为纯数字),可以通过Window Spy 获取
	ket.Selection.Font.Bold := "True"
	ket.Selection.HorizontalAlignment := -4108   ;居中
	ket :=""
Return
a & h::
	Send,{Alt}
	Sleep, 100
	Send, e
	Sleep, 100
	Send,s
	WinWaitActive,选择性粘贴,,3
	if ErrorLevel
		Return
	Sleep,100
	Send,v
return
!d::   ;冻结窗格
	Sleep, 100
	Send,{Alt}
	Sleep, 100
	Send, w
	Sleep, 100
	Send,f
	Sleep, 100
	Send, f
return
;}

td3zzz
Posts: 18
Joined: 15 Aug 2014, 20:53

Re: ahk设置WPS办公自动化热键脚本(word/excel类似)

19 Jan 2021, 07:48

补充一下使用环境:
操作系统:win7
WPS版本:2019个人版、2019专业版(使用以前版本可能class不同,需要自行修改)
ahk版本:v1.1.32.00,v1.1.33.02等均可
loong
Posts: 2
Joined: 08 Feb 2021, 21:58

Re: ahk设置WPS办公自动化热键脚本(word/excel类似)

08 Feb 2021, 22:04

目测楼主体制内。建议试用插件“小恐龙公文助手”,页面、字体设置也很方便。

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 15 guests