473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Excel file open and read/write to it

--------------------------------------------------------------------------------

Hi all,

How can I check if I have an excel speadsheet already open? and if it is, how do I read write from it straight away?

All I have managed to do is ensure that the excel spreadsheet is closed then open it again with my vb app.

Please help
May 20 '07 #1
3 9915
Dököll
2,364 Expert 2GB
--------------------------------------------------------------------------------

Hi all,

How can I check if I have an excel speadsheet already open? and if it is, how do I read write from it straight away?

All I have managed to do is ensure that the excel spreadsheet is closed then open it again with my vb app.

Please help
Hello, Rob!

Found this bit of code interesting and close to what you need, se what it does for you. I did not write this, nor have I tried it:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit 
  3.  
  4. Sub AutomateExcelFromWord()
  5. Dim MSG, Style, Response
  6. Dim Filename As String
  7. Dim SuggestedName As String
  8. Dim oExcel As Excel.Application
  9. Set oExcel = New Excel.Application
  10. Dim oWB As Excel.Workbook
  11. Dim oWS As Excel.Worksheet
  12. Dim oRng1 As Excel.Range
  13. Dim oRng2 As Excel.Range
  14.  
  15. Set oWB = oExcel.Workbooks.Add
  16. Set oWS = oWB.Worksheets("Sheet1")
  17.  
  18. Set oRng1 = oWS.Range("A1")
  19. Set oRng2 = oWS.Range("B1")
  20. oExcel.Visible = False
  21.  
  22. SuggestedName = "Hello"
  23. Filename = oExcel.GetSaveAsFilename("C:\" & SuggestedName & ".xls", _
  24. "WorkBook (*.xls), *.xls", , "Select or enter a File Name:")
  25. If Filename = "False" Then Exit Sub
  26.  
  27. 'These next few lines inform you if the file already exists.
  28. If (Len(Dir$(Filename)) > 0) Then
  29.            Style = vbYesNo + vbExclamation
  30.            MSG = "The file already exists," & vbCrLf & _
  31.            "would you like to overwrite it?"
  32.            Response = MsgBox(MSG, Style)
  33.     If Response = vbNo Then
  34.            GoTo Cleanup
  35.     End If
  36. End If
  37.  
  38. 'Below is the bit that determines if the file is open.
  39. On Error GoTo ErrorHandler
  40.     Open Filename For Binary Access _
  41.            Read Write Lock Read Write As #1
  42.     Close #1
  43. On Error GoTo 0
  44.  
  45. oRng1.Value = "Hello"
  46. oRng2.Value = "GoodBye"
  47.  
  48. oExcel.DisplayAlerts = False 'overwrite existing file without prompt
  49. oWB.SaveAs Filename
  50.  
  51. Cleanup:
  52.     Call oWB.Close(SaveChanges:=False)
  53.     oExcel.DisplayAlerts = True
  54.     oExcel.Quit
  55.     Set oWB = Nothing
  56.     Set oWS = Nothing
  57.     Set oExcel = Nothing
  58.  
  59. Exit Sub
  60. ErrorHandler:
  61.            MsgBox "E R R O R - The file that your are trying to access," _
  62.            & vbCrLf & "is already open." & vbCrLf & vbCrLf & _
  63.            "Please close the file and try again", vbCritical
  64. GoTo Cleanup
  65. End Sub
  66.  
  67.  
Let me know if it does not work, should have it removed from here. Cannot run anything on here as of now...
May 20 '07 #2
Hello, Rob!

