Promrammatically access to the VB project is not trusted

Jul 26 2005 5:09 PM
Iam facing the same problems here when Iam trying to access the macro by C# project. 

The error message is "programmatic access to VB project is not trusted".

Here is the code what I wrote for accessing excel macro.

bindgridtoexcel();

gridtoexcel.Visible = true;

string outURL = "documents/" + DateTime.Now.ToString("dd_MM_yy_hh_mm_ss");

string path = Server.MapPath(outURL);

Response.Write("<iframe src='" + outURL + ".xls' style='display:none'></iframe>");

System.IO.StreamWriter sw = new System.IO.StreamWriter(path + ".htm");

HtmlTextWriter htw = new HtmlTextWriter(sw);

gridtoexcel.RenderControl(htw);

htw.Close();

sw.Close();

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

excelApp.Visible = true;

Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);

wb = excelApp.Workbooks.Open(path + ".htm",0,false,5,"","",false,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,"",true,false,0,true,false,false);

Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet) wb.ActiveSheet;

sheet.get_Range("A1","GE1").EntireColumn.AutoFit();

sheet.Cells.Font.Size = 8;

VBIDE.VBComponent module=null;

module = wb.VBProject.VBComponents.Add(VBIDE.vbext_ComponentType.vbext_ct_StdModule);

module.CodeModule.AddFromString(GetMacro());

wb.Application.Run("FormatSheet",Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing,Type.Missing,

Type.Missing,Type.Missing,Type.Missing);

wb.SaveAs(path+".xls",43,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);

wb.Close(false,Type.Missing,Type.Missing);

}

private static string GetMacro()

{

string retVal = "";

retVal+=("Sub FormatSheet()" + "\n");

retVal+=(" Range(\"A6:J13\").Select " + "\n");

retVal+=(" Selection.Font.ColorIndex = 3" + "\n");

retVal+=("End Sub");

return retVal;
}

I cleared out all the security issues
1. I went to excel-macro-security-permitted to low and clicked all the check boxes
2. Adjusted Microsoft Excel permission levels.
3. On .Net framework cnfg manager. Adjusted all to full trust.

But Iam not finding out the way. Iam getting the same error

Answers will be appreciated