问题
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.
回答1:
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);
}
}
来源:https://stackoverflow.com/questions/2416727/set-excel-named-ranges-via-c