Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Opening a workbook if not opened, going to it if already opened


Anyone know how to do this function?

Basically, if a workbook is not opened yet, then open it.
If its already opened, then go to that window.

I got this from the web, but if the workbook is already opened, i
gives me an error :
Function WorkbookOpen(WorkBookName As String) As Boolean
'Returns TRUE if the workbook is open
Dim myxl As Excel.Application

On Error Resume Next
Set myxl = GetObject(, "Excel.Application")
If Err.Number < 0 Then
Err.Clear
WorkbookOpen = False
Else
WorkbookOpen = True
End If
Set myxl = Nothing

End Function


How I call the function :
Dim strSourceFilename As String
strSourceFilename = Range("A1").Text
If Not WorkbookOpen(strSourceFilename) = False Then
Application.Workbooks.Open FileName:=strSourceFilename
End If


Thanks for any help

--
neotoky
-----------------------------------------------------------------------
neotokyo's Profile: http://www.excelforum.com/member.php...fo&userid=1389
View this thread: http://www.excelforum.com/showthread.php?threadid=26327

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Opening a workbook if not opened, going to it if already opened

Dim wkbk as Workbook
On error resume next
set wkbk = Workbooks("ABC.xls")
On error goto 0
if wkbk is nothing then
set wkbk = Workbooks.Open("C:\Myfiles\ABC.xls")
End if
wkbk.Activate

--
Regards,
Tom Ogilvy


"neotokyo" wrote in message
...

Anyone know how to do this function?

Basically, if a workbook is not opened yet, then open it.
If its already opened, then go to that window.

I got this from the web, but if the workbook is already opened, it
gives me an error :
Function WorkbookOpen(WorkBookName As String) As Boolean
'Returns TRUE if the workbook is open
Dim myxl As Excel.Application

On Error Resume Next
Set myxl = GetObject(, "Excel.Application")
If Err.Number < 0 Then
Err.Clear
WorkbookOpen = False
Else
WorkbookOpen = True
End If
Set myxl = Nothing

End Function


How I call the function :
Dim strSourceFilename As String
strSourceFilename = Range("A1").Text
If Not WorkbookOpen(strSourceFilename) = False Then
Application.Workbooks.Open FileName:=strSourceFilename
End If


Thanks for any help!


--
neotokyo
------------------------------------------------------------------------
neotokyo's Profile:

http://www.excelforum.com/member.php...o&userid=13892
View this thread: http://www.excelforum.com/showthread...hreadid=263271



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
Is there away to keep Excel from opening a copy of an already opened workbook and the pages not be at 100% but Marc New Users to Excel 1 March 13th 06 01:51 PM
How to run a macro as soon a a workbook (.xls) is opened? Sangamesh Acharya Excel Worksheet Functions 1 December 15th 05 11:15 PM
How to see if the opened workbook is opened by another user ? balexis Excel Programming 1 August 18th 04 04:11 PM
Sheets select method fails when workbook is opened by another workbook Mike Excel Programming 2 June 8th 04 04:17 AM
opening diff. worksheets in opened workbook AlexD Excel Programming 3 April 22nd 04 08:23 PM


All times are GMT +1. The time now is 10:30 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"