Excel how to tell if workbook is already open BEFORE you try to open it.

I've been looking all over the net trying to find C# code to determine
if a workbook is open before you try to open it so you can alert the
user to close it before you update it.

There must be a way.  Please help.

hd
0
hdjim
5/3/2010 8:57:29 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

5 Replies
936 Views

Similar Articles

[PageSpeed] 54

"hdjim" <hdjim69@yahoo.com> wrote in message 
news:121fe4a0-138e-497f-b6b7-32b4b3f9ef7a@o14g2000yqb.googlegroups.com...

> I've been looking all over the net trying to find C# code to determine
> if a workbook is open before you try to open it so you can alert the
> user to close it before you update it.
>
> There must be a way.  Please help.

The easiest way is to try to open the file for writing and see if the 
operation fails (i.e., trap the exception). 


0
Jeff
5/3/2010 9:17:18 PM
Jeff Johnson wrote:
> "hdjim" <hdjim69@yahoo.com> wrote in message 
> news:121fe4a0-138e-497f-b6b7-32b4b3f9ef7a@o14g2000yqb.googlegroups.com...
> 
>> I've been looking all over the net trying to find C# code to determine
>> if a workbook is open before you try to open it so you can alert the
>> user to close it before you update it.
>>
>> There must be a way.  Please help.
> 
> The easiest way is to try to open the file for writing and see if the 
> operation fails (i.e., trap the exception). 

When you successfully open a file for writing, doesn't that delete the 
existing contents?
0
Harlan
5/3/2010 9:32:38 PM
"Harlan Messinger" <hmessinger.removethis@comcast.net> wrote in message 
news:848tnmF251U1@mid.individual.net...

>>> I've been looking all over the net trying to find C# code to determine
>>> if a workbook is open before you try to open it so you can alert the
>>> user to close it before you update it.
>>>
>>> There must be a way.  Please help.
>>
>> The easiest way is to try to open the file for writing and see if the 
>> operation fails (i.e., trap the exception).
>
> When you successfully open a file for writing, doesn't that delete the 
> existing contents?

No, depending on the mode you use to open. Think about it: how could you 
ever append to a file if that were the case?

I guess you could also try to open for read with a ShareDenyRead lock. 


0
Jeff
5/3/2010 10:15:08 PM
Jeff Johnson wrote:
> "Harlan Messinger" <hmessinger.removethis@comcast.net> wrote in message 
> news:848tnmF251U1@mid.individual.net...
> 
>>>> I've been looking all over the net trying to find C# code to determine
>>>> if a workbook is open before you try to open it so you can alert the
>>>> user to close it before you update it.
>>>>
>>>> There must be a way.  Please help.
>>> The easiest way is to try to open the file for writing and see if the 
>>> operation fails (i.e., trap the exception).
>> When you successfully open a file for writing, doesn't that delete the 
>> existing contents?
> 
> No, depending on the mode you use to open. Think about it: how could you 
> ever append to a file if that were the case?

OK, fine, for appending. :-) The terminology is tricky--sometimes when 
they say "write" they mean, as opposed to "append".
> 
> I guess you could also try to open for read with a ShareDenyRead lock. 
> 
> 
0
Harlan
5/3/2010 11:16:03 PM
"Harlan Messinger" <h.usenetremoverthis@gavelcade.com> wrote in message 
news:8493ptF3mrU1@mid.individual.net...

>>> When you successfully open a file for writing, doesn't that delete the 
>>> existing contents?
>>
>> No, depending on the mode you use to open. Think about it: how could you 
>> ever append to a file if that were the case?
>
> OK, fine, for appending. :-) The terminology is tricky--sometimes when 
> they say "write" they mean, as opposed to "append".

I don't want to harp on it, but it isn't just appending. You can open a file 
for "normal" writing and alter the existing contents of a file as well as 
adding to the end. Most of the code samples out there for altering the 
contents of a file probably read the entire file into memory, alter the 
contents, and then write the whole thing back out, but it doesn't HAVE to be 
done that way. Probably the best argument against direct manipulation of a 
file stream is that if you're reading a file and you discover something you 
want to change, you have to back up the stream pointer to overwrite that 
particular item. Of course, you have to do this for a MemoryStream too....

