We help IT Professionals succeed at work.

Visual Studio 2008 - Excel 2007 Workbook - Timesheet Generator

2,381 Views
Last Modified: 2012-05-08
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 
    }
}

Open in new window

Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
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?

Author

Commented:
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.

Author

Commented:
And I did not use his link in creating my solution.  I figured it out on my own before reading his.  
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.