Friday, April 24, 2015

C# Excel VBA Side by Side Putting a value in a cell

To enter a value in cell, use the value property of range object.  A range object can consist of a cell or range of cells.  One example is the following

C#

Prior to running this code, you will need to  follow these steps in 


using Excel=Microsoft.Office.Interop.Excel;
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application objExcel=new Excel.Application();
            //Excel.Worksheet objWks;
            try
            {  
                objExcel.Visible=true;
                var objWb = objExcel.Workbooks.Add();

                Excel.Worksheet objWks = objWb.Worksheets[1] as Excel.Worksheet;
                
                objWks.Cells[1, 1].value = 1;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (objExcel !=null)
                {
                    objExcel=null;
                }
            }
        }
    }

Sunday, April 5, 2015

C# Excel VBA Side by Side Open a workbook

The Workbook object has an Open method that opens a workbook. The following samples show how to both open a workbook in edit mode and read-only mode in both Excel VBA and C#.

Excel VBA

Readonly:

Dim strFileName as string
Workbooks.Open(FileName:=strExcelFile, ReadOnly:=True)



Normal mode:

Dim strFileName as string
Workbooks.Open(FileName:=strExcelFile)

C#

Readonly:


Excel.Workbook objWb;
objWb = objExcelApp.Workbooks.Open(Filename: fileName,ReadOnly:true);

Normal mode:

Excel.Workbook objWb;
objWb = objExcelApp.Workbooks.Open(Filename: fileName);