i managed to create my first ever program which makes me really happy, thank you AHK <3
Functioning application that takes all Word supported files that are dragged and dropped to it and save them as PDF into the same folder they came from.
Doct_to_PDF_v0.3.ahk
changes:
- - added progress bar and text
- fixed word process not closing
- used better optimization from https://autohotkey.com/boards/viewtopic.php?f=6&t=6413
- changed saving method to use web publish quality settings (SaveAs2 to ExportAsFixedFormat)
- changed Text language from Arabic to English
- - settings window to add default path options
- support for visio files
Code: Select all
#NoEnv
#MaxHotkeysPerInterval 99000000
#HotkeyInterval 99000000
#KeyHistory 0
#SingleInstance force
ListLines Off
Process, Priority, , A
SetBatchLines, -1
SetKeyDelay, -1, -1
SetMouseDelay, -1
SetDefaultMouseSpeed, 0
SetWinDelay, -1
SetControlDelay, -1
SendMode Input
SetWorkingDir %A_ScriptDir%
;=======================Main_ui_window======================
Gui Font, s13 q5, Times New Roman
Gui Add, Text, x80 y125 w320 h30 +0x200 Center vText_DD, Please drag and drop the word files into this window.
Gui Font
Gui Show, w480 h280, Window
Gui Add, Progress, x115 y130 w250 h20 Range0-%Count_Files% -Smooth vConvertProgress, 0
Gui Font, s16 q5, Times New Roman
Gui Add, Text, x80 y50 w320 h30 +0x200 Center vText_Progress, 0`%
Gui Font
GuiControl, hide, ConvertProgress
GuiControl, hide, Text_Progress
Return
GuiEscape:
GuiClose:
oWord:=""
ExitApp
;===================End_of_the_GUI_section==================
;=========================Functions=========================
;==================Word_converter_function==================
wd_PDFConverter(InputFile)
{
SplitPath, InputFile,, OutDir,, OutNameNoExt
NewFile := OutDir . "\" . OutNameNoExt . ".pdf"
oWord.Documents.Open(InputFile,,,,,,,,,,, 0) ;
oWord.Documents(InputFile).Activate
oWord.ActiveDocument.ExportAsFixedFormat(NewFile, 17, 0, 0)
oWord.ActiveDocument.Close(0)
GuiControl,, ConvertProgress, +1
}
;============================End============================
;=====================Has_Value_function====================
HasVal(haystack, needle)
{
for index, value in haystack
if (value = needle)
return True
if !(IsObject(haystack))
throw Exception("Bad haystack!", -1, haystack)
return 0
}
;============================End============================
;=====================Drag&Drop_function====================
GuiDropFiles:
global oWord := ComObjCreate("Word.Application") ;create MS Word Application object
Convert_list := Object() ;Array initialize
Word_Supported := ["doc", "docm", "docx", "docx", "dot", "dotm", "dotx", "html", "htm", "mht", "mhtml", "odt", "rtf", "txt", "wps", "xml", "xps"]
Loop, parse, A_GuiEvent, `n ;parse through dropped file names with new Line as delimiter
{ ;SplitPath, InputVar [, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive]
SplitPath, A_LoopField,,, ext ;get file extenstion for compability check
if HasVal(Word_Supported, ext)
Convert_list.InsertAt(Convert_list.Length() + 1, A_LoopField) ;insert each File into the "items to convert" array
}
Count_Files := Convert_list.Length()
Progress_Portion := Round(100/Count_Files)
if Count_Files >= 1 ;if there are word supported files, trigger the conversion funciton
GuiControl, Hide, Text_DD ;GUI feedback control
GuiControl, Show, ConvertProgress ;GUI feedback control
GuiControl, Show, Text_Progress ;GUI feedback control
GuiControl, +Range0-%Count_Files%, ConvertProgress ;GUI feedback control
Loop % Count_Files {
wd_PDFConverter(Convert_list[A_Index])
GuiControl,, Text_Progress, %Progress_Portion%`%
Progress_Portion += %Progress_Portion%
}
oWord.Quit(0) ;killing Word process
GuiControl,, ConvertProgress, 100 ;GUI feedback control
GuiControl,, Text_Progress, 100`% ;GUI feedback control
oWord:="" ;releasing COM object, mandatory to kill Word process
sleep 700 ;GUI feedback control
GuiControl, hide, ConvertProgress ;GUI feedback control
GuiControl, hide, Text_Progress ;GUI feedback control
GuiControl, Show, Text_DD ;GUI feedback control
return
;============================End============================
changelog:
Spoiler