For reference, the only members of the FileMode enumeration which will wipe 
out an existing file are Create and Truncate. 


0
Jeff
5/4/2010 1:55:53 PM
Reply:

Similar Artilces:

Excel on website
Hi, I have an Excel file which opens maximum at my desk, but it opens minimum when I view it on my company's intranet. I use FrontPage 03 and Excel 07. Please advice! Thanks Chi After moving the file to server and linked it to the page I got it! Thanks Chi "Chi" wrote: > Hi, > > I have an Excel file which opens maximum at my desk, but it opens minimum > when I view it on my company's intranet. I use FrontPage 03 and Excel 07. > > Please advice! > > Thanks > Chi > > > > > > Aft...

Sum
Hi, I just found this "feature" at Excel formulas today. Please, have a look at: http://danielgudang.multiply.com/journal/item/192 (in portuguese, but images show all) Let me explain: some cell C1 = sum (C2:C5) some cell D1 = sum (D2:D5) some cell E1 = sum (E2:E5) A1 = C1 - D1 - E1 B1 = C1 - (D1 + E1) A1 = B1 ??? Oh, not always! Sometimes A1 will be +0, sometimes -0. Really strange! It's a feature of any application that uses IEEE double precision floating point math (e.g., every commercial spreadsheet I know of). It's the result of having finite precision ...

Having 2 workbooks open simultaneously
I have a wide screen laptop that can accommodate 2 Excel workbooks side by side. I used that feature for a while until sometime about two weeks ago something happened (I don't know what) and now I a only able to keep one workbook open at a time. When I try to open a second one, the first one minimizes. Can anyone tell me how to get it back so that I can once again view 2 workbooks side by side? Many thanks for your help -- Royal Did yesterday's response work: "How about just window|Arrange|Tiled (or whatever you want)" Royal wrote: > > I have a wide screen l...

Can You Tell Me Which Microsoft Certification is the most popular nowdays
Recommend Microsoft certificatons : * Exchange Server 2007 * MCAD.NET * MCDBA * MCDST * MCITP * MCP * MCPD * MCSA * MCSA 2003 * MCSD.NET * MCSE * MCSE 2000 Security * MCSE 2003 * MCSE 2003 Managing * MCSE 2003 Security * MCTS * Microsoft Business Solutions * Microsoft Licensing exam * Microsoft Partner Competency exam * Microsoft Windows Vista, Configuring * Office SharePoint Server 2007 * TS * Windows SharePoint Services 3.0 Which Microsoft Certification is the most popular, get the answer in http://...

Worksheet disappear when saving on shared workbook
Im Using Excel 2000 SP3, when i try to copy a cells from another workbook and paste it on the shared workbook, the worksheet of the shared workbook disappeared after saving it. Please help. ...

Opening Draft E-mail Outlook 2000
Outlook 2000, Windows XP Pro Outlook hung when two e-mails were open, they are both sttill in Drafts folder. But they cannot be opened, an unknown error is returned when opening. Where are the temp files relating to these e-mails stored? If I open them in wordpad I can copy the contents out. Any ideas? Cheers, Richard Thorneycroft IT Systems Support Husky Group Ltd (01455) 555 340 They are stored in your PST. You can try to export them to another format (File | Import and Export) but those messages are possibly corrupt. "RichardThorneycroft" <richard-deletethis-@huskypr...

Embedded Excel worksheet in PowerPoint
I'm working with an embedded excel worksheet in powerponit. The doc is sent to me and if I have to edit the worksheet, I find that some cells have missing characters once I open the worksheet to edit it. It appears that characters beyond some length (256?) are being truncated off. How do I resolve this so I don't have this problem again? Thanks, Barb Reinhardt This kind of problem may occur if the material was inserted into the embedded worksheet via copy/paste from an external sheet. A reliable approach is to open the embedded sheet first, and from that sheet do: File ...

Excel 2003 01-27-10
Hi all, hopefully you can help me. In cell B21 the user will always input an email address, i have created a button and now need the button to automatically email the email address in cell B21. I then need it to default and save to a specific location with the file name automatically generated from b12. Please help. :) ...

