943,917 Members | Top Members by Rank

Ad:
Aug 13th, 2008
0

Help! Check to see if Excel file is Open

Expand Post »
VB6.0
The following is how I open the Excel file, what I need to know is how to see if it is already open, and also a new Sub to close the file.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Command1_Click()
  2.  
  3. 'Place statement here to see if already open, if so then Goto 1
  4. Dim xlTemp As excel.Application
  5. Set xlTemp = New excel.Application
  6. xlTemp.Workbooks.Open "c:\Test.xls"
  7. xlTemp.Visible = True
  8. 1
  9.  
  10. End Sub
  11.  
  12. Private Sub Command2_Click()
  13.  
  14. 'This does not work
  15. xlTemp = nothing
  16. xlTemp.Quit
  17.  
  18. End Sub
Last edited by Tekmaven; Aug 14th, 2008 at 4:08 pm. Reason: Code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snurd is offline Offline
13 posts
since Aug 2008
Aug 14th, 2008
0

Re: Help! Check to see if Excel file is Open

Still more effective solution can be done.

First you may need to iterate through all the processes running in the memory to check whether at all if excel is running.

If it is not running then u may create the new instance of the excel application and open your file as usual.

if it is already running, then get the application handle to the excel application already open and check whether ur file to be opened is already opened or not. if yes then display "File Already Open" message, else open the file.

go for some googling at google.com for help regarding iterating through the running processes, getting the window handles of the applications and attaching them to objects, etc.

good luck.
U Work a Little, We Guide U a Little.
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008
Aug 14th, 2008
0

Re: Help! Check to see if Excel file is Open

Check Out this code. it may be of some help. i'll give still more effective code later on if u r not able to get what u need. try to use the logic from the sample code given here.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Private xlTemp As Excel.Application
  3. Private sFileName As String
  4.  
  5. Private Sub cmdOpen_Click()
  6.  
  7. On Error GoTo cmdOpen_Click_Error
  8.  
  9. Set xlTemp = New Excel.Application
  10. sFileName = "C:\TEST.XLS"
  11.  
  12. If Dir(sFileName) <> "" Then
  13. xlTemp.Workbooks.Open sFileName
  14. xlTemp.Visible = True
  15. Else
  16. MsgBox "File Not Found."
  17. End If
  18.  
  19. cmdOpen_Click_Done:
  20. Exit Sub
  21.  
  22. cmdOpen_Click_Error:
  23. MsgBox "An Error has occured in Procedure cmdOpen_Click." & vbCrLf & Err.Number & " : " & Err.Description & vbCrLf & vbCrLf & "Contact System Administrator."
  24. Resume cmdOpen_Click_Done
  25.  
  26. End Sub
  27.  
  28. Private Sub cmdClose_Click()
  29.  
  30. Dim ret As Integer
  31. Dim nFiles As Integer
  32. Dim iVar As Integer
  33.  
  34. On Error GoTo cmdClose_Click_Error
  35.  
  36. If Not xlTemp Is Nothing Then
  37. If xlTemp.Visible Then
  38. nFiles = xlTemp.Workbooks.Count
  39. For iVar = 1 To nFiles
  40. If UCase(xlTemp.Workbooks(iVar).Path & "\" & xlTemp.Workbooks(iVar).Name) = UCase(sFileName) Then
  41. xlTemp.Workbooks(iVar).Close
  42. If nFiles = 1 Then 'Confirm that no other workbooks are open and then quit
  43. xlTemp.Quit
  44. End If
  45. Exit For
  46. End If
  47. Next
  48. End If
  49. End If
  50.  
  51. cmdClose_Click_Done:
  52. Exit Sub
  53.  
  54. cmdClose_Click_Error:
  55. MsgBox "An Error has occured in Procedure cmdClose_Click." & vbCrLf & Err.Number & " : " & Err.Description & vbCrLf & vbCrLf & "Contact System Administrator."
  56. If Not xlTemp Is Nothing Then
  57. If xlTemp.Visible Then
  58. xlTemp.Workbooks.Close
  59. xlTemp.Quit
  60. End If
  61. End If
  62. Set xlTemp = Nothing
  63. Resume cmdClose_Click_Done
  64.  
  65. End Sub
Reputation Points: 26
Solved Threads: 40
Posting Whiz
aktharshaik is offline Offline
316 posts
since Aug 2008
Aug 14th, 2008
0

Re: Help! Check to see if Excel file is Open

Thank you so much "aktharshaik" it really work great.

snurd
Reputation Points: 10
Solved Threads: 0
Newbie Poster
snurd is offline Offline
13 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Openig an Excel File
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Excel Date/Time Format





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC