简体   繁体   中英

Set Excel Named Ranges via C#?

I'm trying to replicate this Access VBA code using C#, but am unable to do so. Wondering if anyone else has tried this before and can help.

oWB.Worksheets("Signoff").Range("rgSignOffRecTemplate").Value = g_TemplatePath & "Signoff_Rec.XLT"

rgSignOffRecTemplate is a "Defined Name" in the Excel template that I'm trying to write to.

Many thanks for your help.

    private void ThisWorkbook_Startup(object sender, System.EventArgs e)
    {
        Excel.Name oName;
        Excel.Range oRange;

        //'using name
        oName = ExcelWorkbook1.Globals.ThisWorkbook.Names.Item("rgSignOffRecTemplate", missing, missing);
        oName.RefersToRange.Value2 = "here";

        //'using range
        oName = this.Names.Item("rgSignOffRecTemplate", missing, missing);
        oRange = oName.RefersToRange;
        oRange.Value2 = "here i am";

        //'direct access
        this.Names.Item("rgSignOffRecTemplate", missing, missing).RefersToRange.Value2 = "here i am again";

        DisplayWorkbookNames();

    }

    private void DisplayWorkbookNames() {
        for (int i = 1; i <= this.Names.Count - 1; i++) {
            Globals.Sheet1.Range["A" + i.ToString(), missing].Value2 = this.Names.Item(i, missing, missing);
        }
    }

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