C# Excel dropdown event

人走茶凉 提交于 2019-12-07 15:48:11

问题


I have an Excel spreadsheet that sits in an automated testing application that is written in C#. The testing app populates the sheet with final stats and the spreadsheet is then used to generate metrics. Part of that procedure is to place dropdowns into Column L of the spreadsheet, and write their associated VBA change events into the sheet from the app.

Here is the code that writes the dropdown to the spreadsheet Excel.DropDown xlDropDown;

//set range for insert cell
 range = wrkSheet.get_Range("L" + (x + 9).ToString() + ":L" + (x + 9).ToString());

//insert the dropdown into the cell
xlDropDown = xlDropDowns.Add((double)range.Left, (double)range.Top, (double)range.Width, (double)range.Height, true);

//set the nbame of the new dropdown
xlDropDown.Name = "expFail" + (x + 1).ToString();

//assign dropdown name to cmbName 
cmbName = xlDropDown.Name;

 //call function to write change macro for this box 
 cmbWriteMacro(cmbName, xlApp, xlBook, wrkSheet); 

The cmbWrite Function:

    StringBuilder sb;
    VBA.VBComponent xlModule;
    VBA.VBProject prj;
    string modName;
    int modExists;

    prj = wrkBook.VBProject;
    modExists = 0;

    sb = new StringBuilder();

    //build string with module code 
    sb.Append("Sub " + cmbName + "_Change()" + "\n");
    sb.Append("\t" + "Call lstBox_Update(" + cmbName + ")" + "\n");
    sb.Append("End Sub");

    foreach (VBA.VBComponent comp in prj.VBComponents)
    {
        modName = comp.Name;

        if (modName == "Module2")
        {
            modExists = 1;
            break;
        }
    }

     //check to see if module already exists
     if (modExists != 1)
     {
        //set an object for the new module to create
        xlModule = wrkBook.VBProject.VBComponents.Add(VBA.vbext_ComponentType.vbext_ct_StdModule);
     }
     else
     {
        xlModule = wrkBook.VBProject.VBComponents.Item("Module2");
     }

     //add the cmbbox macro to the spreadsheet
     xlModule.CodeModule.AddFromString(sb.ToString());

This writes the following VBA events into the spreadsheet to perform action in the sheet as failures are noted.

Sub expFail1_Change(ByVal Target As Range)
    Call lstBox_Update("expFail1")
End Sub

All the dropdowns call the same function (lstBox_Update) based on what the selection is.

Everything is working OK. The dropdowns show up where they should and the macros are being written into the spreadsheet correctly. The problem seems to be getting the associated change events to fire when the selection is changed. The solution is probably easy, but I have looked all over and can't seem to find an answer


回答1:


I just did a small example based on your code. After some googling around and testing some source code pieces it comes down to a single line of code:

xlDropDown.Name = "expFail" + (x + 1).ToString(); // your code
xlDropDown.OnAction = xlDropDown.Name + "_Change"; // new code: IMPORTANT

Found this code within an Microsoft KB article How To Create an Excel Macro by Using Automation from Visual C# .NET.

It seems that you need this line of code to set some kind of delegate pointing to your custom vba code.

In case you need my complete source let me know and I will mail it to you.




回答2:


You have to write the change event to the Worksheet where you create the combobox (not to the VBA module "module2". The combobox is part of the Worksheet(object) and all the event handlers have to be there.




回答3:


Based on this article you need to write the code to the Worksheet_Change event, not the ComboBox_Change event:

Sub Worksheet_Change(ByVal Target As Range)
    Call lstBoxUpdate("expFail1")
End Sub


来源:https://stackoverflow.com/questions/14857173/c-sharp-excel-dropdown-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!