Solved

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

Posted on 2008-10-27
6
836 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 69

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now