Programmatically creating worksheets in an Excel Add In (C#) - c#

According to MSDN, one can programmatically create and delete Excel Worksheets in Visual Studio (http://msdn.microsoft.com/en-us/library/6fczc37s.aspx). The code to do so is as follows:
private void createNewAccount()
{
Excel.Worksheet newWorksheet;
newWorksheet = Globals.ThisAddIn.Application.ThisWorkbook.Worksheets.Add();
}
One of the forms in my project has a button carrying this code. The user is supposed to be able to press it and create a new worksheet, but it doesn't work. So I decided to make it a button on a ribbon tab, instead. That didn't work either. What am I missing?

My answer is not going to be too long.
If you need to go Microsoft way, please first be sure to first create a Book. Remember Excel files resemble a workbook, then, inside, you can create worksheets.
If you don't please take a look at NPOI project.
With both approaches you will have to create the workbook, then the worksheets inside.
More on this link.
Well, If you are sure you have access to a WorkBook I would recommend you to:
1.- Use the parameters of the WorkSheets.Add method
2.- Please take a look at this example.
3.- Persist the workbook after the worksheet is added. This will allow to check if it was added or not.
Last but not least, debug, debug and debug.
Hope that helps,

Related

Is there a way to supress "Name Conflicts" dialog before opening XLSX file by C# in Excel 2007?

I am facing a problem while opening xlsx workbook by C# code.
Problem:
A pop appear while opening the xlsx, and user expected to acknowledge it.
Reason:
Reason of pop is known since this this is known problem.
Probable Solution: Needs to automate and suppress this pop up so no manual intervention is needed.
Reference of one such solution in VB:
http://social.msdn.microsoft.com/Forums/office/en-US/546c7506-7146-45b6-873e-b3793df2979b/is-there-a-way-to-supress-name-conflicts-dialog-in-excel-2007?forum=exceldev
Question to Forum:
Solution mentioned above is VB based. I am in search of such solution in C#.** And importantly **query is how to detect such pop up within code.
Pop up is seen after invoking: Workbook.Open(.....).
Also it is neither exception nor there is return value.
Is there any way to find out such pop in advance before opening file?
the link shows you not VB.NET code, its VBA code.
For this code you must allready open the XLSX document.
But i know no way to find it before you open the document.
One way, not a good way. To look into the xlsx file (its a zip archive) and find the place and remove it. You can try to use the OpenXML SDK from Microsoft to remove the defined name.
Here you find the description of OpenXML SDK for Excel:
http://msdn.microsoft.com/de-de/library/documentformat.openxml.spreadsheet(v=office.15).aspx
I hope it helps a little!
I got the same problem in a workbook which is getting filled dynamicaly via C# and filters some data.
After i executed this VBA code:
On Error Resume Next
With Workbook("YourWokrbookName")
.Names("_FilterDatabase").Delete
.Names("Criteria").Delete
.Names("Extract").Delete
End With
On Error GoTo 0
Which i found here and adapted for my use i never got the naming confict messagebox again.
The simply deletes the _FilterDatabase object in the worksheet. This might be not usefull in every case but in my case it didn't made a difference.

How update excel file that is locked by another user

Does anyone know how to handle locked excel files with c#?
I'm trying to create an application that updates an excel file that is on the network which will be probably being used by someone when someone else will try to modify it.
So far I'm not able to write and save the file when is in use, and I'm pretty new to work with Excel and C#, so any help is welcome.
Normally, when a file is locked, you cannot open it.
Excel, however, allows you to prepare a workbook to be edited by multiple persons simultaneously. You have to prepare the workbook, and certain features will become unavailable when you do so, so this won't work for just any workbook.
Also, I don't know if you can also edit the workbook in C#, although I wouldn't know why not.
The steps needed are:
On the Review tab, in the Changes group, click Share Workbook.
On the Editing tab, select the Allow changes by more than one user at the same time. This also allows workbook merging check box.
On the Advanced tab, select the options that you want to use for tracking and updating changes, and then click OK.
The full steps and an overview of general functionality and consequences of enabling this, can be found on Microsoft's website:
http://office.microsoft.com/en-us/excel-help/use-a-shared-workbook-to-collaborate-HP010096833.aspx

Preventing users from using an instance of Excel

I'm using Microsoft.Office.Interop.Excel to create Excel reports with C#. Those reports have a large amount of graphics and take long time to prepare. During the preparation, The instance of the Excel application that my program uses is hidden from the user.
MY problem is that Microsoft Office tends to share application instances automatically. If the user opens an Excel workbook, Excel will try to find a running instance of Excel and open the document from there. When the user tries to open an Excel workbook while my program is running, it is attached to the instance my program uses.
This generates two problem. First, it forces my reports into visibility before they are supposed to become visible. And second, my program now needs to fight with user over the attention of the Excel instance - and my program usually loses.
So, is there any way to make the Excel instance reject requests(from the user. it should still obey to my program) to open documents, and make Office ignore my instance when it has to decide how to open an Excel document?
You could handle the Application.WorkbookOpen event. In here, either start a second instance of Excel and have it open the workbook, or close the workbook with an error message.
I also saw the Application.Interactive property. I haven't played with this, but it may be of use to you.
You can use NPOI, I suggest you visit the following link
http://npoi.codeplex.com/discussions/36157?ProjectName=npoi
I have done a little experimenting, and I think this will work:
Whenever you begin working with Excel, create two instances, and work with the second. When you're done with your work, delete the second instance and its object, then check the "UserControl" property of the first. If it returns "true", then delete only the object, but leave the process for that instance behind. If it returns "false", then delete the instance as well.
As far as I can tell, the user can open and close any number of workbooks, and it will use the first instance you created, as long as you don't delete it, and the second instance will be unmolested.

Excel Add in VSTO don't work when other XLA add in are installed

I have developed an Excel Add in with C#, and was working fine.
But since i had installed another Add in written in VBS in XLA file, my C# Add in doesn't work anymore...
Does anyone faced this situation before? how can i proceed to resolve that?
Can i create an XLA file fore my add in to force it loads?
Best regards
Sabri,
Try disabling and re-enabling your Addin. This could also be a very specific conflict between actions performed by your addin and the XLA file. Finally, it's possible that your Add-in does actions on the Application.Worksheets() elements and is not properly distinguishing between what the active workbook is (i.e. actions are being performed on the hidden XLA workbook that opens with each instance.)
A world of possibilities, so try some of these resolutions, and if nothing works, clarify your problem with a description of what each add-in does.

Restrict access to excel file opened by C# program

Question 1:
I've opened an excel file with
Excel.Application app = new Excel.ApplicationClass();
Excel.Workbook Wbook = app.Workbooks.Open("aaa.xlsx",...);
Now, I want to stop other programs accessing "aaa.xlsx".
(want to restrict access by other programs like excel.exe & etc)
Are there any options that I can set to lock/block/restrict file open?
Question 2:
Since I've done this
Excel.Application app = new Excel.ApplicationClass();
I've created a new instance of excel.
I want to hide it from external use.
(I don't want it to pop up when some random excel file is double clicked on the system.)
Is there something I can do to prevent it from being called up automatically?
Any help would be much appreciated.
These should probably be separate questions, but I will try.
Also, caveat emptor: It's been a while since I've touched Office Automation and don't have the docs handy, so I'm going off memory.
The Excel.Application object should have a Visible property. Set it to false to hide the application.
As for opening the document, check the docs to see if there are any parameters like "FileAccess". I can't remember off hand, but I will keep looking...
Edit: Okay, I found the documentation on MSDN, but there doesn't appear to be any way to specify that Excel should lock the document while it's open. Sorry.
An ugly solution but you could do it:
Open and lock the file yourself. Copy it to a temporary location, load that temporary into your hidden window. After saving it copy it back to the original location. Note that both of these copies will have to be implemented in your program as the lock will preclude Windows from doing it.

Categories

Resources