Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open an existing Excel file in Windows application controlled by C# 1

Status
Not open for further replies.

navpreet1983

Programmer
Sep 14, 2006
1
US
I have the code to open a new Excel workbook, but can't seem to know how to open an existing file from say my C drive. Here is the code I have...any help would be great! Still new to C#.
Code:
            Excel.Application oXL;
            Excel._Workbook oWB;
            Excel._Worksheet oSheet;
            Excel.Range oRng;

            try
            {
                //Start Excel and get Application object.
                oXL = new Excel.Application();
                oXL.Visible = true;

                [green]//Get a new workbook. But need an old file. [/green]
                [red]oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));[/red]
                oSheet = (Excel._Worksheet)oWB.ActiveSheet;

Essentially, the second last line of code "oWB = ..." should open a file, say in C:\abc.xls. Once I have control of the workbook I can do the rest. :-s

Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top