Doc_to_PDF script

Post your working scripts, libraries and tools for AHK v1.1 and older
mandus
Posts: 14
Joined: 27 Oct 2016, 01:33
Contact:

Doc_to_PDF script

22 Nov 2016, 05:07

Hi,

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
future plans:
  • - 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============================
here is my Github page

changelog:
Spoiler
Last edited by mandus on 25 Nov 2016, 12:57, edited 5 times in total.
mandus
Posts: 14
Joined: 27 Oct 2016, 01:33
Contact:

Re: My first application :), Doc_to_PDF

23 Nov 2016, 02:10

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
mandus
Posts: 14
Joined: 27 Oct 2016, 01:33
Contact:

Re: Doc_to_PDF script

25 Nov 2016, 12:58

anyone knows how to get the process ID for a process that is started from a ComObj?

in this example i open a document in word, i want to know the process id

Code: Select all

;==================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
}
RicKami
Posts: 29
Joined: 08 Oct 2020, 06:55

Re: Doc_to_PDF script

26 Feb 2022, 14:25

Hi, I tried your script but I get an error saying "The export failed because this feature is not installed."
I locked for an answer and the only thing I found is an answer to this same question stating: "I am pretty sure you have office 2007, have you got add-in installed?"
I do have WORD 2007 but I have no idea what "add-in" means... the further reply simply says "ah, that was easy, thank you" but no further data on HOW is this actually solved. :-(
Anyone has any idea please?
Thank you!
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: Doc_to_PDF script

26 Feb 2022, 16:29

Not sure, but if I remember correctly, MS Word didn't have built-in pdf support back in 2007. I assume you could change that with an add-in, probably downloadable from Microsoft.
(Like there was an add-in - or add-on or whatever - to open docx files, for older Word versions. In that case, you had to download it from Microsoft. After installing, my Word 2000 could open and save docx documents.)
If it's still available - don't know...
RicKami
Posts: 29
Joined: 08 Oct 2020, 06:55

Re: Doc_to_PDF script

27 Feb 2022, 05:39

Yes! :-) I found an Add-in (SaveAsPDFandXPS.exe) and now it works fine :-)
Thank you!!!!!!!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 144 guests