Google Answers Logo
View Question
 
Q: Applescript for MS Word batch processing ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Applescript for MS Word batch processing
Category: Computers > Programming
Asked by: beta-ga
List Price: $15.00
Posted: 17 Feb 2003 18:30 PST
Expires: 19 Mar 2003 18:30 PST
Question ID: 162781
I need an Applescript that can batch process a folder of Microsoft
Word 2001 files and save them out as plain text.

Request for Question Clarification by maniac-ga on 18 Feb 2003 19:40 PST
Hello Beta,

A few questions on how to solve this

1 - Which version of MS Word are you using? (98, X, ...)

2 - Which version of Mac OS are you using? (X, 9.x, 8.x, ...)

3 - If I provide a Word Basic macro instead of Applescript - would you
take that instead?

4 - I assume you want a suffix of .txt (or similar) for the new files
(instead of replacing the original files). Please confirm.

Thanks
  --Maniac

Clarification of Question by beta-ga on 18 Feb 2003 20:34 PST
I am using Word from Office 2001
I am on OS 9.2.2
If a Word Macro will achieve the same result, that's fine.
To clarify what I'm looking for:  I get batches of Word docs which I
need to convert into plain vanilla ASCII text.  What I'm doing now is
opening each one individually and Saving As "Text Only".  I would like
the ability to simply drag my batch of Word docs onto something (like
a applescript droplet) and have it spit out new versions of the docs
as .txt files.
Answer  
Subject: Re: Applescript for MS Word batch processing
Answered By: maniac-ga on 19 Feb 2003 05:00 PST
Rated:5 out of 5 stars
 
Hello Beta,

There are a few ways to do this, but perhaps this is the most simple
to implement. I will describe first how to put the macro into your
"Normal" document (so it is available whenever you run MS Word). You
could save this to another file if desired (so you can share it with
other users); select the other file if you want to do this.

Step 1 - Create the macro

Open Microsoft Word.
Select Tools -> Macro -> Visual Basic Editor

At this point, you should get a window labeled "Project" - Normal. You
may get other windows if you have ever used Visual Basic before.
Select the item at the top of the window labeled "Normal".

Select Insert -> Module

You should now see a window labeled "Normal - Module1 (code)" [or
similar]. Paste the following code into that window.

' Save all as text - save all open documents as text files
Sub SaveAllAsText()
    Dim a$
    For Each adoc In Documents
        a$ = adoc.Name + ".txt"
        adoc.SaveAs FileName:=a$, FileFormat:= _
        wdFormatText, AddToRecentFiles:=False
        adoc.Close
    Next adoc
End Sub

To explain the code above, it declares a string variable A$. Then for
each document that is currently open,
 - sets A$ to (current name).txt
 - saves the file with that name, text format, and does not add the
file to the "Recent Files" menu
 - closes the file
When all files are processed, the macro stops.

Select File -> Save Normal
Select File -> Close and Return to Microsoft Word

At this point, you can open a few files to test it.

Step 2 - When it is time to convert files...

Select the files in the finder & open w/ Microsoft Word

Select Tools -> Macro -> Macros

A window will appear, select SaveAllAsText and then Run. The macro
will then process each open document and quit. You can then confirm
the conversion by opening the files or browsing the directory with the
finder.

Step 3 - Making it "better".

Just a few hints on how to write these kinds of macros (which work in
a similar manner in Excel and Powerpoint) are:
 - In Word, select Tools -> Macro -> Record Macro, then record a few
steps, and then stop. Go into Word Basic to see what was recorded. Cut
/ paste the steps into another macro to have it do what you want to.
 - In Visual Basic, select View -> Object Browser. This brings up a
tool that can describe the "objects" (such as Documents) defined by
Word. Type a phrase into the field to get a list of objects, methods,
and fields that match that phrase. Select one & then [?] to go
directly to the help on that topic. Be sure to look at the examples
for suggestions on using this. Do this for Documents for the idea on
the for loop on open documents.
 - In Visual Basic, select View -> Immediate Window. This brings up an
empty window. You can enter print statements here to examine variables
you define or the values in Word Objects. Something like
  print Documents.count
will display
  1
if you have just one open document.
 - Start with something simple. Add steps to it after each part is
"correct". There is a debugger as well, click on the left side of the
code window to set a "stop sign" on a line. That will stop your macro
before running that statement and allow you to print values (in the
immediate window) and make some minor fixes if needed.

I tested this with both Word 98 and Word X - it should work the same
on 2001. If not, tell me what you see so I can help diagnose the
problem.
  --Maniac
beta-ga rated this answer:5 out of 5 stars
Very good answer with excellent explanatory text.  Much appreciated!

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy