We help IT Professionals succeed at work.
Get Started

Error: Use of unassigned local variable 'worksheet'

-Dman100- asked
Last Modified: 2013-11-26
Why am I getting the following error: Use of unassigned local variable 'worksheet' in this  code:

protected void Page_Load(object sender, EventArgs e)
        Application oXL = new Excel.Application();
        Workbook theWorkbook;
        Worksheet worksheet;

        theWorkbook = oXL.Workbooks.Add(Type.Missing);

        Range rg = worksheet.get_Range("A1", "E1");
        rg.Font.Bold = true;
        rg.Font.Name = "Arial";
        rg.Font.Size = 10;
        rg.WrapText = true;
        rg.HorizontalAlignment = Excel.Constants.xlCenter;
        rg.Interior.ColorIndex = 40;
        rg.Borders.Weight = 3;
        rg.Borders.LineStyle = Excel.Constants.xlSolid;
        rg.Cells.RowHeight = 38;

        rg = worksheet.get_Range("A1", Type.Missing);
        rg.Cells.ColumnWidth = 7;
        rg.Value = "Test Column A1";

        rg = worksheet.get_Range("B1", Type.Missing);
        rg.Cells.ColumnWidth = 11;
        rg.Value = "Test Column B1";

        rg = worksheet.get_Range("C1", Type.Missing);
        rg.Cells.ColumnWidth = 6.5;
        rg.Value = "Test Column C1";

        rg = worksheet.get_Range("D1", Type.Missing);
        rg.Cells.ColumnWidth = 10;
        rg.Value = "Test Column D1";

        rg = worksheet.get_Range("E1", Type.Missing);
        rg.Cells.ColumnWidth = 11;
        rg.Value = "Test Column E1";

        worksheet.Cells[1, 1] = "cell A1";
        worksheet.Cells[1, 2] = "cell B1";
        worksheet.Cells[1, 3] = "cell C1";
        worksheet.Cells[1, 4] = "cell D1";
        worksheet.Cells[1, 5] = "cell E1";

        oXL.ActiveWorkbook.SaveAs("f:\\" + @"\NonTemplateReport",
        Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing);


Error occurs on this line:
Range rg = worksheet.get_Range("A1", "E1");

Thanks for any help.
Watch Question
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE