ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Alerting user if file not open (https://www.excelbanter.com/excel-programming/372044-alerting-user-if-file-not-open.html)

Matilda

Alerting user if file not open
 
Dear Wise Ones,

I am wanting to prompt users to open the required file if not already open.
There is some nearly-helpful answers in these posts, but I can't quite
achieve what I want.

This code of mine is useless because errorMsgs executes whether error or not.

Set rotaBk = Workbooks("EAA.xls")
rotaBk.Activate
On Error GoTo errorMsgs
errorMsgs:
MsgBox ("Open EAA.xls ")
Exit Sub


If I try to put a condition on error handling execution:

If error then
MsgBox ("Open EAA.xls " & error)
exit sub
end if

I get a "type mismatch" in the error MsgBox

As always, help most appreciated,

matilda

Norman Jones

Alerting user if file not open
 
Hi Matilda,

Try something like:

'=============
Public Sub Tester()
Dim rotaBk As Workbook
Const myPath As String = "C\:Data\" '<<==== CHANGE

On Error Resume Next
Set rotaBk = Workbooks("EAA.xls")
On Error GoTo 0

If rotaBk Is Nothing Then
Set rotaBk = Workbooks.Open _
(Filename:=myPath & "EAA.xls")
End If
End Sub
'<<=============


---
Regards,
Norman



"Matilda" wrote in message
...
Dear Wise Ones,

I am wanting to prompt users to open the required file if not already
open.
There is some nearly-helpful answers in these posts, but I can't quite
achieve what I want.

This code of mine is useless because errorMsgs executes whether error or
not.

Set rotaBk = Workbooks("EAA.xls")
rotaBk.Activate
On Error GoTo errorMsgs
errorMsgs:
MsgBox ("Open EAA.xls ")
Exit Sub


If I try to put a condition on error handling execution:

If error then
MsgBox ("Open EAA.xls " & error)
exit sub
end if

I get a "type mismatch" in the error MsgBox

As always, help most appreciated,

matilda




Matilda

Alerting user if file not open
 
Thanks for your suggestion, Norman.

I would adopt it word for word except that the path is not going to be
fixed, and the file can be in one of three folders (I have no control over
that)

Trying to help users all I can... all I can do is tell them that required
folders are not open at the outset, so they can go away, find the version
they want, open it and start again.

tried this:

On Error Resume Next
Set wbk = Workbooks("myFile.xls")
On Error Resume Next
wbk.Activate
On Error GoTo 0

If wbk Is Nothing Then
MsgBox ("Open myFile.xls and restart procedure ")
Exit Sub
End If

with the file open, and again with the file closed.
The error seems to be ignored and code resumes without the prompt if file
is actually closed.

Thanks for your patience,

Matilda



"Norman Jones" wrote:

Hi Matilda,

Try something like:

'=============
Public Sub Tester()
Dim rotaBk As Workbook
Const myPath As String = "C\:Data\" '<<==== CHANGE

On Error Resume Next
Set rotaBk = Workbooks("EAA.xls")
On Error GoTo 0

If rotaBk Is Nothing Then
Set rotaBk = Workbooks.Open _
(Filename:=myPath & "EAA.xls")
End If
End Sub
'<<=============


---
Regards,
Norman



"Matilda" wrote in message
...
Dear Wise Ones,

I am wanting to prompt users to open the required file if not already
open.
There is some nearly-helpful answers in these posts, but I can't quite
achieve what I want.

This code of mine is useless because errorMsgs executes whether error or
not.

Set rotaBk = Workbooks("EAA.xls")
rotaBk.Activate
On Error GoTo errorMsgs
errorMsgs:
MsgBox ("Open EAA.xls ")
Exit Sub


If I try to put a condition on error handling execution:

If error then
MsgBox ("Open EAA.xls " & error)
exit sub
end if

I get a "type mismatch" in the error MsgBox

As always, help most appreciated,

matilda





Norman Jones

Alerting user if file not open
 
Hi Matilda,

Perhaps try something like:
'=============
Public Sub Tester()
Dim rotaBk As Workbook
Dim FName As Variant
Const myPath As String = "C\:Data\" '<<==== CHANGE

On Error Resume Next
Set rotaBk = Workbooks("EAA.xls")
On Error GoTo 0

If rotaBk Is Nothing Then
FName = Application.GetOpenFilename( _
FileFilter:="File Excel (*.xls),*.xls", _
Title:="SelectFile")

If FName < False Then
Set rotaBk = Workbooks.Open(FName)
Else
MsgBox "No file selected!"
'do something?
End If
End If
End Sub
'<<=============



---
Regards,
Norman


"Matilda" wrote in message
...
Thanks for your suggestion, Norman.

I would adopt it word for word except that the path is not going to be
fixed, and the file can be in one of three folders (I have no control over
that)

Trying to help users all I can... all I can do is tell them that required
folders are not open at the outset, so they can go away, find the version
they want, open it and start again.

tried this:

On Error Resume Next
Set wbk = Workbooks("myFile.xls")
On Error Resume Next
wbk.Activate
On Error GoTo 0

If wbk Is Nothing Then
MsgBox ("Open myFile.xls and restart procedure ")
Exit Sub
End If

with the file open, and again with the file closed.
The error seems to be ignored and code resumes without the prompt if file
is actually closed.

Thanks for your patience,

Matilda



"Norman Jones" wrote:

Hi Matilda,

Try something like:

'=============
Public Sub Tester()
Dim rotaBk As Workbook
Const myPath As String = "C\:Data\" '<<==== CHANGE

On Error Resume Next
Set rotaBk = Workbooks("EAA.xls")
On Error GoTo 0

If rotaBk Is Nothing Then
Set rotaBk = Workbooks.Open _
(Filename:=myPath & "EAA.xls")
End If
End Sub
'<<=============


---
Regards,
Norman



"Matilda" wrote in message
...
Dear Wise Ones,

I am wanting to prompt users to open the required file if not already
open.
There is some nearly-helpful answers in these posts, but I can't quite
achieve what I want.

This code of mine is useless because errorMsgs executes whether error
or
not.

Set rotaBk = Workbooks("EAA.xls")
rotaBk.Activate
On Error GoTo errorMsgs
errorMsgs:
MsgBox ("Open EAA.xls ")
Exit Sub


If I try to put a condition on error handling execution:

If error then
MsgBox ("Open EAA.xls " & error)
exit sub
end if

I get a "type mismatch" in the error MsgBox

As always, help most appreciated,

matilda







Norman Jones

Alerting user if file not open
 
Hi Matilda,

Const myPath As String = "C\:Data\" '<<==== CHANGE


I should have deleted the above, now redundant, code line.


--
Regards,
Norman



Matilda

Alerting user if file not open
 
Hi Norman,

That's clever!

Opening the dialog for users to choose a file solves the problem, in a way I
hadn't thought of.

Many, many thanks.

Matilda

"Norman Jones" wrote:

Hi Matilda,

Const myPath As String = "C\:Data\" '<<==== CHANGE


I should have deleted the above, now redundant, code line.


--
Regards,
Norman





All times are GMT +1. The time now is 03:09 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com