Jump to content

Problems with Word.au3 UDF


Recommended Posts

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:

#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 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

  • Moderators

Here is a working example.

#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

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

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:

#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

  • Moderators

Are you by chance using Office 2007? I don't get prompted either since upgrading.

This works very well for me.

#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 by big_daddy
Link to comment
Share on other sites

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

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...