Jump to content

Word title to filename changer


Recommended Posts

I've found the following (below) script on the internet, but when I try run it I get:

Line 158 (File "C:......etc)

MsgBox(0,"Completed","All Word Documents in "&$sDoPath & " have

Error: Unterminated string.

Could anyone shed any light on the problem. Many thanks.

#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

Link to comment
Share on other sites

Hi, Did you try stepping through the errors with scite and fixing those word wrap errors?

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

#NoTrayIcon ; Hides tray icon

; 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

Cheers

Link to comment
Share on other sites

Apears this:

FileClose($sLogPath); Closes log file
FileClose($search); Closes the search handle
MsgBox(0,"Completed","All Word Documents in " & $sDocPath & " have had their " & _
_WordQuit($oWordApp); Closes MS Word
"titles changed to their corresponding filenames. The modification dates on " & _
"the files have remained intact.")
Exit
EndFunc

Should be something similar to:

FileClose($sLogPath); Closes log file
FileClose($search); Closes the search handle
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.")
_WordQuit($oWordApp); Closes MS Word
Exit
EndFunc

EDIT: Hell... I don't even know why that doesn't exist... :S It pasted like that too :S WEIRD! Disregard me :)

Edited by Bert
Link to comment
Share on other sites

Thanks for the replies. I'm not at all experienced with autoit. I have tried the above but still get the same error message.

I appreciate your time with helping. Is there any chance you could post a corrected version of the script?

I'm bascially trying to rename the title of the word file to the filename before the .doc

eg filename: Builder Letter.doc current title: Dear Mr.

to

filename: Builder Letter.doc new title: Builder Letter

Link to comment
Share on other sites

  • Moderators

I can verify that the script I linked to in my last post works on Windows XP SP2 w/Office 2007.

Please provide the following information:

  • Windows Version & Service Pack Level
  • Office Version & Service Pack Level
  • AutoIt Version
  • Error written to the SciTE output console
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...