Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.
I've searched for a code in C# which create Excel document with dynamically added macro code for VBAProject. I found something like this - a piece of code is below. A Workbook and macro is created as I expect but there is one problem with it. After saving document (line wb.SaveAs) a whole VBAProject become locked and I haven't access to it anymore. When I click on Project Excel says that Project is unviewable. I can still execute a macro which is added by C# (FormatSheet) but can't edit a code.
Weird thing that when I tried to use line: wb.Save(); then VBAProject isn't locked (but I have to manually save file in Excel).
It is something wrong with parameters I am using in method SaveAs ?
My version of Office is 2003.
I'll be appreciate for any sugestions.

C#
string MyFile = Path.GetFullPath(".") + @"\sample.xls";
Excel.Application xl = null;
Excel._Workbook wb = null;
Excel._Worksheet sheet = null;
VBIDE.VBComponent module = null;
      
try
{

    if (File.Exists(FileName)) { File.Delete(FileName); }
  
    GC.Collect(); 

    xl = new Excel.Application();                
    xl.Visible = true;
		 
    wb = (Excel._Workbook)(xl.Workbooks.Add( Missing.Value ));              
    sheet = (Excel._Worksheet)wb.ActiveSheet;
				
    for(int r = 0;r<20;r++)
    {
    	for(int c=0;c<10;c++)
    	{
    		sheet.Cells[r + 1, c+1] = 125; 
    	}
    }

               
    module = wb.VBProject.VBComponents.Add( VBIDE.vbext_ComponentType.vbext_ct_StdModule);
    module.CodeModule.AddFromString(GetMacro);
      
    //wb.Save();
                			wb.SaveAs(FileName,Excel.XlFileFormat.xlWorkbookNormal , null, null, false, false,Excel.XlSaveAsAccessMode.xlShared,false,false,null,null,null);
				 
}
catch( Exception theException ) 
{
    Console.WriteLine(theException.Message );
}
finally
{

    try
    {
    	xl.Visible = false;
    	xl.UserControl = false; 					
    	xl.Workbooks.Close();
    }
    catch { }

    xl.Quit();
    
    if (module != null)  { Marshal.ReleaseComObject (module); }
    if (sheet !=null) { Marshal.ReleaseComObject (sheet); }
    if (wb !=null)    { Marshal.ReleaseComObject (wb); }
    if (xl !=null)    { Marshal.ReleaseComObject (xl); }
    
    module = null;
    sheet=null;
    wb=null;
    xl = null;
    GC.Collect(); 
}

private static string GetMacro()
{
   StringBuilder sb = new StringBuilder();
  
   sb.Append("Sub FormatSheet()" + "\n");
   sb.Append("  Range(\"A6:J13\").Select " + "\n");
   sb.Append("  Selection.Font.ColorIndex = 3" + "\n");
   sb.Append("End Sub");

   return sb.ToString();
}
Posted

1 solution

I've done some experiments with parameters of the SaveAs method and when I used this form

C#
wb.SaveAs(FileName, Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, false, false, null, null, null);

I have access to VBProject so it seems that enumeration
C#
Excel.XlSaveAsAccessMode.xlShared
is in this case responsible for my problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900