开发者

Enable Excel Macro in codes?

I am working an existing project which is base do Excel class to copy values from one worksheet A to another one B. There are some formulas and Addin functions in ws A cells. The copy codes are used to copy only values out to ws B as a result. Here is a block of simplified codes:

using Excel = Microsoft.Office.Interop.Excel;
....

object missing = System.Type.Missing;

Excel.Application app = new Excel.Application();
// Creating Excel application with source workbook in memory
Excel.Workbook workbook = app.Workbooks.Open("SourceA.xls",
  Excel.XlUpdateLinks.xlUpdateLinksNever,....);
...

app.Calculate() // Forcing to recalculate value in cells.
...

//create destination workbook
Excel.Workbook destWb = app.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.WorkSheet destSheet = (Excel.Worksheet)destWb.Worksheets[1];
Excel.Range destRange = (Excel.Range)destSheet.Cells[1, 1];
//rename sheet 1
destSheet.Name = "wsB Report";

//Set Source Data Range from 
Excel.WorkSheet sourceWA = (Excel.Wor开发者_高级运维ksheet)workbook.Worksheets["wsA"];
//Get the predefined named range "DataRange" from source wsA
Excel.Range sourceRange = sourceWA.get_Range("DataRange", missing);
//copy data
sourceRange.Copy(missing);

//paste values and number formats
destRange.PasteSpecial(Excel.XlPasteType.xlPasteValuesAndNumberFormats,
    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, missing, missing);
//paste formats
destRange.PasteSpecial(Excel.XlPasteType.xlPasteFormats,
    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, missing, missing);
//paste column widths
destRange.PasteSpecial(Excel.XlPasteType.xlPasteColumnWidths,
    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, missing, missing);
....

destWb.SaveAs("destB.xls", missing, ....);

The problem I have is that the copies cells in destB.xls are all marked as "#NAME?". They should be values copied. I open the source excel file and I can see the data in the source range are updated with values. However, when I run my codes, the values seem not available.

I guess that the Excel application I created in my codes may not have Macro enabled. This may be reason values cannot be copied. For example, when I open the source excel file, I'll see a prompt to enable or disable Macros. If I click on Enable button, the excel will take some time to display values in data cells.

If that is the reason, is there any way in codes to enable Macro so that all formulas in cells will be able to update values?

Another reason might be caused by OS or Windows security updates. Not sure if any Windows security settings may affect Excel macros. If this is the case, should I change Windows security level or any way to force Macros enabled in my project?

By the way, my project runs in Windows XP and Windows 2008 Server, with Office Excel 2003 installed. The project was done in VS 2008.


I went further into my codes and I found that by making Excel app visible, I'd able to see what actually happened. As Ben Voigt's suggestion, the security settings for Macro can be done in Excel, but in codes, you can force to execute any VBA-like codes no matter what the setting is. Here are some codes to make Excel visible and see all the prompt dialogs:

Excel.Application app = new Excel.Application();
Excel.Workbook workbook = app.Workbooks.Open("SourceA.xls", ...);
app.Visible = true;       //set to 'true' when debbugging, Exec is visible
app.DisplayAlerts = true; //enable all the prompt alerts for debug. 
...

For example, I rebuild links in my C# codes, set values in cells and refresh or calculate values(simulate F9). By making Excel visible, I saw what was going on and found bugs in my codes which did not do what I want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