Trouble with Word script

I want to create a drop folder that will convert a Word document into plain text. Everything works as long as I manually define the file path in the script, which obviously won’t work because I don’t want every Word doc to end up as example.txt. The way I have the script written, the exact same output is being sent to the event log, but if I try to use variables to define the path and file name, Word doesn’t save anything. Anyone have any ideas why?


on adding folder items to this_folder after receiving added_items
    set filePath to "2074:word test" as alias
    repeat with i in added_items 
        tell application "Microsoft Word"
            activate
            open i without add to recent files
            set fileName to name of active document
            try
                set N to count of characters of fileName
                if (N > 3) and character (N - 3) of fileName is "." then
                    set fileName to characters 1 thru (N - 4) of fileName
                    set fileName to fileName as text
                else
                    --there isn't an extension
                end if
            end try
            set fileName to fileName & ".txt"
            set fullPath to (filePath & fileName) as text
            save active document in fullPath as file format text
            --save active document in "2074:word test:Preflight Procedures.txt" as file format text
            close active document without saving
        end tell
    end repeat
end adding folder items to

As it is, the script opens the file and closes it. If you comment out “save active document in fullPath as file format text” and uncomment the next line, it will save. As far as I can tell, the script is sending exactly the same command to Word, so it should work, right?

I figured it out.


on adding folder items to this_folder after receiving added_items
	repeat with i in added_items
		tell application "Microsoft Word"
			activate
			open i without add to recent files
			set fileName to name of active document
		end tell
		try
			set N to count of characters of fileName
			if (N > 3) and character (N - 3) of fileName is "." then
				set fileName to characters 1 thru (N - 4) of fileName
				set fileName to fileName as text
			else
				--there isn't an extension
			end if
		end try
		set fileName to fileName & ".txt"
		set newFolder to "Word to Text:"
		set thePath to (this_folder & newFolder) as string
		set theOutputPath to (thePath & fileName) as string
		tell application "Microsoft Word"
			save as active document file name theOutputPath file format format text
			close active document without saving
		end tell
	end repeat
end adding folder items to

It seems that putting any of the set commands within the tell block for Word would confuse Word to the point where it didn’t know how to save properly. By creating a separate tell block for the open command and one for the save command, everything worked the way it should.