+ Reply to Thread
Results 1 to 11 of 11

Closing A Word Document WIth Excel VBA

  1. #1
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Closing A Word Document WIth Excel VBA

    I've tried something as simple as :
    Please Login or Register  to view this content.
    This clearly won't work as this statement is exclusive to Word VBA.

    How would I go about saving and closing an open Word document, file name known, from Excel VBA?

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Closing A Word Document WIth Excel VBA

    Hi,

    As long as you know the full path to the file, you might use GetObject thus
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Closing A Word Document WIth Excel VBA

    Hello xlnitwit ... thank you so much for your help. That worked wonderfully!
    Last edited by Jenn68; 03-28-2017 at 10:08 AM.

  4. #4
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Closing A Word Document WIth Excel VBA

    OK ... looks like I also need to close Word after the document has been closed. My Google encounters haven't shown anything that appears useful in my case, so my guess is it's so simple I'm the only one having a need to ask.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Closing A Word Document WIth Excel VBA

    Perhaps this
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Closing A Word Document WIth Excel VBA

    xlnitwhit, this did work well. However, I do wish to save the document prior to closing it. I changed the .saved=true to .saved=false. I used DisplayAlerts to avoid the prompt.

    What I am finding is that after this routine, WINWORD.exe is still an active process. Because of this, it may be messing with some of my other code, namely function code that checks to see if a document is open or not. As long as WINWORD is an active process, that function code always sees an open document when really there isn't.

    Is there a far deeper rooted problem at play here?

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Closing A Word Document WIth Excel VBA

    It would be helpful to see all the code you are using.

  8. #8
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Closing A Word Document WIth Excel VBA

    Thank you.

    This is quite a large project. I'll post the code that (I feel) reflects the bulk of Word document access.
    This code is is triggered from a toggle button push on a userform. (uf4_works). It has been a challenge to explain how it is supposed to work, so I hope you are able to follow it.

    Please Login or Register  to view this content.
    The button can be in various states ...

    1False ... Button not pressed, colour is RGB(176,196,222). This indicates no report (sRPT) has been previously created. Pressing it puts in into state 1True
    1True ... Button pressed. Code picks up at the 'ELSE' statement, processes some code, before returning to the userform where the user can choose to view or print a merged document. It is here the sRPT is originally created and becomes accessible for future viewing with this same toggle button click.

    2False ... Button not pressed, colour is RGB(0,153,211). This indicates that the report (sRPT) has been previously created and available for viewing. Pressing it will allow the user to access the sRPT document for viewing and/or editing.
    2True ... Button is pressed. Launches a function to determine if the document is already open. If it is, nothing happens, other than turn the button colour purple indicating the file is already open.. If it isn't, this code will open it. Toggling from this state will close the document.

    Third state should not be encountered other than during testing phases. It will have been a result of the button remaining in a 2True state before terminating the code voluntarily. The Word document opened remains open. When encountered, the document is closed.
    Without this code, if the document remained open while attempting to access it again, the application would lock up. (Because the file is in use)

    3False ... Button not pressed, colour is RGB(102,0,204). This indicates that the document is currently open. This will close the document.
    3True ... allows the user to view


    Please Login or Register  to view this content.
    So, what I am experiencing, at the time the button is pushed, the document remains open and the button is purple. The document rRPT is successfully closed, the Word application quits, but there is still an instance of WINWORD.exe still in the process list. I am unable to open up the doc via 2True state. It is treated as though the document is already open.
    Last edited by Jenn68; 03-28-2017 at 01:05 PM.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Closing A Word Document WIth Excel VBA

    Change this
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Closing A Word Document WIth Excel VBA

    Wow. That was simple. Thank you again!

    What kind of behaviour can I expect (an error?) if the user closes the document before returning to the userform and hits the button to trigger the code which would normally close the word document for them? Ideally, the user depresses the button (now in a true state), the document opens, they view/edit, and then return to the userform. Hitting the button again sends it to a false state causing the document to save and close. But if the document is no longer there, I sense an error.

    While risking abusing your generousity, may I ask one additional question of similar nature?

    On a separate button click, how could I close all existences of open Word documents, regardless of their names?

  11. #11
    Forum Contributor
    Join Date
    06-15-2009
    Location
    Ontario, canada
    MS-Off Ver
    Office 365
    Posts
    371

    Re: Closing A Word Document WIth Excel VBA

    All is good. I answered my own questions after some trial and error.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Mail merge of Excel data to Excel document, not using a Word document
    By Howie D in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-23-2015, 07:35 AM
  2. Need assistance with Opening and closing Excel-document
    By RinorM in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-28-2014, 02:01 AM
  3. Add text to Existing Word Document if it Doesn't exist create Word Document
    By unstable81 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 12:15 AM
  4. Trouble closing MS Word document from Excel macro
    By Cumberland in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-17-2007, 01:00 PM
  5. closing a word document
    By natanz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2006, 06:35 PM
  6. How to change a excel document into a word document?
    By wannaknow in forum Excel General
    Replies: 1
    Last Post: 07-27-2005, 05:05 PM
  7. [SOLVED] Can you transform an excel document into a word document?
    By Tammy in forum Excel General
    Replies: 1
    Last Post: 05-19-2005, 11:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1