Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old September 16th, 2004, 02:09 AM
kiran_karnati kiran_karnati is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Braunschweig , Germany
Posts: 39 kiran_karnati User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 5 sec
Reputation Power: 4
Send a message via Yahoo to kiran_karnati
Unhappy kindly check what is wrong in checking an excel file open or not

halloo everybody
Here is my code to check an excel sheet wether it is open or not.
It is working good in the following situations.

1.if the excel file is not yet opened. then it is opening and bringing a value from some cell of the sheet1.
2.If the file is already opened and not yet closed. it is not opening again it is understanding that the file is already openrd and bringing the cell value.
3.It is working when it is closed by the user and again clicked the button to open. It is opening and bringing the value.
4.NOW the problem starts.
IT IS NOT WORKING , If I click the button again to open. It is opening another copy of the same file. I did't understand why is it working perfectly? before I close the file and not working if I close the File.

please kindly check my code and respond to me .
here is my code but it is not working when the file is already opened.


Private sub Cmd_Click()

Dim XL As New Excel.Application
Dim wbk As New Excel.Workbook
Dim ws As New Excel.Worksheet


WorkBookName = "Book2.xls"
If Not WorkbookOpen(WorkBookName) Then
chk = 1
Set wbk = XL.Workbooks.Open("C:\Dokumente und Einstellungen\Kiran Karnati\Desktop\EXCEL\Book2.xls")
Else
Set wbk = Workbooks(WorkBookName)
End If

Set ws = wbk.Worksheets("Sheet1")
If chk = 0 Then
With ws
Label48.Caption = .Cells(1, 2).Value
.Cells(1, 3).Select
End With
Else
With ws
Text49.Value = .Cells(1, 2).Value
End With
End If
XL.Visible = True

Set SA = Nothing
Set XL = Nothing
Set wbk = Nothing
End Sub
_________________________________________________


Function WorkbookOpen(WorkBookName As String) As Boolean
'Returns TRUE if the workbook is open
WorkbookOpen = False
On Error GoTo WorkBookNotOpen
If Len(Excel.Workbooks(WorkBookName).Name) > 0 Then
WorkbookOpen = True
Exit Function
End If

WorkBookNotOpen:
End Function

Thank you.
Kiran.

Reply With Quote
  #2  
Old September 17th, 2004, 07:30 AM
kiran_karnati kiran_karnati is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: Braunschweig , Germany
Posts: 39 kiran_karnati User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 5 sec
Reputation Power: 4
Send a message via Yahoo to kiran_karnati
Really am very happy to say that am a member of this forum.

I used that technique that you gave and it was working good if I don't close the file but If the user closes the file and tries to open again by clicking the button then the GETOBJECT() function is not generating any error even the file is not yet opened, so I have used your technique and also did something else to make it work and I want to post my solution here so that no one else will sufer for this problem again.
Any way if you find that this is bad and any other technique is also possible pelase help me, but at the moment it is working .
Thank you and here is my solution.
Code:

Private Sub command47_Click()

Dim XL As New Excel.Application
Dim wbk As New Excel.Workbook
Dim ws As Excel.Worksheet
Dim WorkBookName As String
Dim chk As Integer
Dim bool_wkbPresent As Boolean

chk = 0


WorkBookName = "Book2.xls"

If Not WorkbookOpen(WorkBookName) Then
chk = 1
Set wbk = XL.Workbooks.Open("C:\Dokumente und Einstellungen\Kiran Karnati\Desktop\EXCEL\Book2.xls")
Else
On Error GoTo openExcel
DoCmd.SetWarnings False
Set wbk = GetObject("C:\Dokumente und Einstellungen\Kiran Karnati\Desktop\EXCEL\Book2.xls")
End If

Work_On_Worksheet:
Set ws = wbk.Worksheets("Sheet1")
If chk = 0 Then
With ws
Label48.Caption = .Cells(1, 2).Value
.Cells(1, 3).Select
End With
Else
With ws
Text49.Value = .Cells(1, 2).Value
.Cells(1, 6).Select
End With
XL.Visible = True
End If
GoTo Exit_Work

openExcel:
chk = 1
Set wbk = XL.Workbooks.Open("C:\Dokumente und Einstellungen\Kiran Karnati\Desktop\EXCEL\Book2.xls")
Resume Work_On_Worksheet


Exit_Work:
wbk.Activate
ws.Visible = xlSheetVisible
'Releasing the SA and Xl Objects is wise TODO
Set SA = Nothing
Set XL = Nothing
Set wbk = Nothing
DoCmd.SetWarnings True
End Sub

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



Have fun there
Kiran Karnati

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > kindly check what is wrong in checking an excel file open or not


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway