?
Solved

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

Posted on 2008-10-27
6
Medium Priority
?
864 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

741 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