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.
13 17082
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 -
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
-
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.
[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.
[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?
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?
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
[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.
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 -
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
-
Here is an option capturing the errors -
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 -
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
-
and another if that fails to work for you -
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: - 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?
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?
-
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
-
-
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!
Thanks a bunch for all your help!
Happy to help. Let us know if you need any more help.
J
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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()
|
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,...
| |
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 >
...
|
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...
|
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...
|
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...
|
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 /...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |