Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Automation Examples > Word Document Title Changer

Word Document Title Changer

This program was written using the Microsoft Word Automation Library mentioned earlier in the user-defined functions section. The script in Word Document Title Changer scours a directory for *.doc files, and then sets the Title property of each file to its filename while keeping modified dates intact.

Example. Word Document Title Changer

#NoTrayIcon
					; Hides tray icon

; Include File, Word, and GUI Constants
;=====================================================================
					#include <file.au3>
#include <Word.au3>
#include <GuiConstants.au3>

; Change to OnEvent Mode
;=====================================================================
					Opt('GUIOnEventMode', 1)
					; Declare Global Variables
;=====================================================================
					Global
					$LogPath, $DocPath, $progress, $progresspercent
					; GUI
					GUICreate("Microsoft Word Document Title Changer", 320, 250)
GUISetIcon("icon.ico")
GUISetOnEvent($GUI_EVENT_CLOSE, 'Event_GUIClose')
					; PIC
					GUICtrlCreatePic("logo.gif", 128.5, 5, 63, 56)
					; Log Path
					GUICtrlCreateLabel("Please type the full path where you would", 10, 70, 300, 15, _
$SS_CENTER)
GUICtrlCreateLabel("like to save the log file:", 10, 85, 300, 15, $SS_CENTER)
					$LogPath = GUICtrlCreateInput("", 10, 105, 300, 20)
					; Modification Path
					GUICtrlCreateLabel("Please type the full path of the directory", 10, 130, 300, 15, _
$SS_CENTER)
GUICtrlCreateLabel("you would like files changed in:", 10, 145, 300, 15, $SS_CENTER
)
					$DocPath = GUICtrlCreateInput("", 10, 165, 300, 20)
					; Button
					GUICtrlCreateButton("Go!", 45, 200, 230, 30)
GUICtrlSetOnEvent(−1, 'TitleChange')
					GUISetState(@SW_SHOW)
					; show the GUI
					While 1
    Sleep(250)
WEnd
					Func TitleChange()
					; Hide the GUI while the function is running
    ;=====================================================================
					GUISetState(@SW_HIDE)

    _WordErrorHandlerRegister()
					; Opens Word
    ;=====================================================================
					$oWordApp = _WordCreate("", 0, 0, 0)
					; Creates $sDocPath variable based on the entry in the GUI for the Change path
    ;=====================================================================
					$sDocPath = GUICtrlRead($DocPath)
					; Adds trailing backslash if it doesn't exist
    ;=====================================================================
					If
					StringRight($sDocPath, 1) <> "\" Then
					$sDocPath &= "\"
    EndIf
					$logpathfirst = GUICtrlRead($LogPath)
    If
					StringRight($logpathfirst, 1) <> "\" Then
					$logpathfirst &= "\"
    EndIf
					; Creates $sLogPath variable based on the entry in the GUI for the Log path
    ;=====================================================================
					$sLogPath = $logpathfirst & "wordtitle.log"

    ; Pulls the filenames of all files in the Change directory
    ;=====================================================================
					$search = FileFindFirstFile($sDocPath & "*.doc")
					$filelist = _FileListToArray($sDocPath,"*.doc",1)
					; Calculates the percentage change of each doc file toward the total number
    ;=====================================================================
					$filepercent = 100 / $filelist[0]
					; Check if the search for *.doc was successful
    ;=====================================================================
					If
					$search = −1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf
					; Opens the log file for writing
    ;=====================================================================
					FileOpen($sLogPath, 1)
					; Turns on the Progress Bar
    ;=====================================================================
					ProgressOn("Doc Title Change Progress","Word Document titles " & _
    "changing...","",300,200,16)
					While 1 ; loops until there are no more *.doc files
					$file = FileFindNextFile($search)
        If @error Then
					; if the last file listed was not *.doc then the
loop exits
					ProgressOff()
					; turns off progress bar
					ExitLoop
					; exits the While...WEnd statement
					Else
        EndIf
					$filetime = FileGetTime($sDocPath & $file,0,1)
					; pulls the
Modified Date
        ; from file properties
					$oDoc = _WordDocOpen($oWordApp, $sDocPath & $file)
					; opens
the last found
        ; *.doc file
					$sTitle = _WordDocPropertyGet($oDoc, "Title")
					; gets original
title of Word
        ; Doc
        ; Writes the old title to the log file
        ;=====================================================================
					FileWriteLine($sLogPath, "===========================")
        FileWriteLine($sLogPath, $sDocPath & $file)
        FileWriteLine($sLogPath, "===========================")
        FileWriteLine($sLogPath, "Old Title was: " & $sTitle)
					; Creates $sFileName variable based on the filename and removes the trailing
        ; .doc
        ;=====================================================================
					$sFileName = StringTrimRight($file, StringLen($file) -
StringInStr($file, _
        ".", Default, −1) + 1)
        _WordDocPropertySet($oDoc, "Title", $sFileName)
					; sets new title
to $sFileName
					$sTitleNew = _WordDocPropertyGet($oDoc, "Title")
						; gets new title
for Log
        ; Write the new title and the modification date to the log file
        ;=====================================================================
						FileWriteLine($sLogPath, "New Title is: " & $sTitleNew)
        FileWriteLine($sLogPath, "Modification Date: " & $filetime)
        FileWriteLine($sLogPath, "")
        FileWriteLine($sLogPath, "")
        _WordDocClose($oDoc, −1)
					; closes the Word doc saving changes made
					FileSetTime($sDocPath & $file,$filetime,0)
					; sets the
modification date back
        ; to the original time/date
					$progress = $progress + $filepercent
					; adds the percentage change of
        ; completing one file to the progress bar
					$progresspercent = StringLeft($progress,2)
					; trims the percentage
to 2
        ; characters
					ProgressSet($progress,$progresspercent & " % completed...")
					; sets the
        ; progress bar to the current completion %
					WEnd
					FileClose($sLogPath)
					; Closes log file
					FileClose($search)
					; Closes the search handle
    _WordQuit($oWordApp)
					; Closes MS Word
					MsgBox(0,"Completed","All Word Documents in " & $sDocPath & " have
had their " & _
    "titles changed to their corresponding filenames. The modification dates on " & _
    "the files have remained intact.")
    Exit
EndFunc
					; Closes the GUI if the X button is pressed to exit
;=====================================================================
					Func Event_GUIClose()
    Exit
EndFunc

					  


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint