Solved

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

Posted on 2008-10-27
6
852 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 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
Where does legacy ASP.NET initialize language? 2 98
I need help adding validation to my MVC.Net view 6 34
Windows Universal App 22 55
Pdf not loading 2 38
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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