Windows Develop Bookmark and Share   
 index > Windows Forms General > Microsoft.Office.Interop.Word.Application works; Word.Application doesn't. The heck?!
 

Microsoft.Office.Interop.Word.Application works; Word.Application doesn't. The heck?!

Below is my code snipped. What do I need to do so that the reference to "Word.Application" works and I don't have to spell out the whole namespace?

I've right-clicked on the test solution, done an add reference, and added "Microsoft Word 11.0 Object Library" version "8.3.0.0".

The code "Word.Application y = ... " throws the error when building:
"The type or namespace "Word" cannot be found (are you missing a using directive or assembly reference?)"

What am I missing?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Word;

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

        private void button1_Click(object sender, EventArgs e)
        {   //The next line works
            Microsoft.Office.Interop.Word.Application x = new Microsoft.Office.Interop.Word.Application();
            //The next line does not compile. Why?
            Word.Application y = new Word.Application();
        }
    }
}
FireMyst  Tuesday, September 29, 2009 2:15 AM
Because none of the namespaces you listed in your using statements has a class named Word. You can easily fix it with a namespace alias:

using Word = Microsoft.Office.Interop.Word;


Hans Passant.
  • Proposed As Answer byWang, JieMSFTTuesday, September 29, 2009 11:17 AM
  • Marked As Answer byFireMyst Wednesday, September 30, 2009 2:41 AM
  •  
nobugz  Tuesday, September 29, 2009 4:46 AM
Hi,

I think nobugz is right.

When automating Office Word in a Windows Forms application, we usually use the following statement:
using Word = Microsoft.Office.Interop.Word;


Because if you "using Microsoft.Office.Interop.Word;", thenApplication will become ambiguous - System.Windows.Forms.Application and Microsoft.Office.Interop.Word.Application.

So by "using Word = Microsoft.Office.Interop.Word;", you can differenciate Word.Application from System.Windows.Forms.Application.

Regards,
Jie
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

If you have any feedback, please tell us.

The CodeFx Project
My Blog (in Simplified Chinese)
  • Marked As Answer byFireMyst Wednesday, September 30, 2009 2:41 AM
  •  
Wang, Jie  Tuesday, September 29, 2009 11:17 AM
Because none of the namespaces you listed in your using statements has a class named Word. You can easily fix it with a namespace alias:

using Word = Microsoft.Office.Interop.Word;


Hans Passant.
  • Proposed As Answer byWang, JieMSFTTuesday, September 29, 2009 11:17 AM
  • Marked As Answer byFireMyst Wednesday, September 30, 2009 2:41 AM
  •  
nobugz  Tuesday, September 29, 2009 4:46 AM
Hi,

I think nobugz is right.

When automating Office Word in a Windows Forms application, we usually use the following statement:
using Word = Microsoft.Office.Interop.Word;


Because if you "using Microsoft.Office.Interop.Word;", thenApplication will become ambiguous - System.Windows.Forms.Application and Microsoft.Office.Interop.Word.Application.

So by "using Word = Microsoft.Office.Interop.Word;", you can differenciate Word.Application from System.Windows.Forms.Application.

Regards,
Jie
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

If you have any feedback, please tell us.

The CodeFx Project
My Blog (in Simplified Chinese)
  • Marked As Answer byFireMyst Wednesday, September 30, 2009 2:41 AM
  •  
Wang, Jie  Tuesday, September 29, 2009 11:17 AM
Thank you guys!

I did have to add:

using

Word = Microsoft.Office.Interop.Word;

at the top of my code.

I didn't know you could do aliases like that. How cool. :-)
FireMyst  Wednesday, September 30, 2009 2:40 AM

You can use google to search for other answers

Custom Search

More Threads

• programmatically retrieve my network places
• Upload files from Win Forms over Citrix
• Scope of Module Variable
• login form DialogResult help
• making a form as an acive form through program when minimised
• Ghost Window
• How to save variables and controls created at runtime?
• About Propertygrid--How can I sort the Category?
• How to get text of submenu items without adding events to each(ContextMenuStrip)
• How to access methods of MDI Child from by MDI Parent Toolbar?