CoryDambach
asked on
Visual Studio 2008 - Excel 2007 Workbook - Timesheet Generator
I'm writing an Excel 2007 Workbook Project using C# and Visual Studio 2008. I've written the below code to generate my timesheets (which are required for bureaucratic reasons even though they are all the same) and save them. However, they are saved with references to the code or assemblies and when another person opens the spreadsheet file, they are given a nasty error like: The customization failed to load because the assembly could not be found (or something like that). How do I save each spreadsheet as just a spreadsheet with data. I don't want any code or customizations to be included in the spreadsheets I save.
using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace TimesheetsSinceJuly22
{
public partial class Sheet1
{
private void Sheet1_Startup(object sender, System.EventArgs e)
{
Object[,] range = WeekDates.get_Value( Missing.Value ) as Object[,];
DateTime curDate = DateTime.Parse( range[1,1].ToString() );
while( curDate.Year == 2009 )
{
range = WeekDates.get_Value( Missing.Value ) as Object[,];
for( int j = 0; j < 7; j++ )
{
WeekDates.Cells[1,1+j] = DateTime.Parse( range[1,1+j].ToString() ).AddDays( 7 ).ToShortDateString();
}
curDate = curDate.AddDays( 7 );
SaveWeek( curDate.ToShortDateString() );
}
}
private void SaveWeek( string weekName )
{
string path = Environment.GetFolderPath( Environment.SpecialFolder.DesktopDirectory );
string fullPath = Path.Combine( path, weekName.Replace("/", "-") + ".xlsx" );
SaveAs( fullPath, Excel.XlFileFormat.xlWorkbookDefault, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing );
);
}
private void Sheet1_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(Sheet1_Startup);
this.Shutdown += new System.EventHandler(Sheet1_Shutdown);
}
#endregion
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My suggestion was to create a new workbook and copy the worksheets into it. This is exactly what CoryDambach's code does. As far as I can see, it is the same solution. I provided the methodology and backed it up with a link to a coded example. What else do I have to do to earn points?
ASKER
I selected mine because it was a more direct solution to the problem I posted. My answer is in C# and is directly posted here. If you post a VB.Net solution with your code attached to a post I'll select yours.
ASKER
And I did not use his link in creating my solution. I figured it out on my own before reading his.