-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathExcelSetPrint.ahk
318 lines (264 loc) · 7.08 KB
/
ExcelSetPrint.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance,off
SetBatchLines, -1
coordMode,mouse,screen
coordMode,pixel,screen
coordMode,tooltip,screen
SetTitleMatchMode,2
SetControlDelay,-1
#include <Acc>
;~ #include <Printer>
;~ printerStr:=GetInstalledPrinters()
;~ defaultPrinter:=GetDefaultPrinter()
;~ str=
;~ loop,parse,printerStr,|
;~ {
;~ if !a_loopfield
;~ continue
;~ str.=a_loopfield
;~ str.=(defaultPrinter=a_loopfield)?"||":"|"
;~ }
updateInterval=500
oExcel:=excel_get()
ComObjError(false)
gui +lastfound +AlwaysOnTop
guiHwnd:=WinExist()
gui,font,s8,Verdana
;~ gui,add,DropDownList,vprinter w300,% str
gui,add,text,,direction
gui,add,radio,yp xp+100 gchange,Straight
gui,add,radio,yp xp+50 gchange,Landscape
;~ gui,add,checkbox,yp xp+60 vlockorientation,Lock
gui,add,text,xs,straight
gui,add,dropdownlist,vpagewidth yp xp+100 w50 gchange,1|2|3|4|5|6|7|8|9|0
;~ gui,add,checkbox,yp xp+60 vlockpagewidth,Lock
gui,add,text,xs,Landscape
gui,add,dropdownlist,vpageheight yp xp+100 w50 gchange,1|2|3|4|5|6|7|8|9|0
;~ gui,add,checkbox,yp xp+60 vlockpageheight,Lock
gui,add,button,gA4 xs w90,A4
gui,add,button,gprintPreview yp xp+100 w90,orientation
gui,add,button,gprint xs w90,Print
gui,add,button,gprintAll yp xp+100 w90,All India
gui,add,radio,xs,Single-sided printing
gui,add,radio,yp xp+100 checked,Double-sided printing
gui,add,button,gprior xs w90,<
gui,add,button,gnext yp xp+100 w90,>
gui,show,w250 h200,Excel Print Setting
setTimer,update,% updateInterval
return
change:
gui,submit,nohide
if excelStatus() {
oExcel:=Excel_Get()
if (a_guicontrol="straight")
oExcel.activeWorkBook.activeSheet.pageSetup.orientation:=1
else if (a_guicontrol="Landscape")
oExcel.activeWorkBook.activeSheet.pageSetup.orientation:=2
else if (a_guicontrol="pagewidth" || a_guicontrol="pageheight")
oExcel.activeWorkBook.activeSheet.pageSetup.Zoom:=comobj(0xB,0)
,oExcel.activeWorkBook.activeSheet.pageSetup.FitToPagesWide:=pagewidth?comobj(3,pagewidth):comobj(0xB,0)
,oExcel.activeWorkBook.activeSheet.pageSetup.FitToPagesTall:=pageheight?comobj(3,pageheight):comobj(0xB,0)
;~ else if (a_guicontrol="pageheight")
;~ oExcel.activeWorkBook.activeSheet.pageSetup.Zoom:=comobj(0xB,0)
;~ ,oExcel.activeWorkBook.activeSheet.pageSetup.FitToPagesTall:=pageheight?comobj(3,pageheight):comobj(0xB,0)
}else{
;~ winget,list,controllisthwnd,ahk_class XLMAIN
;~ loop,parse,list,`n
;~ {
;~ if (acc_objectfromWindow(a_loopfield).accName="版面設定")
;~ controlclick,,ahk_id %a_loopfield%
;~ ,break
;~ }
winactivate,ahk_class XLMAIN
winwaitactive,ahk_class XLMAIN,,5
if errorlevel
return
send !psp
winwait,Layout setting ahk_class bosa_sdm_XL9,,5
if errorlevel
return
if (a_guicontrol="直向")
send {tab}t
else if (a_guicontrol="橫向")
send {tab}l
else if (a_guicontrol="pagewidth")
send {tab}f%pagewidth%
else if (a_guicontrol="pageheight")
send {tab}f{tab}%pageheight%
send {enter}
}
return
A4:
if excelstatus() {
oExcel.activeWorkBook.activeSheet.pageSetup.paperSize:=9
guicontrol,disable,A4
}else {
winactivate,ahk_class XLMAIN
winwaitactive,ahk_class XLMAIN,,5
if errorlevel
return
send !psp
winwait,版面設定 ahk_class bosa_sdm_XL9,,5
if errorlevel
return
send {tab}z
}
return
printPreview:
winactivate,ahk_class XLMAIN
winwaitactive,ahk_class XLMAIN,,5
if errorlevel
return
if excelStatus() {
;~ oExcel.activeWorkBook.activeSheet.printOut(comobjmissing(),comobjmissing(),comobjmissing(),comobj(0xB,-1))
send ^{F2}
}else{
send {esc}
}
return
print:
winactivate,ahk_class XLMAIN
winwaitactive,ahk_class XLMAIN,,5
if errorlevel
return
if excelStatus() {
send ^p
}else{
send !pp
}
winwait,列印 ahk_class bosa_sdm_XL9,,5
if errorlevel
return
gosub setDuplexPrint
return
printAll:
Msgbox,4,Print ALL !!,Are you sure?
IfMsgBox,No
return
oExcel:=Excel_Get()
loop % (sh:=oExcel.activeWorkBook.sheets).count {
winactivate,ahk_class XLMAIN
sh.item(a_index).select()
winwaitactive,ahk_class XLMAIN,,5
if errorlevel
return
send ^p
winwait,列印 ahk_class bosa_sdm_XL9,,5
if errorlevel
return
gosub setDuplexPrint
send {enter}
}
return
setDuplexPrint:
guicontrolget,duplexPrint,,雙面列印
send {tab}r
winwait,Properties #32770,,5
if errorlevel
return
controlget,currentDuplexSetting,checked,,Button25,Properties #32770
if (duplexPrint^currentDuplexSetting)
controlclick,Button25,Properties #32770
controlclick,Button38,Properties #32770
return
prior:
if !oExcel:=Excel_Get()
return
winactivate,ahk_class XLMAIN
if (sheetIndex:=oExcel.activeWorkBook.activeSheet.index)=1
oExcel.activeWorkBook.sheets.item(oExcel.activeWorkBook.sheets.count).activate
else
oExcel.activeWorkBook.sheets.item(sheetIndex-1).activate
return
next:
if !oExcel:=Excel_Get()
return
winactivate,ahk_class XLMAIN
if (sheetIndex:=oExcel.activeWorkBook.activeSheet.index)=oExcel.activeWorkBook.sheets.count
oExcel.activeWorkBook.sheets.item(1).activate
else
oExcel.activeWorkBook.sheets.item(sheetIndex+1).activate
return
update:
priorActiveW:=nowActiveW
winget,nowActiveW,id,A
if(priorActiveW!=nowActiveW)
if(nowActiveW=guiHwnd)
winset,Transparent,off,ahk_id %guiHwnd%
else
winset,Transparent,150,ahk_id %guiHwnd%
;~ oExcel:=ComObjActive("Excel.Application")
if !excelStatus()
return
if !oExcel:=Excel_Get()
return
if !((nowWb:=oExcel.activeWorkBook.name)=priorWb && (nowSh:=oExcel.activeWorkBook.activeSheet.name)=priorSh)
;~ return
;~ else
priorWb:=nowWb,priorSh:=nowSh
if ((pageSetup:=oExcel.activeWorkBook.activeSheet.pageSetup).orientation)
guicontrol,,直向,1
else
guicontrol,,橫向,1
if !(PageSetup.Zoom) {
guicontrol,choosestring,pageWidth,% pageSetup.FitToPagesWide
guicontrol,choosestring,pageHeight,% pageSetup.FitToPagesTall
}else {
guicontrol,choose,pageWidth,0
guicontrol,choose,pageHeight,0
}
if (pageSetup.paperSize=9)
guicontrol,disable,A4
else
guicontrol,enable,A4
return
Excel_Get(WinTitle="ahk_class XLMAIN") { ; by Sean and Jethrow http://www.autohotkey.com/forum/viewtopic.php?p=492566#492566
global excelHwnd
ControlGet, hwnd, hwnd, , Excel71, %WinTitle%
Window := Acc_ObjectFromWindow(hwnd, -16)
Loop
try
Application := Window.Application
catch {
ControlSend, Excel71, {esc}, %WinTitle%
controlsend, ,{esc},ahk_class bosa_sdm_XL9
}
Until !!Application || a_index>10
winget,excelHwnd,id,%wintitle%
return Application
}
; 0 for print preview mode, 1 for others
; if excel is not accessible (eg. edit mode of cell), send tab and shift+tab
ExcelStatus(WinTitle="ahk_class XLMAIN"){
ControlGet, hwnd, hwnd, , ExcelB1, %WinTitle%
ret:=hwnd?0:1
if (ret) {
controlget,hwnd,hwnd,,Excel71,%wintitle%
try
app:=Acc_ObjectFromWindow(hwnd, -16).Application
catch
controlsend,,{tab}+{tab},ahk_id %hwnd%
}
return ret
}
#ifwinactive,Excel Print Setting ahk_class AutoHotkeyGUI
~^F2::
gosub printPreview
return
~^p::
gosub print
return
~left::
gosub prior
return
~right::
gosub next
return
#ifwinactive
guiclose:
;~ ~$esc::
;~ winactivate,SciTE
exitapp
return