473,480 Members | 3,796 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Check if Workbook is open and close if it is

8 New Member
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.
May 23 '07 #1
13 17082
JConsulting
603 Recognized Expert Contributor
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

Expand|Select|Wrap|Line Numbers
  1. Function fFileOpen(strFile As String) As Boolean
  2. On Error Resume Next
  3.  
  4.     Dim intFile As Integer
  5.     If Dir(strFile) = "" Then Exit Function
  6.  
  7.     intFile = FreeFile()
  8.     Open strFile For Input Lock Read As intFile
  9.     Close intFile
  10.  
  11.     If err <> 0 Then
  12.         fFileOpen = True
  13.     End If
  14.  
  15. End Function
  16.  
May 23 '07 #2
JConsulting
603 Recognized Expert Contributor
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.
May 23 '07 #3
davnao
8 New Member
[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.
May 23 '07 #4
JConsulting
603 Recognized Expert Contributor
[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?
May 23 '07 #5
davnao
8 New Member
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?
May 23 '07 #6
JConsulting
603 Recognized Expert Contributor
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
May 23 '07 #7
davnao
8 New Member
[quote=JConsulting]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.
May 23 '07 #8
JConsulting
603 Recognized Expert Contributor
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

Expand|Select|Wrap|Line Numbers
  1. Sub TestExcel(strFile)as boolean
  2.     Dim intFreeFile As Integer
  3.  
  4.     On Error Resume Next
  5.     intFreeFile = FreeFile
  6.     Open strFile For Input Lock Read As #intFreeFile
  7.     Select Case Err.Number
  8.         Case 70
  9.             MsgBox "This file is already in use"
  10.                  TestExcel = true
  11.         Case 0
  12.             Close #intFreeFile
  13.         Case Else
  14.             '   Some other error
  15.     End Select
  16.  
  17. End Sub
  18.  
May 23 '07 #9
JConsulting
603 Recognized Expert Contributor
Here is an option capturing the errors

Expand|Select|Wrap|Line Numbers
  1. Sub TestExcel(strFile)as boolean
  2.     Dim intFreeFile As Integer
  3.  
  4.     On Error Resume Next
  5.     intFreeFile = FreeFile
  6.     Open strFile For Input Lock Read As #intFreeFile
  7.     Select Case Err.Number
  8.         Case 70
  9.             MsgBox "This file is already in use"
  10.                  TestExcel = true
  11.         Case 0
  12.             Close #intFreeFile
  13.         Case Else
  14.             '   Some other error
  15.     End Select
  16.  
  17. End Sub
  18.  

and another if that fails to work for you

Expand|Select|Wrap|Line Numbers
  1. Private Function IsFileAlreadyOpen(Filename As String) As Boolean
  2. '   Returns TRUE if the workbook is open
  3.   Dim x As Workbook
  4.   On Error Resume Next
  5.   Set x = Workbooks("c:\test.xls")
  6.   If Err = 0 Then IsFileAlreadyOpen = True _
  7.       Else IsFileAlreadyOpen = False
  8. End Function
  9.  
May 23 '07 #10
davnao
8 New Member
and another if that fails to work for you

Expand|Select|Wrap|Line Numbers
  1. Private Function IsFileAlreadyOpen(Filename As String) As Boolean
  2. '   Returns TRUE if the workbook is open
  3.   Dim x As Workbook
  4.   On Error Resume Next
  5.   Set x = Workbooks("c:\test.xls")
  6.   If Err = 0 Then IsFileAlreadyOpen = True _
  7.       Else IsFileAlreadyOpen = False
  8. End Function
  9.  
Pasted your code with the following in a new module and it worked:

Expand|Select|Wrap|Line Numbers
  1. Private Declare Function lopen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
  2. Private Declare Function GetLastError Lib "kernel32" () As Long
  3. 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?
May 24 '07 #11
JConsulting
603 Recognized Expert Contributor
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?
Expand|Select|Wrap|Line Numbers
  1. Dim xlBook As Excel.Workbook, xlApp As Excel.Application
  2. Set xlBook = GetObject("Your Excel File Name")
  3. Set xlApp = xlBook.Parent
  4. xlBook.Save
  5. xlBook.Saved = True
  6. xlBook.Close
  7. Set xlBook = Nothing
  8. xlApp.Quit
  9. Set xlApp = Nothing
  10.  
May 24 '07 #12
davnao
8 New Member
Expand|Select|Wrap|Line Numbers
  1. Dim xlBook As Excel.Workbook, xlApp As Excel.Application
  2. Set xlBook = GetObject("Your Excel File Name")
  3. Set xlApp = xlBook.Parent
  4. xlBook.Save
  5. xlBook.Saved = True
  6. xlBook.Close
  7. Set xlBook = Nothing
  8. xlApp.Quit
  9. Set xlApp = Nothing
  10.  
Thanks a bunch for all your help!
May 24 '07 #13
JConsulting
603 Recognized Expert Contributor
Thanks a bunch for all your help!
Happy to help. Let us know if you need any more help.
J
May 24 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2800
by: Robin Tucker | last post by:
Just a quick question about connection management. My application will never need more than 1 or 2 connections about at any given time. Also, I do not expect many users to be connected at any...
4
2453
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring...
2
1549
by: Maziar Aflatoun | last post by:
Hi, To update the database I first open a connection, update and then I close it. Is there way to do this without opening the connection first? Such as using the DataAdaptor (which manages the...
1
2536
by: Sorin Schwimmer | last post by:
Hi, I tried for the first time a Tix Tree, so, if my question is naive, I apologize upfront. The following code: <code> from Tix import * r=Tk()
1
2108
by: yduani | last post by:
Hi, I'm running 2 different executables: One saving infinitely an excell file, Other Read it infinitely. time duration for Save/Open is randomally. This 2 applications works fine differently,...
2
2394
by: jy43 | last post by:
I am writing a method for a web service that returns an empty element.(not null) The SOAP generate the response XML message using shorthand format for the empty return object. < soap:Body > ...
4
3412
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I am currently developing my first webapplication using .net (2.x) In a couple examples seen in the helpfile that came with VS2008 i see that they are opening a connection to the sql...
2
12901
by: Nitinkcv | last post by:
Hi all, I have a div which can be closed and opened on click of a section. Now what i would like to do is that on open and close the div should have a sliding effect. Normally what we would do...
0
1557
by: foltac | last post by:
We run a database replication process that ultimately puts a new copy of an Access DB in a new folder (on the server harddisk) every night. The folder is programmatically named and includes the...
1
1893
by: sierra jones | last post by:
So guys, I was just wondering if there was a code I could get from anyone that would Open + Close a .bat or .cmd file? Not ALL .bat / .cmd files that are open, but just ONE (runserver.bat /...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4478
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.