|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > kindly check what is wrong in checking an excel file open or not |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|