Found this bit of code interesting and close to what you need, se what it does for you. I did not write this, nor have I tried it:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Option Explicit 
  3.  
  4. Sub AutomateExcelFromWord()
  5. Dim MSG, Style, Response
  6. Dim Filename As String
  7. Dim SuggestedName As String
  8. Dim oExcel As Excel.Application
  9. Set oExcel = New Excel.Application
  10. Dim oWB As Excel.Workbook
  11. Dim oWS As Excel.Worksheet
  12. Dim oRng1 As Excel.Range
  13. Dim oRng2 As Excel.Range
  14.  
  15. Set oWB = oExcel.Workbooks.Add
  16. Set oWS = oWB.Worksheets("Sheet1")
  17.  
  18. Set oRng1 = oWS.Range("A1")
  19. Set oRng2 = oWS.Range("B1")
  20. oExcel.Visible = False
  21.  
  22. SuggestedName = "Hello"
  23. Filename = oExcel.GetSaveAsFilename("C:\" & SuggestedName & ".xls", _
  24. "WorkBook (*.xls), *.xls", , "Select or enter a File Name:")
  25. If Filename = "False" Then Exit Sub
  26.  
  27. 'These next few lines inform you if the file already exists.
  28. If (Len(Dir$(Filename)) > 0) Then
  29.            Style = vbYesNo + vbExclamation
  30.            MSG = "The file already exists," & vbCrLf & _
  31.            "would you like to overwrite it?"
  32.            Response = MsgBox(MSG, Style)
  33.     If Response = vbNo Then
  34.            GoTo Cleanup
  35.     End If
  36. End If
  37.  
  38. 'Below is the bit that determines if the file is open.
  39. On Error GoTo ErrorHandler
  40.     Open Filename For Binary Access _
  41.            Read Write Lock Read Write As #1
  42.     Close #1
  43. On Error GoTo 0
  44.  
  45. oRng1.Value = "Hello"
  46. oRng2.Value = "GoodBye"
  47.  
  48. oExcel.DisplayAlerts = False 'overwrite existing file without prompt
  49. oWB.SaveAs Filename
  50.  
  51. Cleanup:
  52.     Call oWB.Close(SaveChanges:=False)
  53.     oExcel.DisplayAlerts = True
  54.     oExcel.Quit
  55.     Set oWB = Nothing
  56.     Set oWS = Nothing
  57.     Set oExcel = Nothing
  58.  
  59. Exit Sub
  60. ErrorHandler:
  61.            MsgBox "E R R O R - The file that your are trying to access," _
  62.            & vbCrLf & "is already open." & vbCrLf & vbCrLf & _
  63.            "Please close the file and try again", vbCritical
  64. GoTo Cleanup
  65. End Sub
  66.  
  67.  
Let me know if it does not work, should have it removed from here. Cannot run anything on here as of now...
Thanks for the code listing, it's not exactly what I needed, however, very useful and it pointed me in the right direction.

Again Thanks
May 25 '07 #3
Thanks for the code listing, it's not exactly what I needed, however, very useful and it pointed me in the right direction.

Again Thanks


If thats does not help try this


http://www.gemboxsoftware.com/GBSpre...FSV0hgodM1UBKQ
May 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Greener | last post by:
In addition to the previous questions, I wonder, if opening an Excel file in NON-read-only mode, would the macros on the worksheets work as well? Thanks millions! Helena
8
by: Phoenix | last post by:
Here's a challenge that is killing me: I've got 2 web servers and a SQL Server and about 5,000 'users' who stay connected to the site all day. I have a page that is supposed to be 'real-time',...
0
by: HAS | last post by:
Intermittently I am experiencing a problem where an excel file I open is opened read only because it claims to be locked. I have gone into task manager and killed all running Excel, I can open the...
0
by: Server Control | last post by:
Hi, I am exporting my datagrid to Excel using the standard Export to Excel Method using : dg.RenderControl(). and Page.Response.AddHeader("Content-Disposition", "attachment;filename=" +...
5
by: barnetod | last post by:
I am trying to open a text file designated by the user. Then I want to change all lower case values to capital letters. Then write file. I am stuck and can not change the characters or am...
2
by: momukhtar | last post by:
I am using Win32::OLE to write a perl script which opens an excel file. That excel file is password protected and everytime i run that script dialog box pops up and I have to click on Read-Only then...
1
Xx r3negade
by: Xx r3negade | last post by:
Sorry for the extreme newbie question, but I honestly can't find this anywhere else. When I do f = open('/var/www/some_file', 'r+') it is supposed to open the file for reading and writing,...
3
by: Mel | last post by:
How do I open an Excel file as a read only document? It would be okay if it opened in the browser window but I don't want the user to save any changes and stomp over my original file on the...
0
by: Brian Pinto | last post by:
Im facing a problem with file stream read/write for a usb device. The code works fine on XP (32 bit). but fails to work on Windows 7 (64 bit). I'm successfully past problem of getting the device to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.