Check if Workbook is open and close if it is

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davnao
    New Member
    • May 2007
    • 8

    Check if Workbook is open and close if it is

    Looking for VBscript that will check to see if a specific Excel work book is open, by passing a variable name to check for which is the full file name with path. If it is open, it closes it.
  • JConsulting
    Recognized Expert Contributor
    • Apr 2007
    • 603

    #2
    Originally posted by davnao
    Looking for VBscript that will check to see if a specific Excel work book is open, by passing a variable name to check for which is the full file name with path. If it is open, it closes it.
    How about something easy

    Code:
    Function fFileOpen(strFile As String) As Boolean
    On Error Resume Next
    
        Dim intFile As Integer
        If Dir(strFile) = "" Then Exit Function
        
        intFile = FreeFile()
        Open strFile For Input Lock Read As intFile
        Close intFile
    
        If err <> 0 Then
            fFileOpen = True
        End If
        
    End Function

    Comment

    • JConsulting
      Recognized Expert Contributor
      • Apr 2007
      • 603

      #3
      Originally posted by davnao
      Looking for VBscript that will check to see if a specific Excel work book is open, by passing a variable name to check for which is the full file name with path. If it is open, it closes it.

      About the close part.

      Unless you are the person/program with the excel file open, you cannot close it.

      Comment

      • davnao
        New Member
        • May 2007
        • 8

        #4
        [JConsulting]

        When I run code, I get a Run Time error '70'; Permission denied when I actually have a file open on code line which reads:

        Open strFile For Input Lock Read As intFile

        For some reason, won't "resume" as code tells it to on error. Odd

        About the close part, only I have the file open so it is my file.

        Comment

        • JConsulting
          Recognized Expert Contributor
          • Apr 2007
          • 603

          #5
          Originally posted by davnao
          [JConsulting]

          When I run code, I get a Run Time error '70'; Permission denied when I actually have a file open on code line which reads:

          Open strFile For Input Lock Read As intFile

          For some reason, won't "resume" as code tells it to on error. Odd

          About the close part, only I have the file open so it is my file.
          how are you running the code?

          Comment

          • davnao
            New Member
            • May 2007
            • 8

            #6
            Originally posted by JConsulting
            how are you running the code?
            Pasted your code in a Module.

            Running code from a form, on click, right now just asking to display status

            MsgBox fFileOpen(fname ) ' where fname includes path & filename

            Is that what you were asking?

            Comment

            • JConsulting
              Recognized Expert Contributor
              • Apr 2007
              • 603

              #7
              Originally posted by davnao
              Pasted your code in a Module.

              Running code from a form, on click, right now just asking to display status

              MsgBox fFileOpen(fname ) ' where fname includes path & filename

              Is that what you were asking?
              you're doing everything right...can you capture the error?

              if err.number = 77 then
              fFileOpen = true
              end if

              Comment

              • davnao
                New Member
                • May 2007
                • 8

                #8
                [QUOTE=JConsulti ng]you're doing everything right...can you capture the error?

                Won't let me...seems to get here

                Open strFile For Input Lock Read As intFile

                and get stuck and will go no further, won't execute the "on error" code at all; tried turning off warnings and still won't execute next line of code.

                Comment

                • JConsulting
                  Recognized Expert Contributor
                  • Apr 2007
                  • 603

                  #9
                  Originally posted by JConsulting
                  you're doing everything right...can you capture the error?

                  if err.number = 77 then
                  fFileOpen = true
                  end if

                  Here is an option capturing the errors

                  Code:
                  Sub TestExcel(strFile)as boolean
                      Dim intFreeFile As Integer
                  
                      On Error Resume Next
                      intFreeFile = FreeFile
                      Open strFile For Input Lock Read As #intFreeFile
                      Select Case Err.Number
                          Case 70
                              MsgBox "This file is already in use"
                                   TestExcel = true
                          Case 0
                              Close #intFreeFile
                          Case Else
                              '   Some other error
                      End Select
                  
                  End Sub

                  Comment

                  • JConsulting
                    Recognized Expert Contributor
                    • Apr 2007
                    • 603

                    #10
                    Originally posted by JConsulting
                    Here is an option capturing the errors

                    Code:
                    Sub TestExcel(strFile)as boolean
                        Dim intFreeFile As Integer
                    
                        On Error Resume Next
                        intFreeFile = FreeFile
                        Open strFile For Input Lock Read As #intFreeFile
                        Select Case Err.Number
                            Case 70
                                MsgBox "This file is already in use"
                                     TestExcel = true
                            Case 0
                                Close #intFreeFile
                            Case Else
                                '   Some other error
                        End Select
                    
                    End Sub

                    and another if that fails to work for you

                    Code:
                    Private Function IsFileAlreadyOpen(Filename As String) As Boolean
                    '   Returns TRUE if the workbook is open
                      Dim x As Workbook
                      On Error Resume Next
                      Set x = Workbooks("c:\test.xls")
                      If Err = 0 Then IsFileAlreadyOpen = True _
                          Else IsFileAlreadyOpen = False
                    End Function

                    Comment

                    • davnao
                      New Member
                      • May 2007
                      • 8

                      #11
                      Originally posted by JConsulting
                      and another if that fails to work for you

                      Code:
                      Private Function IsFileAlreadyOpen(Filename As String) As Boolean
                      '   Returns TRUE if the workbook is open
                        Dim x As Workbook
                        On Error Resume Next
                        Set x = Workbooks("c:\test.xls")
                        If Err = 0 Then IsFileAlreadyOpen = True _
                            Else IsFileAlreadyOpen = False
                      End Function
                      Pasted your code with the following in a new module and it worked:

                      Code:
                      Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
                      Private Declare Function GetLastError Lib "kernel32" () As Long
                      Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
                      Now how do I close a file if it is found open by this code?
                      Last edited by NeoPa; May 24 '07, 11:19 PM. Reason: Tags

                      Comment

                      • JConsulting
                        Recognized Expert Contributor
                        • Apr 2007
                        • 603

                        #12
                        Originally posted by davnao
                        Pasted your code with the following in a new module and it worked:

                        Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
                        Private Declare Function GetLastError Lib "kernel32" () As Long
                        Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long

                        Now how do I close a file if it is found open by this code?
                        Code:
                        Dim xlBook As Excel.Workbook, xlApp As Excel.Application
                        Set xlBook = GetObject("Your Excel File Name")
                        Set xlApp = xlBook.Parent
                        xlBook.Save
                        xlBook.Saved = True
                        xlBook.Close
                        Set xlBook = Nothing
                        xlApp.Quit
                        Set xlApp = Nothing

                        Comment

                        • davnao
                          New Member
                          • May 2007
                          • 8

                          #13
                          Originally posted by JConsulting
                          Code:
                          Dim xlBook As Excel.Workbook, xlApp As Excel.Application
                          Set xlBook = GetObject("Your Excel File Name")
                          Set xlApp = xlBook.Parent
                          xlBook.Save
                          xlBook.Saved = True
                          xlBook.Close
                          Set xlBook = Nothing
                          xlApp.Quit
                          Set xlApp = Nothing
                          Thanks a bunch for all your help!

                          Comment

                          • JConsulting
                            Recognized Expert Contributor
                            • Apr 2007
                            • 603

                            #14
                            Originally posted by davnao
                            Thanks a bunch for all your help!
                            Happy to help. Let us know if you need any more help.
                            J

                            Comment

                            Working...