Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 17, 2011 11:52 AM
Hi i want to open an existing word document in my windows form application! i want to open it by clicking from a menu item!!! it is to open the user documentation of the application!! i tried using most of the codes but none of them gave me successful results
this is the code i used:
this.Application.Documents.Open(@"D:\HD Project\User Documentation.docx");
and i used the following header for this
using Microsoft.Office.Interop.Word;
this is another code
Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
wdApp.Visible = true;
Word.Document aDoc = wdApp.Documents.Open(@"D:\HD Project\User Documentation.docx");
Can some one please tell me what the problem is??
thanks in advance
All replies (7)
Friday, November 18, 2011 10:26 AM âś…Answered
Declaring the namespace as you did, 'using Microsoft.Office.Interop.Word;', is fine. If you do this:
Word.Document aDoc
should be
Document aDoc
You can also declare your variables the long way, which will work even if you do not declare the namespace. You have done this for your word application (Microsoft.Office.Interop.Word.Application wdApp). You could also do this for the document (Microsoft.Office.Interop.Word.Document aDoc)
Also, There is clearly some problem with the references. You have a duplicate; two referrences point to instances of the Word Object Library. In the solution explorer, expand the 'references' section, and then delete one. If you right click each and look at the properties, you can see the library file they point to.
Thursday, November 17, 2011 2:27 PM
using System.IO;
FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);
try
{
// read from file or write to file
}
finally
{
fileStream.Close();
}
Techie Marked as answer if it is useful http://royalarun.blogspot.com/
Thursday, November 17, 2011 6:18 PM
Hi Mirfath
The first line of code can't work as "this" in a Windows Forms application refers to the Windows Form. And that cannot open a Word document.
The lines of code starting with "using Microsoft.Office.Interop.Word" I would expect to work, assuming the Word application is installed on the machine where your Windows Form is installed and running.
Unfortunately, since you give no information about HOW things aren't working, it's not possible to even begin to guess why it's not working for you...
Cindy Meister, VSTO/Word MVP
Friday, November 18, 2011 1:30 AM
What she (Cindy) said.
Although, a very wild and probably wrong guess: That you refer to 'using Microsoft.Office.Interop.Word' as a 'header' makes me wonder if you're more familiar with C++ than C#, and have not added references to the PIAs. In Visual Studio you would do this by going to project->add reference>COM and adding 'Microsoft Word x Object Library'.
Friday, November 18, 2011 6:49 AM
Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
wdApp.Visible = true;
Word.Document aDoc = wdApp.Documents.Open(@"D:\HD Project\User Documentation.docx");
Errors are:
Error 3 The type or namespace name 'Word' could not be found (are you missing a using directive or an assembly reference?) D:\HDProject\GOGREEN\GOGREEN\Main Menu.cs 436 13 GOGREEN
Error 1 The type 'Microsoft.Office.Interop.Word.Application' exists in both 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll' and 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Word.dll' D:\HDProject\GOGREEN\GOGREEN\Main Menu.cs 434 43 GOGREEN
Error 2 The type 'Microsoft.Office.Interop.Word.Application' exists in both 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\14.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll' and 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Word.dll' D:\HDProject\GOGREEN\GOGREEN\Main Menu.cs 434 97 GOGREEN
Friday, November 18, 2011 6:52 AM
hey i have already added it to the solution but i don't know what type of header i have to put for it! i also imported the
Microsoft.Office.Interop.Word
Friday, November 18, 2011 11:15 AM
hey thanks a million it works!! :) :) :)