简体   繁体   中英

C# Excel automation works only when Excel is visible

I have developed a small C# utility that opens several workbooks and copy worksheets and specific cell ranges into a merged workbook. The app works flawlessly until I set Excel.Aplication.Visible = false. Then, it throws a COM error 800A03EC on opening a workbook. This is the same location, same workbook that is opened when Visible = true.

The Excel processing occurs on a BackgroundWorker thread.

To begin, I start Excel and open several "global" workbooks that will be used as sources for all of the merged workbooks.

//Start Excel and get Application object.
XL = new Excel.Application();
XL.Visible = true;

//Open workbooks that are used for all providers
sourceTemplate = (Excel._Workbook)(XL.Workbooks.Open(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Provider Summary Template.xlsx")));
currentTemplateSheet = sourceTemplate.Worksheets["Summary"];
sourceAuditPlan = (Excel._Workbook)(XL.Workbooks.Open(Path.Combine(DataSetFolder.Text, "Audit Plan.xlsx")));
currentAuditPlanSheet = sourceAuditPlan.Worksheets["Audit Plan"];
sourceRiskSummary = (Excel._Workbook)(XL.Workbooks.Open(Path.Combine(DataSetFolder.Text, "Provider Risk Summary.xlsx")));
currentRiskSummarySheet = sourceRiskSummary.Worksheets["Visible Risk"];

Then I loop through a list of workbooks, opening one at a time, and opening additional workbooks based on data found in that workbook (so opening workbook for Dr. One, a Chiropractor, I then open utilization workbooks for the Chiropractor specialty, etc.). It is on that first utilization workbook that the error is thrown.

foreach (string workbook in workbooksList)
{
    try
    {
        //Determine provider ID - in target filename as Dr_[Provider ID]_RVU.xlsx
        string providerId = workbook.Substring(workbook.IndexOf("_") + 1, workbook.LastIndexOf("_") - (workbook.IndexOf("_") + 1));

        //Open target workbook (Dr_xx_RVU.xlsx)
        targetWorkbook = (Excel._Workbook)(XL.Workbooks.Open(workbook));
        Excel.Sheets targetWorksheets = targetWorkbook.Worksheets;
        currentTargetSheet = (Excel._Worksheet)targetWorksheets.get_Item(1);

        //Determine specialty - in cell A1 of first worksheet as [Data Set Name]: [Provider Name], [Specialty]
        Excel.Range providerRange = currentTargetSheet.get_Range("A1");
        string providerInfo = providerRange.Cells.Value;
        string specialty = providerInfo.Substring(providerInfo.LastIndexOf(",") + 2);
        string providerName = providerInfo.Substring(providerInfo.LastIndexOf(":") + 2, providerInfo.LastIndexOf(",") - (providerInfo.LastIndexOf(":") + 2));

        //Open appropriate Modifier Util workbook, select and copy this provider's worksheet
        string sourceWorkbookName = Path.Combine(DataSetFolder.Text, "Modifier Utilization - " + specialty + ".xlsx");
        Debug.WriteLine(sourceWorkbookName);
        sourceWorkbook = (Excel._Workbook)(XL.Workbooks.Open(sourceWorkbookName));
        if (sourceWorkbook == null)
        {
            Debug.WriteLine("Unable to open worksheet " + sourceWorkbookName);
        }
        Excel.Sheets sourceWorksheets = sourceWorkbook.Worksheets;
        currentSourceSheet = (Excel._Worksheet)sourceWorksheets[providerId];
        if (currentSourceSheet == null)
        {
            Debug.WriteLine("Unable to find " + providerId + " worksheet in " + sourceWorkbookName);
        }

        currentSourceSheet.Copy(Before: currentTargetSheet);                //insert as first worksheet
        currentTargetSheet = (Excel._Worksheet)targetWorksheets.get_Item(1);
        currentTargetSheet.Name = "Modifier Util";

I omitted the rest of the loop - I simply open two other workbooks in the same fashion, copy worksheets into the target workbook, save the target and close this set of workbooks for the next iteration.

The error occurs on

sourceWorkbook = (Excel._Workbook)(XL.Workbooks.Open(sourceWorkbookName));

So I see the DebugWriteLine of the sourceWorkbookName, but no further. Excel opens a dialog displaying the error number800A03EC (which I have researched). If I allow the app to continue processing, on the next iteration it opens a dialog announcing that the workbook is already open, and do I want to re-open and lose changes or continue. Makes no sense to me, based on the first error and the fact that no worksheets are copied from that workbook.

Again, all is well when Excel.visible = true.

I appreciate all suggestions, Scott

Have u tried setting sourceWorkbookName to a basic filename without space's, in a directory without any permission issues? I know its a long shot, but for me it would be the place to start, i have encountered some strange behavior with excel 2007 on that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM