Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default Problem with handling on error when file is already open

I have tried to make a event handler to view a warning message, but before it
displayed, this system message pops up first: "<Filename is already open.
Reopening will cause any changes you made to be discarded. Do you want to
reopen <filename?"

How can I avoid the system message and only display my own message?

Regards

Frank Krogh

_________________________

Option Explicit
Sub ChangeDateFormat()

Dim strFileName As Variant
Dim wkbk As Workbook
Dim showMsg As String

strFileName = Application.GetOpenFilename("Report (*.xls),*.xls")
'Application.ScreenUpdating = False
If strFileName < False Then
On Error GoTo ErrorHandler ' Enable error-handling routine.
Set wkbk = Workbooks.Open(Filename:=strFileName)
With wkbk.Worksheets(1)
' Do something
End With
End If
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
showMsg = MsgBox("File is already open. Please close and try again",
vbExclamation, "File already open")
Exit Sub

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default Problem with handling on error when file is already open

Frank, "Set" the workbook to your variable before opening, then test to
see if it is open.
On Error Resume Next
Set wkbk = Workbooks(strFileName)
If Not wkbk Is Nothing Then
'Trigger Msg
Else
'Not Open
End If

HTH

Charles Chickering
Frank wrote:
I have tried to make a event handler to view a warning message, but before it
displayed, this system message pops up first: "<Filename is already open.
Reopening will cause any changes you made to be discarded. Do you want to
reopen <filename?"

How can I avoid the system message and only display my own message?

Regards

Frank Krogh

_________________________

Option Explicit
Sub ChangeDateFormat()

Dim strFileName As Variant
Dim wkbk As Workbook
Dim showMsg As String

strFileName = Application.GetOpenFilename("Report (*.xls),*.xls")
'Application.ScreenUpdating = False
If strFileName < False Then
On Error GoTo ErrorHandler ' Enable error-handling routine.
Set wkbk = Workbooks.Open(Filename:=strFileName)
With wkbk.Worksheets(1)
' Do something
End With
End If
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
showMsg = MsgBox("File is already open. Please close and try again",
vbExclamation, "File already open")
Exit Sub

End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 170
Default Problem with handling on error when file is already open

Thank you for the solution. It worked fine.

"Die_Another_Day" wrote:

Frank, "Set" the workbook to your variable before opening, then test to
see if it is open.
On Error Resume Next
Set wkbk = Workbooks(strFileName)
If Not wkbk Is Nothing Then
'Trigger Msg
Else
'Not Open
End If

HTH

Charles Chickering
Frank wrote:
I have tried to make a event handler to view a warning message, but before it
displayed, this system message pops up first: "<Filename is already open.
Reopening will cause any changes you made to be discarded. Do you want to
reopen <filename?"

How can I avoid the system message and only display my own message?

Regards

Frank Krogh

_________________________

Option Explicit
Sub ChangeDateFormat()

Dim strFileName As Variant
Dim wkbk As Workbook
Dim showMsg As String

strFileName = Application.GetOpenFilename("Report (*.xls),*.xls")
'Application.ScreenUpdating = False
If strFileName < False Then
On Error GoTo ErrorHandler ' Enable error-handling routine.
Set wkbk = Workbooks.Open(Filename:=strFileName)
With wkbk.Worksheets(1)
' Do something
End With
End If
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
showMsg = MsgBox("File is already open. Please close and try again",
vbExclamation, "File already open")
Exit Sub

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 415
Default Problem with handling on error when file is already open

Frank,
Or if you that someone else has the file open, you can try to open it
Exclusive first.
If that fails, you know it is already open.

Dim FileNum as long
dim MyFile as string
dim WB as workbook

myfile="C:\Whatever.xls"
Filenum=freefile
on error resume next
Open MyFile For Input Read Lock As FileNum

Select case err.number
case 0
'OK, so close and open normally
close filenum
Set WB=Workbooks.open(myfile)
case ?? 'Forget the number fpor a locked file, but you can test

case else
'Decide what to do
end select

Or maybe you can call a function in this
http://www.dr-hoiby.com/WhoLockMe/index.php

Haven't used it so can't tell you much.


NickHK

"Frank" ...
I have tried to make a event handler to view a warning message, but before
it
displayed, this system message pops up first: "<Filename is already open.
Reopening will cause any changes you made to be discarded. Do you want to
reopen <filename?"

How can I avoid the system message and only display my own message?

Regards

Frank Krogh

_________________________

Option Explicit
Sub ChangeDateFormat()

Dim strFileName As Variant
Dim wkbk As Workbook
Dim showMsg As String

strFileName = Application.GetOpenFilename("Report (*.xls),*.xls")
'Application.ScreenUpdating = False
If strFileName < False Then
On Error GoTo ErrorHandler ' Enable error-handling routine.
Set wkbk = Workbooks.Open(Filename:=strFileName)
With wkbk.Worksheets(1)
' Do something
End With
End If
Exit Sub ' Exit to avoid handler.
ErrorHandler: ' Error-handling routine.
showMsg = MsgBox("File is already open. Please close and try again",
vbExclamation, "File already open")
Exit Sub

End Sub




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Handling - Check a file isn't already open before overwritin NDBC Excel Discussion (Misc queries) 4 August 13th 09 08:36 AM
Error handling problem Jim G Excel Discussion (Misc queries) 2 October 3rd 07 02:19 AM
workbooks.open and error handling John Keith[_2_] Excel Programming 5 August 18th 05 05:16 PM
Error Handling problem Brassman[_5_] Excel Programming 3 May 24th 05 03:43 PM
Error Handling Problem Minitman[_4_] Excel Programming 3 November 15th 04 06:52 AM


All times are GMT +1. The time now is 11:25 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"