Results 1 to 6 of 6
  1. #1
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150

    Close word document based on file name

    Hi allm



    Sorry for not having any sample code already. I'm looking for a way to close a word document that I know if open based on the file name of that document.

    I have the following code to check whether the document is open, I'm stuck on the next step of how to automatically close it.

    Code:
        Public Function FileIsOpen(sPath As String, sName As String) As Boolean
    
    Dim ff As Long
    
         ' Set the name of the full path
        sWordDoc = TrailingSlash(sPath) & sName
    
    
        On Error Resume Next
    
    
        ff = FreeFile()
        Open sWordDoc For Input Lock Read As #ff
        Close ff
        FileIsOpen = (Err.Number <> 0)
    
    
        On Error GoTo 0
    
    End Function
    Trailing slash is just a bit of code that ensures that the string entered is followed by a trailing slash

  2. #2
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    To add I can close the active document just with
    Code:
    Public Function CloseWordDoc(sPath As String, sName As String) As Boolean
    
    
            Dim wdApp As Object
            Set wdApp = GetObject(, "Word.Application")
            wdApp.Documents.Close
    End Function
    However I don't know that it will be the active document, so I now need some code to activate it.

    I would also like to close word word itself if it's the last document open

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Bing: access vba close specific word document

    Does this help https://answers.microsoft.com/en-us/...b-68b599b31bf5
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    When I try and use

    Code:
    Public Function CloseWordDoc(sPath As String, sName As String) As Boolean
    
    
            Dim wdApp As Object
            Set wdApp = GetObject(, "Word.Application")
            wdApp.Documents(sPath & sName).Close
    End Function
    I get the file not specified error that that article suggests is because the file isn't open, when I know for a fact that it is

  5. #5
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Okay so this seems to work.

    Code:
    Public Function CloseWordDoc(sPath As String, sName As String) As Boolean
    
            Dim wdApp As Object
            Set wdApp = GetObject(, "Word.Application")
            wdApp.Documents.Open(sPath & sName).Activate
            wdApp.Documents.Close
    End Function
    Now, can anyone help with, if the document I just closed is the last word document open, then close Word as well.

    ' Count open word documents
    ' if count = 0, close word

  6. #6
    Robyn_P's Avatar
    Robyn_P is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Nov 2017
    Posts
    150
    Got it, thanks anyway. Very simple just
    Code:
    If wdApp.Documents.Count = 0 Then wdApp.Quit

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Close word document that is not active.
    By Homegrownandy in forum Programming
    Replies: 1
    Last Post: 07-20-2018, 02:47 AM
  2. Replies: 3
    Last Post: 06-27-2017, 12:06 PM
  3. Extract text from each word document in a file
    By mercapto in forum Programming
    Replies: 11
    Last Post: 03-12-2013, 10:29 PM
  4. Replies: 3
    Last Post: 02-22-2013, 06:41 AM
  5. Replies: 1
    Last Post: 12-21-2005, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums