1

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.

Nick Heidke
  • 2,787
  • 2
  • 34
  • 58
Chapax
  • 251
  • 1
  • 4
  • 8
  • any error/exception, stacktrace? – shahkalpesh Mar 10 '10 at 12:20
  • No error/exception ... because I just don't know how to set the value – Chapax Mar 10 '10 at 12:30
  • Try this search: http://stackoverflow.com/search?q=write+excel+[c%23] It is esier to work from Excel, rather than Excel though Access. – Fionnuala Mar 10 '10 at 12:40
  • Is this Access? I don't think so. You are dealing with workbook & worksheets. That is part of Excel & **not** Access. – shahkalpesh Mar 10 '10 at 12:41
  • Sorry, yes .. .this is Access VBA Code writing to Excel – Chapax Mar 10 '10 at 12:45
  • Never used via C# but I guess this shoudl work as soon as you have the proper reference to the Excel library. How do you instantiate the oWB ? – iDevlop Mar 10 '10 at 13:19
  • the code I've mentioned is in Access VBA, and is setting the value of a named reference "rgSignOffRecTemplate" in an Excel template. My question is, how can I do the same in c#? – Chapax Mar 10 '10 at 15:16
  • You should add a C# tag to this question. That would help the C# experts to locate it and help you. – PowerUser Mar 10 '10 at 15:51
  • Worst case scenario, you could set up a shell in C to run an Access macro that will do it for you. Unnecessarily complicated, but it would get the job done. – PowerUser Mar 10 '10 at 15:52

1 Answers1

6
    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);
        }
    }
AMissico
  • 21,470
  • 7
  • 78
  • 106