Help! Check to see if Excel file is Open

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 10
Reputation: snurd is an unknown quantity at this point 
Solved Threads: 0
snurd snurd is offline Offline
Newbie Poster

Help! Check to see if Excel file is Open

 
0
  #1
Aug 13th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

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

 
0
  #2
Aug 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 304
Reputation: aktharshaik is an unknown quantity at this point 
Solved Threads: 37
aktharshaik's Avatar
aktharshaik aktharshaik is offline Offline
Posting Whiz

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

 
0
  #3
Aug 14th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: snurd is an unknown quantity at this point 
Solved Threads: 0
snurd snurd is offline Offline
Newbie Poster

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

 
0
  #4
Aug 14th, 2008
Thank you so much "aktharshaik" it really work great.

snurd
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC