Word applescript that will open doc and insert page

P

pixelZION

Hi, I have about 1500 word docs that I have to open> unprotect> insert
two pages from another word doc at the beginning> protect> save> close

I haven't gotten very far with the dictionary. I'd like it to be a
subroutine if possible.

Thanks a lot for your help all.
 
J

John McGhie [MVP - Word and Word Macintosh]

Sorry... We won't write code "for" you in here -- that's paid-for work.

We "would" send you code that would do this if we had it around (maybe
someone has...) but I doubt if anyone has suitable code.

I have, but it's written in VBA for the PC and could not be expected to work
on the Mac.

If you would like to post the code you have written so far (as plain text!!)
someone may agree to show you how to modify it to do what you want.

However, you have only 1,500 documents to do. In the time it would take you
to write code to do this, you would have done the job by hand. If you were
to start the VBA Macro recorder, then step through doing ONE document, stop
the macro recorder and post the result that it recorded here, I could help
you.

Come to think of it, let me be a little more helpful than that :)

Here is code that I recorded with the Word macro recorder that will do
exactly as you want (it assumes that the Source document contains ONLY the
text you want to add, and that when you Unprotect the document the cursor is
already in the correct place).

Making a macro to process all the files in a directory is quite advanced
coding: instead, double-click the files in the Finder one by one, and start
your Macro recording with the target file already open on the screen.

Edit the code below as needed Remove the first two statements if the file
is already open on the screen).

This article will help:
http://www.word.mvps.org/macwordnew/installmacromaccontent.htm

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 11 September 2005 by John McGhie
'
ChangeFileOpenDirectory "John:Documents:MVP:"
Documents.Open FileName:="John:Documents:MVP:Document.doc", _
ConfirmConversions:=True, ReadOnly:=False, AddToRecentFiles:=False,
_
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto

ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
wdNoProtection
ActiveDocument.Unprotect

ChangeFileOpenDirectory "John:Documents:MVP:"
Documents.Open FileName:="John:Documents:MVP:Source.doc", _
ConfirmConversions:=True, ReadOnly:=False, AddToRecentFiles:=False,
_
PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto

Selection.WholeStory
Selection.Copy
ActiveWindow.Close
Selection.PasteAndFormat (wdPasteDefault)
ActiveDocument.Protect Password:="", NoReset:=False, Type:= _
wdAllowOnlyRevisions
ActiveDocument.Save
ActiveWindow.Close
End Sub

Cheers


Hi, I have about 1500 word docs that I have to open> unprotect> insert
two pages from another word doc at the beginning> protect> save> close

I haven't gotten very far with the dictionary. I'd like it to be a
subroutine if possible.

Thanks a lot for your help all.

--

Please reply to the newsgroup to maintain the thread. Please do not email
me unless I ask you to.

John McGhie <[email protected]>
Microsoft MVP, Word and Word for Macintosh. Consultant Technical Writer
Sydney, Australia +61 4 1209 1410
 
P

pixelZION

Thanks John. I'll look into that. Once I get back to work, I'll post my
Applescript code so far. I've got it to search the contents of the
folder, and then open each word doc. I guess then I'll use that macro.
I have no doubt I could have figured it out using only Applescript, had
I known someone that could have explained the syntax (the AS libraries
for apps aren't extremely helpful, as I'm new to AS).

Thanks, again. I'll definitely look into this tomorrow (monday).
 
M

matt neuburg

pixelZION said:
Thanks John. I'll look into that. Once I get back to work, I'll post my
Applescript code so far. I've got it to search the contents of the
folder, and then open each word doc. I guess then I'll use that macro.
I have no doubt I could have figured it out using only Applescript, had
I known someone that could have explained the syntax (the AS libraries
for apps aren't extremely helpful, as I'm new to AS).

To learn AppleScript, read my book (or any book on the subject that you
find congenial). To learn about Word's AppleScript terminology, read the
document linked from this page:

<http://www.microsoft.com/mac/resources/resources.aspx?pid=asforoffice>

m.
 
P

pixelZION

Thanks Matt, that link really helped. I've got this so far, which does
exactly what I want, EXCEPT that it's placing the inserted doc
seemingly arbitrarily on the page. I need it to be at the beginning of
the document. Think you can help? Thanks man.

jH



set wordDoc to (choose file with prompt "Choose file")

tell application "Microsoft Word"

activate

open wordDoc

unprotect active document password "jh"

insert file at text object of selection file name "Macintosh
HD:Users:jmhaynes:Desktop:WordTest:How to Fill Out a Copy Template.doc"
with link
protect active document protection type allow only form fields
password "jesse" with no reset
close active document saving yes

end tell
 
P

Paul Berkowitz

Thanks Matt, that link really helped. I've got this so far, which does
exactly what I want, EXCEPT that it's placing the inserted doc
seemingly arbitrarily on the page. I need it to be at the beginning of
the document. Think you can help? Thanks man.

jH



set wordDoc to (choose file with prompt "Choose file")

tell application "Microsoft Word"

activate

open wordDoc

unprotect active document password "jh"

insert file at text object of selection file name "Macintosh
HD:Users:jmhaynes:Desktop:WordTest:How to Fill Out a Copy Template.doc"
with link
protect active document protection type allow only form fields
password "jesse" with no reset
close active document saving yes

end tell


You have two choices: 1) if you put your cursor at the beginning of the
document, that's where the script above should insert it. (Or if you put it
somewhere else, that's where.) It inserts it at the 'selection' - which is
the cursor location i nothing is selected. Doesn't it? (A bug otherwise).

2) Or, you can be safe if you always want it at the beginning, and instead
of inserting the file at selection, you _should_ be able to do it here:

insert file at (character 0 of active document) file name "Macintosh
HD:Users:jmhaynes:Desktop:WordTest:How to Fill Out a Copy Template.doc" with
link


Except that the file seems to get inserted _after_ character 1, instead of
before it - where character 0 is supposed to be. This will do it right,
however:


set theStart to create range active document start 0 end 0
insert file at theStart file name "Macintosh
HD:Users:jmhaynes:Desktop:WordTest:How to Fill Out a Copy Template.doc" with
link

(Note that an inserted file is a field, so it will be shaded if you have
Preferences/View set to Show Field Shading Always, or When Swelected if
clicked anywhere within. Don't be alarmed. You can turn it off.)

--
Paul Berkowitz
MVP MacOffice
Entourage FAQ Page: <http://www.entourage.mvps.org/faq/index.html>
AppleScripts for Entourage: <http://macscripter.net/scriptbuilders/>

Please "Reply To Newsgroup" to reply to this message. Emails will be
ignored.

PLEASE always state which version of Microsoft Office you are using -
**2004**, X or 2001. It's often impossible to answer your questions
otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top