Solved

VS.NET 2003 & Office 2003: Word Object integration into a C# program

Posted on 2008-10-27
6
849 Views
Last Modified: 2012-05-05
Hello,

I am researching an idea. How can I pull Word (from Office, in this case Office 2003) into a standard C# program?

Put simply, I'd like to wrap the entirety of Word 2003 inside my program. And be able to store the resulting text in SQL.

Thanks,
Ryan
Word.jpg
0
Comment
Question by:rossryan
  • 2
  • 2
6 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22812757
Hello,
Heres some code for reading all text from a word document into your project.
You do all these in say for exampla a button click event which will copy all the text from word document and put it in a variable, then using that variable you can do whatever you want with the text.

First of all,
1) The First Step is that you'll need to add a COM reference to your project by right clicking in the solution explorer on References->Add Reference. Click on the COM tab and look for the Microsoft Word 9.0 OR 10.0 Object Library. Click Select and OK.
(Please note, if you do not see these kind of library references in the dialog box, while installing office you haven't select the .NET Programmability support option which comes in advanced setup step)

2)
then in a button's click event paste this code

Word.Application app = new Word.ApplicationClass();
        object nullobj = System.Reflection.Missing.Value;
        object file = @"C:\testFile.doc";
        Word.Document doc = app.Documents.Open(
    ref file, ref nullobj, ref nullobj,  
    ref nullobj, ref nullobj, ref nullobj,  
    ref nullobj, ref nullobj, ref nullobj,  
    ref nullobj, ref nullobj, ref nullobj,  
    ref nullobj, ref nullobj, ref nullobj);
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        IDataObject data = Clipboard.GetDataObject();
        string strDoctext = data.GetData(DataFormats.Text).ToString();

        doc.Close(ref nullobj, ref nullobj, ref nullobj);
        app.Quit(ref nullobj, ref nullobj, ref nullobj);

Now the variable strDocText holds all of the text from your word document.
Hope it helped.

Enjoy Coding!
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 22812895
0
 

Author Comment

by:rossryan
ID: 22817980
Nice. But what I am trying to do is put the whole Word Object into my Form.

I am not up on COM programming. Does anyone have any information on pull the entire Word editor (meaning: at least the spellchecking + formatting options) into the C# world?

The code above implies that it's possible (at least a portion of Word can be inserted the C# world via COM programming). I'm just asking if anyone has attempted this, and if so, to please share any information related to your experience (things not to do, things to look out for, things to speed me along to accomplishing my task).

To shortcut things, here's the belly of the problem: my program needs to make use of a certain spellchecker. Rather than code up a new text interface (and some adhoc interface to make use of said commercial spellchecker (for a medical and legal field, imagine the sheer number of words)), I got the idea of pulling Word into my world (and making sure that the aforementioned spellchecker was installed with Word), enabling me to make use of the spellchecker AND Word's capabilities. If I can make use of Word's capabilities, it makes my program look "hotter".

<BADCAR ANAOLOGY>
What computer (in a standard business environment) doesn't have Windows + Office installed? Rather than reinvent the wheel, I'd like to buy a few, and latch them onto the car I'm building. </BADCAR ANAOLOGY>

Thank You,
Ryan Ross  
0
 

Author Comment

by:rossryan
ID: 22987631
Right, then let me change the question to this: How would I add a Word object (Office 2003) to a form in VS.NET 2008? There are projects to create Word documents, but I am not up to speed yet on VS.NET 2008.

Thanks,
Ryan
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22997082
Hia, try this

http://www.codeproject.com/KB/miscctrl/winwordcontrol.aspx
This control allows you to use MS Word 2000 in your own projects such as a windows form.

It is in c++ .net but I guess as a dll is to be used anywhere, it should work with c#.
Just read the article, download the sourcecode and use it.

Hope it helped!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question