MS Word 2004 "save as text"

Hi,

I’m trying to get Word (2004) to open a text file, save it as a text file and close it.

The text file to open is produced by Applescript (from Mail) and is in Unicode, but I need it to be in MAC-ASCII. Opening and “saving as text” in Word will do this conversion.

I can’t however get this to work with Applescript.

this:
on open of finderObjects
repeat with sourceFile in (finderObjects)
tell application “Finder”
tell application “Microsoft Word”
open sourceFile
save active document as (name of sourceFile)
close active document saving no
end tell
end tell
end repeat
end open

doesn’t work. The code works however when opening Word files coming from Windows, to convert them to Mac Word files.

Both of these conversions are necessary to prepare the files for placing in InDesign.

Any ideas on how to get Word to “save as” text files?? Any other solution for converting to MAC-ASCII is also welcome.

Thanks in advance,

Ton

Model: G5 2.5DP
Browser: Safari 417.9.2
Operating System: Mac OS X (10.4)

Ton:

Have you downloaded the gargantuan AppleScript reference for Word? Although daunting, I have found it useful for seemingly simple things like this. I will try to take a look through it myself sometime this weekend to see what it says.

Hi Craig,

Thanks for the tip. Finally a document that gives some complete examples. Where I went wrong was with the fact that you need to write:

save as active document file name “something” file format format text

It was the double “format” that eluded me.

A new problem is that I have to rename the file.

save as active document file format format text

does not save the document, despite the fact that the Word Reference states the file name parameter is optional. When omitted it should use the default (the durrent folder/file name). Using “name of sourceFile”:

save as active document file name (name of sourceFile) file format format text

(which does work when converting WIndows .doc’s to Mac .doc’s) also doesn’t work.

(name of soureFile as text)

and

(name of soureFile as string)

generate error messages.

Any ideas about that?

Thanks againg for the tip about the Word Reference. Although it is a large document it took me just 5 seconds to find the “save as” section and another 7 seconds to find out what I was doing wrong…

Ton

Ton:

Word is certainly quirky to script. What I have found is that to save a document, you first set the document to a variable, then use the full path (filename included) to save, NOT the filename, like this:

set desk_path to path to desktop as Unicode text
set the_text to "Hello World"
tell application "Microsoft Word"
	set newDoc to make new document
	insert text the_text at end of text object of newDoc
	save as newDoc file name (desk_path & "Sample.doc") --The default without the path saves in the Office 2004 folder.
end tell

I know it is crazy, but let’s face it. Any software that Bill and HIs Boys touch gets pretty screwy.

I hope this helps, let me know.

Hi Craig,

Thanks again for the input. Your solution is not exactly what I was looking for, but it set me on my way. What I was looking for was to save the new format of my text-file with the same name as the original (and then importing the text file with that name in InDesign and saving the Indesign file with the same name as the imported textfile).
But making a new file with a standard name, importing that file while Applescript remembers the name of the original file and using that as the filename in InDesign also works.
The reason this “name remembering” is important because it is an automated system for making business cards and the file name(s) refers to the person requesting the business card.

Thanks for sharing your knowledge.

Ton

PS As for Bill and his boys; Would we really appreciate the Positive if we wouldn’t have a Negative to compare it to??:smiley:

Ton:

OK, it is a little clearer now, but I am a bit slow on the uptake anyway. Once you have a script put together that works, go ahead and post it back with some pointers on where you would like things done differently. For me, anyway, that is always the easiest method, although there are plenty others here that don’t need that much clarity.

Well said.

Hi Craig,

Thanks for the offer, but I think it’s just some strange Word behavior that’s keeping me from getting exactly what I want. I got a working version now so don’t spend to much time on this, but if you want to look upon this as an expantion of your knowledge try this:

on open of finderObjects
	repeat with sourceFile in (finderObjects)		
		tell application "Microsoft Word"
			open sourceFile
			save active document as (name of sourceFile)
			close active document saving no
		end tell
	end repeat
end open

We use this to “convert” Windows Word files to Mac Word files (we noticed some strange behavior when importing Windows Word files into InDesign. This behavior disappears after this conversion). This works (notice the new date/time for the file(s)) when using Word files that come directly from Windows. It will do nothing when the file has been opened (and saved) on a Mac.

In a different script I use something similar to convert a text file to MAC-ASCII. Open a text file (coding unknown) in Word, save it as a text file and presto, a MAC-ASCII text file. This:

tell application "Microsoft Word" 
					open sourceFile
					save as active document file name (name of sourceFile) file format format text
					close active document saving no
				end tell

however does nothing. Trying with different kinds of file names (full path name as you suggested etc) doesn’t help the situation. Omitting the file name bit (the Word dictionary has this to say about it:

save as document
[file name Unicode text] : The name for the document. The default is the current folder and file name. If a document with the specified file name already exists, the document is overwritten without the user being prompted first.

) schould do the trick, but no joy from that either.

So what I was looking for is starting with a text file with a certain name in a certain location and ending up with a slightly different text file with the same name in the same location. And all of this in one line of code. There are other, more cumbersome ways to get to this (that’s the working script I got now), but what bugs me is that it should work in just the one line… But some Word quirks may be invincible…

regards,

Ton

PS: A positive note on Bill’s boys; They did greatly expand the AppleScript possibilities in Word 2004, because all of this would not have been possible without VB in previous versions of Word…:cool:

Ton:

I agree, according to the manual, your code should work, however, if you add a [.txt] to the end of the file, it does work, sort of:

set sourceFile to ((path to desktop as Unicode text) & "maintenance_results")
tell application "Microsoft Word"
	open sourceFile
	save as active document file name (sourceFile & ".txt") file format format text
	close active document saving no
end tell

You now have two files, one with the .txt (in the desired format) and the original file is unchanged. Here is my theory for why it is screwy: Even though the manual states that the file of the same name will be overwritten without warning, I think it chokes if the format is different from the original file. I think that the native Word coding simply will not allow it, at least in this particular fashion.

By tweaking it just a bit more, you can retain this new file in a variable to use other places:

set sourceFile to ((path to desktop as Unicode text) & "maintenance_results")
set new_File to sourceFile & ".txt"
tell application "Microsoft Word"
	open sourceFile
	save as active document file name (new_File) file format format text
	close active document saving no
end tell

Now, your variable [new_File] is the newly formatted file, and can be passed on to whatever else you want to do with it.

I hope this helps.

Hi Craig,

My solution was also along these lines, but I’m always looking for a way to optimize scripts (I’ve got another question out in the forum about removing unused styles in InDesign. I’ve written my own repeat&if (7 lines) solution, but manually you can do it in 2 steps and I want to reduce this script to two lines:rolleyes:…).

Now I know I can’t get this script any better I’ll put it to rest.

Thanks again for your help.

Ton