I can't seem to "close" my documents on MSoft Word with AppleScript!

Hi
I have tons of documents open on MSoft Word and wish to close some of them. Luckily, those that I wish to close all start with the word "book", so I was thinking I could actually automate that with AppleScript hence avoid closing them one by one.
I wrote the following code:
tell application "Microsoft Word"
count (document)
          set countresult to result
          repeat with incrementValue from 1 to countresult
                    if name of document incrementValue starts with "book" then
  activate document
  close document
                    end if
          end repeat
end tell
To my surprise, it didn't work!
I got the following under "Replies", which showed that AppleScript did indeed try to close the document that starts with "book":
tell application "Microsoft Word"
count every document of current application
  --> 6
get name of document 1
  --> "book_mac_OS_X_and_iOS_internals.htm"
activate
close document
get name of document 2
  --> "applicable books.htm"
get name of document 3
  --> "What Do You Want Homespace to Do.htm"
get name of document 4
  --> "URL_repository_for_outcomes.htm"
get name of document 5
  --> "SYNTHESIS - History a Step Back.docx"
get name of document 6
  --> "SYNTHESIS - History a Step Back.docx"
end tell
But when I went back to Word, I still saw the document there!
I actually added an additional line (I didn't include the code in that which I pasted here) asking to count the number of documents in Word AFTER the so-called "close", and the number remains 6.
Can anyone enlighten me on this?
Regards,
junxiang99

Hi,
The quickest way to do this (tested with OS 10.8.2, MS Word 2011) is in one line:
tell application "Microsoft Word" to close every document whose name begins with "book"
The reason why your script doesn't work is that you aren't specifying to Word which document you want it to close.
So, changing one line ought to do it:
close document incrementvalue -- instead of "close document"
But... when you close a document, you change the document count. Document 2 becomes document 1, document 3 becomes document 2 etc. So eventually you'd get a message saying "The object you are trying to access doesn't exist".
Because of this, if you want to do it in a repeat loop, you have to work backwards:
tell application "Microsoft Word"
  set doc_count to count documents
  repeat with x from doc_count to 1 by -1
  if name of document x begins with "book" then close document x
  end repeat
end tell
Hope this helps.

Similar Messages

  • Can't seem to change the Document Properties Advanced Reading Options in a Form that was created in Adobe LiveCycle. This option has been "greyed" out - anyone know how to update this to English so I can pass Accessibility Testing?

    Can't seem to change the Document Properties > Advanced > Reading Options in a Form that was created in Adobe LiveCycle. This option has been "greyed" out - anyone know how to update this to English so I can pass Accessibility Testing?

    LiveCycle forms use a very different approach to create a PDF form than Acrobat and that approach makes many features for processing PDFs non-functional.
    You might want to ask this question in one of the LiveCycle product forums.

  • Somehow my MacBookPro iCloud account has a different name and password from my other devices.  Even my date of birth is listed incorrectly.  I have tried but can't seem to change this info to match up with my other devices.  Can someone help?

    Somehow my MacBookPro iCloud account has a different name and password from my other devices.  Even my date of birth is listed incorrectly.  I have tried but can't seem to change this info to match up with my other devices.  Can someone help?

    Thanks
    I have a PC (Windows 7)
    Not sure when mobile me changed, but it was this weekend that my storage etc. lapsed (I have paid to keep storage with icloud). So i assume this has something to with it stopping.

  • How can I convert my Open Source document files into Word document files? I cannot download Pages since my Macbook Air does not have the most recent software.

    How can I convert my Open Source document files into Word document files? I cannot download Pages since my Macbook Air does not have the most recent software. I downloaded open office to my mac to try and save money. It worked well for a while. Now I get this pop-up message that asks me to "Reopen" and when I select the option, nothing happens. I cannot save my documents anymore and I cannot convert them to word. Help!

    dwb wrote:
    Does OpenOffice output Word documents by default or do you have to select it manually?
    You have 17 options to save as in Open Office, one of which is .doc  files,  yes it needs to be saved manually.
    You may be able to default to DOC, but have not tried same.
    Since Open Office is 99% same as Word, I use it, or Word, either one.  Open Office is a bit less buggy than Word 11'

  • I reset my phone and it now receives calls that were meant for my husband. I know how to fix this with messaging and facetime, but can't seem to find how to make it stop with the calls. Please help.

    I reset my phone and it now receives calls that were meant for my husband. I know how to fix this with messaging and facetime, but can't seem to find how to make it stop with the calls. Please help.

    It may be due to Continuity
    The following quote is from  Connect your iPhone, iPad, and iPod touch using Continuity
    Turn off iPhone cellular calls
    To turn off iPhone Cellular Calls on a device, go to Settings > FaceTime and turn off iPhone Cellular Calls.

  • How do I talk to someone about my account and merging two subscriptions together...Can't seem to get to any way to conversing with a human from the website.

    How do I talk to someone about my account and merging two subscriptions together...Can't seem to get to any way to conversing with a human from the website.

    Worst Service In the World
    I just want to cancel my order and get a refund, the company MUST be corrupt because it is nearly Impossible to get what is rightfully mine. It's been 3 years.. how, sad, is that, I got other people to try too...
    they steal from people... by making it impossible to talk to anyone real.

  • Why can't i open an attached document (most often pps) with firefox when i can with internet explorer?

    why can't i open an attached document (most often pps) with firefox when i can with internet explorer?

    Perhaps if you change your setting here:
    orange Firefox button ''or'' Tools menu > Options > Applications tab
    In the search box, type '''power''' and then pause until the list is filtered. If you have these, try changing the action to "Always Ask":
    Microsoft PowerPoint Presentation<br>
    Microsoft PowerPoint Slide Show
    It's possible that if you don't have Microsoft Office installed, you might not have those listed.

  • How can I exchange my pc created documents on Excel, Word, and PowerPoint with my mac and my Pages, Numbers and Keynote created content with my pc?  I need to create and edit between both.

    How can I exchange my pc created documents on Excel, Word, and PowerPoint with my mac and my Pages, Numbers and Keynote created content with my pc?  I need to create and edit and exchange between both types of operating systems.

    Your Windows system will not open any Pages, Numbers or Keynote documents. No applications exist for Windows that can open those formats. You will need to export your documents in Word, Excel and PowerPoint formats, respectively, before you'll be able to view and edit the documents on your Windows system. The iWork documents can natively open Word, Excel and PP documents, though there are limitations on what can be imported (for instance, Numbers does not support all possible Excel functions). Consult the documentation for the relevant iWork application for details on importing and exporting.
    If you are asking how to get the documents from one system to the other, then there are several ways to do that, including file sharing, using an external drive (hard drive, USB flash drive), or emailing the documents to yourself. Which would be best for your situation I can't say without more details about your usage.
    Regarsd.

  • I can't seem to export page documents as word documents

    Recently, when try to save a pages document as a word document, I get a message that tells me that I can't export the document as a similarly named word document. What's happened? It is a new phenomenon, as I've been doing this as long as I've had Pages. Often the pages document was created by copying text from the internet.

    I get a message that tells me that I can't export the document as a similarly named word document.
    Are you trying to save it with the name of an already existing Word document in the same location??? If not try closing Pages, then delete the com.apple.iwork.pages.plist in ~ > Library > Preferences. restart the computer if you haven't done it lately. Try Export to Word in Pages again.

  • Can't close pop up ad in Words with Friends

    IWhile playing Words With Friends got a pop up ad for Verizon that will not close and can't continue playing. Clicking on top right hand close X does not work

    I have noticed the same problem when using Win 8 Pro. Works fine with Win 7 Pro. CHrome also doesn't work from Win 8 but Sarfari works partially. Problem with it is you need to refresh (F5) the screen after each completed play.

  • How do I Scan Document to MS WORD With HP ENVY 4501

    How do I Scan Document to MS WORD on HP ENVY 4501

    Hey @LarrySr63 ,
    You can not scan to Microsoft Word. You scan the document as a specific file (PDF, JPEG or TIFF) to a storage directory on your computer that you can then open later in Microsoft Word. Remember, when scanning you are saving the document as an image and you need special programs to be able to edit those documents.
    For how to scan on Vista >> http://h10025.www1.hp.com/ewfrf/wc/document?docname=c03515720&tmp_task=useCategory&cc=us&dlc=en&lang...
    Happy new year! Let me know how it goes!
    I worked for HP.

  • How do i convert a pdf document to ms word with adobe acrobat x standard?

    I purchased adobe acrobat x standard so as to convert pdf documents into ms word and excel. When I try to convert docments it asks me to purchase another adobe product.

    The ability to export a PDF file to Word or Excel is part of Acrobat X Std. as detailed in this comparison chart. It sounds like you might actually have Adobe Reader X open instead of Acrobat X Std because Reader has the command File > Save As > Word or Excel Online. This partcular command in Reader links to the ExportPDF online service.

  • I can't seem to close the screen on my new MacBook Pro with ext montitor

    I have a new MacBook Pro with a Samsung T240 through the DVI link. I can see my desktop on the external monitor when the laptop screen is open, but if I close it the computer goes to sleep and I cannot see anything on the T240.
    I have the Bluetooth keyboard and the Bluetooth mouse. The mouse also deactivates until I manually browse for it after I wake the computer up by opening the screen.
    I know I should be able to keep the display closed but how do I do this? Thanks!

    MrAceXPS wrote:
    I know I should be able to keep the display closed but how do I do this? Thanks!
    Let the laptop sleep, then hit a key on the keyboard or wiggle the mouse to wake it up.

  • Cube spin transitions create gaps I can't seem to close

    Hi,
    I'm using cube spin transitions for the closing credits. I'm stuck with three different clips having gaps between the spin and the next credit, and no amount of control + g in the timeline gaps, no amount of snapping, has been able to push the two clips together with this transition. I am using other transitions in other parts of my movie, and have not had any trouble. Does anyone have any other ideas as to what next to try? I suspect that the trouble is with the type of files I'm using for the credits. I'm connecting .psd files with mov files (from LiveType) and using the cube spin transition between them. I know that these have alpha channels and so need to be on a higher line in the timeline than the rest of the movie. But obviously I'm missing something.
    Thanks,
    Sheila

    I think I am cheating when I say that I put transitions on both the endings of one clip, and on the beginning of the next clip for full cube turning.

  • Can you password protect a Pages document saved in Word format

    Hi
    I know how do do it for a Pages document, but when I try to save as a Word document it appears I can't.  Is that not allowed?

    I think they are just using different methods.
    Peter

Maybe you are looking for

  • Help: Parameter form related

    Hi, I am working a set of FORM LETTER reports. I'd like to include a parameter form so when user enter multiple ID numbers separated by comma ',', the REPORT will print multiple letters. In my query, I included a line of constraint as: where ID IN (:

  • External hard drive use with both Mac and Windows

    I know that you can't use an iPod (through iTunes) with both Mac and Windows, but can you still use it as an external hard drive with both operating systems?

  • Will the iMac support a usb 3.0 hub

    I would like to get a 4 + port hub and thought I'd go ahead anf get the USB 3.0 but I'm not sure my MAC can handle it.  I have all 2.0 devices now but am thinking about the future.  I have the iMAC Quad Core i7.

  • Populating application item when updateable query report item is changed

    I have been looking at the help for cascading LOVs posted here. Unfortunately, I am not able to get it to work, and so am backing up with baby steps. I have a sql updateable report. It has a column (IN_STATE) that is a LOV sql query that pulls up a l

  • Dynamic URL-variables

    I have this URL-variables: test.php?id=1&kw=lente. id and cat_lev02 are dynamic placed. Now I want to filter the content so only item id="1" with cat_lev02 = "lente" are shown: id and cat_lev02 are dynamic placed in the link above. How can I do this?