Search code examples
c#exceloffice-automation

Visual Studio forcing me to use full namespace even with reference added and "using" added


Im using VS2013 community copying and I have downloaded the excel library and it still forces me to write the whole namespace each time saying it cant distinguish between winforms.application() and excel.application. It also says that the "missing" in "missing.value" does not have context.

This code is copied almost entirely from the MS website.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace ExcelAuto 
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Initiate Excel Objects

            //XlMSApplication oXL;
            Microsoft.Office.Interop.Excel.Application oXL;
            Workbook oWB;
            Worksheet oSheet;
            Range oRng;

            try
            {
                //Start Excel and get Application Object.
                oXL = new Microsoft.Office.Interop.Excel.Application();
                oXL.Visible = true;

                //Get a new workbook
                oWB= (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));

            }
            catch { }
        }
    }
}

Solution

  • This happens because you are using the Application class which is present in two namespaces: System.Windows.Forms and Microsoft.Office.Interop.Excel. Fully qualified name allows to determine which one has to be used.