Airwolf Posted May 2, 2007 Share Posted May 2, 2007 (edited) Using the following UDF: http://www.autoitscript.com/forum/index.php?showtopic=30461 I am trying to create a script that reads the filename of a *.doc file and then changes the Word Document Title to that filename string. So far this is what I have: expandcollapse popup#include <file.au3> #include <Word.au3> Global $titlesearch Global $currentfile Global $oldtitle Global $filename Global $log Global $oDoc Global $oWordApp $log = "wordtitle.log" FileOpen($log,1) $titlesearch = FileFindFirstFile("C:\Documents and Settings\aflesner\Desktop\*.doc") Do $currentfile = FileFindNextFile($titlesearch) If @error = 1 Then Else $oWordApp = _WordCreate("",0,0,0) $oDoc = _WordDocOpen ($oWordApp,$currentfile) $oldtitle = _WordDocPropertyGet($oDoc,"Title") FileWriteLine($log,"===========================") FileWriteLine($log,$currentfile) FileWriteLine($log,"===========================") FileWriteLine($log,"Old Title was: " & $oldtitle) $filename = StringTrimRight($currentfile,4) _WordDocPropertySet($currentfile,"title",$filename) FileWriteLine($log,"New Title is: " & $filename) FileWriteLine($log,"") FileWriteLine($log,"") _WordDocSave ($oDoc) _WordQuit ($oWordApp) EndIf Until $currentfile = "" Exit My log file shows the following: =========================== Document Test 1.doc =========================== Old Title was: 0 New Title is: 0 =========================== Document Test 2.doc =========================== Old Title was: 0 New Title is: 0 =========================== Document Test 3.doc =========================== Old Title was: 0 New Title is: 0 =========================== Document Test 4.doc =========================== Old Title was: 0 New Title is: 0 Any ideas why this isn't working? I really appreciate any help. Thanks! Edited May 2, 2007 by Airwolf123 Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt Link to comment Share on other sites More sharing options...
Moderators big_daddy Posted May 2, 2007 Moderators Share Posted May 2, 2007 Here is a working example. expandcollapse popup#include <Word.au3> Global _ $search, $file, _ $sTitle, $sFileName, _ $oDoc, $oWordApp _WordErrorHandlerRegister () $oWordApp = _WordCreate ("", 0, 1, 0) $sPath = "c:\docs\" ; don't forget trailing backslash ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile($sPath & "*.doc") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop $oDoc = _WordDocOpen ($oWordApp, $sPath & $file) $sTitle = _WordDocPropertyGet ($oDoc, "Title") ConsoleWrite("Filename: " & $file & @CR) ConsoleWrite(@TAB & "Title: " & $sTitle & @CR) $sFileName = StringTrimRight($file, 4) _WordDocPropertySet ($oDoc, "title", $sFileName) $sTitle = _WordDocPropertyGet ($oDoc, "Title") ConsoleWrite(@TAB & "New Title: " & $sTitle & @CR & @CR) _WordDocClose ($oDoc, -1) WEnd ; Close the search handle FileClose($search) _WordQuit ($oWordApp) Link to comment Share on other sites More sharing options...
Airwolf Posted May 2, 2007 Author Share Posted May 2, 2007 Works great! You're the best! Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt Link to comment Share on other sites More sharing options...
Moderators big_daddy Posted May 2, 2007 Moderators Share Posted May 2, 2007 Works great! You're the best!That's good to hear! Link to comment Share on other sites More sharing options...
Airwolf Posted May 4, 2007 Author Share Posted May 4, 2007 Now I've got a new problem... I'm trying to use a GUI and the script has stopped working for some reason. Here is what I have now: expandcollapse popup#include <Word.au3> #include <GuiConstants.au3> ; Change to OnEvent mode Opt('GUIOnEventMode', 1) Opt("TrayIconHide", 1) Global _ $search, $file, _ $sTitle, $sFileName, _ $oDoc, $oWordApp, _ $log, $logpath, _ $sPathpath, $sTitlenew ; 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) $sPathpath = GUICtrlCreateInput("",10,165,300,20) ; BUTTON GUICtrlCreateButton("Go!", 45, 200, 230, 30) GUICtrlSetOnEvent(-1, 'TitleChange') Func TitleChange() ; Hide the GUI while the function is running GUISetState(@SW_HIDE) _WordErrorHandlerRegister () $oWordApp = _WordCreate ("", 0, 1, 0) $sPath = GUICtrlRead($sPathpath) & "\" $log = GUICtrlRead($logpath) & "\wordtitle.log" ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile($sPath & "*.doc") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file = FileFindNextFile($search) If @error Then ExitLoop $oDoc = _WordDocOpen ($oWordApp, $sPath & $file) $sTitle = _WordDocPropertyGet ($oDoc, "Title") FileOpen($log,1) FileWriteLine($log,"===========================") FileWriteLine($log,$file) FileWriteLine($log,"===========================") FileWriteLine($log,"Old Title was: " & $sTitle) FileClose($log) $sFileName = StringTrimRight($file, 4) _WordDocPropertySet ($oDoc, "Title", $sFileName) $sTitlenew = _WordDocPropertyGet ($oDoc, "Title") FileOpen($log,1) FileWriteLine($log,"New Title is: " & $sTitlenew) FileWriteLine($log,"") FileWriteLine($log,"") FileClose($log) _WordDocClose ($oDoc, -1) WEnd ; Close the search handle FileClose($search) _WordQuit ($oWordApp) Exit EndFunc GUISetState(@SW_SHOW) While 1 Sleep(250) WEnd Func Event_GUIClose() Exit EndFunc ;==>Event_GUIClose Even if I change _WordDocClose ($oDoc, -1) to _WordDocClose ($oDoc, -2) I don't even receive a prompt to save. My log file shows that the title is changed but it is not successfully being saved. Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt Link to comment Share on other sites More sharing options...
Moderators big_daddy Posted May 4, 2007 Moderators Share Posted May 4, 2007 (edited) Are you by chance using Office 2007? I don't get prompted either since upgrading. This works very well for me. expandcollapse popup#include <Word.au3> #include <GuiConstants.au3> ; Change to OnEvent mode Opt('GUIOnEventMode', 1) Opt("TrayIconHide", 1) Global _ $LogPath, _ $DocPath ; 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) While 1 Sleep(250) WEnd Func TitleChange() ; Hide the GUI while the function is running GUISetState(@SW_HIDE) _WordErrorHandlerRegister () $oWordApp = _WordCreate ("", 0, 0, 0) $sDocPath = GUICtrlRead($DocPath) If StringRight($sDocPath, 1) <> "\" Then $sDocPath &= "\" EndIf $sLogPath = GUICtrlRead($LogPath) & "\wordtitle.log" ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile($sDocPath & "*.doc") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf $hLogFile = FileOpen($sLogPath, 1) While 1 $file = FileFindNextFile($search) If @error Then ExitLoop $oDoc = _WordDocOpen ($oWordApp, $sDocPath & $file) $sTitle = _WordDocPropertyGet ($oDoc, "Title") FileWriteLine($sLogPath, "===========================") FileWriteLine($sLogPath, $file) FileWriteLine($sLogPath, "===========================") FileWriteLine($sLogPath, "Old Title was: " & $sTitle) FileClose($sLogPath) $sFileName = StringTrimRight($file, StringLen($file) - StringInStr($file, ".", Default, -1) + 1) _WordDocPropertySet ($oDoc, "Title", $sFileName) $sTitleNew = _WordDocPropertyGet ($oDoc, "Title") FileOpen($sLogPath, 1) FileWriteLine($sLogPath, "New Title is: " & $sTitleNew) FileWriteLine($sLogPath, "") FileWriteLine($sLogPath, "") _WordDocClose ($oDoc, -1) WEnd FileClose($hLogFile) ; Close the search handle FileClose($search) _WordQuit ($oWordApp) GUISetState(@SW_SHOW) EndFunc ;==>TitleChange Func Event_GUIClose() Exit EndFunc ;==>Event_GUIClose Edited May 4, 2007 by big_daddy Link to comment Share on other sites More sharing options...
Airwolf Posted May 4, 2007 Author Share Posted May 4, 2007 I'm using Office 2003... I tried that modified script you made and it still isn't working. The log file displays the proper new title but when I look at file properties the title still shows as the old one. Maybe I'll reboot my box... could be an Office issue. Certifications: A+, Network+, Security+, Linux+, LPIC-1, MCSA | Languages: AutoIt, C, SQL, .NETBooks: AutoIt v3: Your Quick Guide - $7.99 - O'Reilly Media - September 2007-------->[u]AutoIt v3 Development - newbie to g33k[/u] - Coming Soon - Fate Publishing - Spring 2013UDF Libraries: SkypeCOM UDF Library | ADUC Computers OU Cleanup | Find PixelChecksumExamples: Skype COM Examples - Skype4COMLib Examples converted from VBS to AutoIt Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now