And we'll tell the difference how?
"Although no specific plans have been made, executives within Microsoft are examining whether it makes sense to release ad-supported versions of products such as Works, Money, or even the Windows operating system itself, according to internal documents seen by CNET News.com." http://news.com.com/Microsoft+eyes+making+desktop+apps+free/2100-1014_3-5951569.html?part=rss&tag=5951569&subj=news ...

Purge data based on another Excel file???
I am attempting to purge data from a large list based on data in another document. I have the files I want to remaove in on file and the file I want to remove them from. There should be simple way to do this no? ...

Can't open many XLS files at once
I have Excel 2000 and Win XP pro. I used to be able to highlight many XLS files in Explorer, right click, choose open and all the highlighted files would open in Excel with the different xls files on my task bar. I can still do this on my W98 computer, but I can only open one file at a time on my Win XP computer. If I double click on a 2nd XLS file the 2nd XLS file opens and the first file closes. I tried the run excel.exe /regserv fix but this did not solve the problem. Does anyone know how to fix this for Win XP? Thanks Dennis Hi maybe: Tools - Options - General and uncheck 'Ig...

RUS does not allow mail boxes to be opened
When creating new user/mail box in AD on our Exchange 2000 server, the RUS generates a smtp address, but when user logs on Outlook reports 'Unable to open your default e-mail folders, you do not have permission to logon'. We have checked ADSI and the setting 'msExchUsserAccountControl' has a value of 'not set'. If you change this to '0', then the mailbox can be opened. Why is RUS not setting this value? We have tried creating new RUS and recepient policy and this does not make any difference. The user has permission to open his own mailbox. We have tried ...

EXCEL 2007
I have a GIS application which uses .dbf files (not sure if they are actually Dbase files). When I need to append data (fields) to the files, I have a big problem in that excel 2007 no longer allows save as / export to .dbf files. If I try bring the data in MS Access and save as .dbf files, I have untold problems with the GIS application... The only way I have been successful with this is to find a user with an older version of excel, insert the data as database columns and then save as .dbf Any suggestions as to how I can get around this with out purchasing a converter? ...

in Excel: How do I change "selected cells" highlight color?
When I select cells in Excel the shading is so light (blue) that I can barely tell which cells are selected from those that are not. Can I change the background color of cells that are selected? Thanks. Assuming Excel 2007 this is a known problem and as yet no patch for it. I don't have Excel 2007 but I have read that playing with your Contrast can help some. Gord Dibben MS Excel MVP On Tue, 10 Jun 2008 12:51:00 -0700, flameretired <flameretired@discussions.microsoft.com> wrote: >When I select cells in Excel the shading is so light (blue) that I can barely >tell whic...

email from excel workbook button does nothing
In my old version of excel(2003) as long as i had outlook open i could email directly from the worksheet. Now i have office 2007 with "windows mail" when i press the button in the "quick access" tool bar it does nothing. I have tried having widows mail open when I do it too but the only way i can send is by making file attachments. seems like the long way around. Is there something I am doing wrong? I have tried to read some of the previous posts and dont understand anything about codes ect. Please help, Mark hi Mark Is Windows Mail your default m...

HELP!! Publisher won't open
I have Publisher 2000. When I click on the icon on the desktop, the program opens to the catalog page. When I click on 'existing files'. the program closes down. I can open the existing file in Windows Explorer, but as soon as I click on something, it closes down. This is a relatively new computer running Windows XP Media Center. I have been able to access my existing file before on this system. Can someone please help? Can you open Publisher in Safe Mode? Do you have a default printer enabled? Insert your 2000 disk, select repair. -- Mary Sauer MSFT MVP http://office.micros...

Delete duplicate rows from a list in Excel
Hi, Below are instructions on how to "delete duplicate rows from a list in Excel". I learned about this tip from the Microsoft Office Assistant website. However, each time I try step 4, my list is not filtered and no records are hidden. Is there a secret I am missing to make this work? Thanks for your help! -Greg http://office.microsoft.com/en-us/assistance/HA010346261033.aspx Applies to Microsoft Office Excel 2003 Microsoft Excel 2002 A duplicate row (also called a record) in a list is one where all values in the row are an exact match of all the values in another row. To delet...

How to tell Excel to insert cells and shift down from Access
Hi all, I have Access 2003. I am trying to tell Excel to insert cells and shift cells down from Access, with this line of code: oWksh.Cells.Insert Shift:=xlDown But Access does not like that line. It gave me run-time error 1004. I know if has to do with the part Shift:=xlDown Can you suggestion what I can do? Thank you in advance, Ben "Ben" <Ben@NoSpam.com> wrote in message news:eSZuzcDpKHA.1892@TK2MSFTNGP02.phx.gbl... > Hi all, > > I have Access 2003. I am trying to tell Excel to insert cells and shift > cells down from Access,...

VB.NET for Office Open XML
I have been working on a function that inserts a slide into a presentation. I am having a problem grasping the steps to add a child node to the parent document. in this case a slide to the presentation. There is code on the MSDN site but it has several errors and when I fixed them it would not update the slide title. It would actually corrupt the file. Here is what I have so far: Public Function PPTInsertNewSlide(ByVal fileName As String, ByVal position As Integer, ByVal title As String) As Boolean Dim returnValue As Boolean = False Dim documentPart As PackagePart = Noth...

Text Wrap in Excel 2002
I have been having difficultly wrapping text. I merge several cells and wrap text sucessfully. However, if I go into one of the cells and add a couple of sentences and enter, my text that was wrapped perfectly is simply a string of #########'s and I can't view the text correctly by changing the row height or reformatting (even though it is all still there - as I can see in the edit box - when my cursor is on the text box). Any suggestions? Try formatting the cell as General. bdean wrote: > > I have been having difficultly wrapping text. I merge > several cells an...

EXCEL Cell Formatting: Custom or Conditional
At work I have Excel 2002 and I have some cells that I import data in from the mainframe. I have a table where there is a column for GENDER (MALE or FEMALE) that I would like to see shown as "Male" or "Female" (mixed case). Is there a way to change all upper case to mixed case using either CUSTOM formatting or CONDITIONAL formatting? I would prefer not using a macro if there is something simpler. I know that =PROPER() produces the correct result, but I don't see how I can use this unless I'm referencing another cell. I would like to be able to just change what fa...

Excel Version When Posting Please State Your Excel Version!!!
On Jan 8, 6:58=A0pm, "Tyro" <T...@hotmail.com> wrote: > Subject: Excel Version When Posting Please State Your > Excel Version!!! And Please Include Your Questions and Comments In the Body of Your Posting!!! Not just in the subject line, even if it simply duplicates the subject line. Some news readers truncate the subject line. Some news servers do not permit you to reply to articles with empty bodies. Some news servers seem to reject such articles when they come in from the network, or perhaps the originating news server does not push them into the network. That ...

Excel Formulas
I have a spreadsheet showing whether customers have paid their debts or not. Each row represents a customer and details of the debt and the last cell in the row states whether the debt is "PAID" OR "UNPAID". Where the word "PAID" appears in a cell, I want to write a formula that will move that whole row to a different location on my sheet. I eventually want to record this action on a MACRO, so that every time I run a MACRO on my accounts spreadsheet it will take the updated information of customers marked "paid" and put in a seperate spreadsheet. Ple...

how can I change the a,b,c, column headers in excel to names
I've tried and can't figure out how to change the column header from a,b,c, etc. to names for each column. Any tips are appreciated You can only show A,B,C...,IV or numbers: 1,2,3,...,256 But you could put your names in Row 1 and then select A1, then select A2 and click on Window|Freeze panes. espray wrote: > > I've tried and can't figure out how to change the column header from a,b,c, > etc. to names for each column. Any tips are appreciated -- Dave Peterson ...

How to stop a file from opening when XL starts?
Hi, This isn't as easy as you may think. I'm running XL2003 on Windows XP home and have a strange problem. When I start XL it opens with a file named chart 1 gif 1.gif. I have looked in the XLSTART folder but there's nothing in there, I do not have anything in the folder to open files from upon starting XL either. I have searched my hard drives for a file named as such without luck. While testing I tried saving the file as chart 1 gif 1.guf to see if I could find out where it was stored unsuccessfully, so now I get both of those files opening each time I start XL, but their is n...