How to work with word document?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

51 Replies - 2274 Views - Last Post: 19 January 2013 - 09:25 AM Rate Topic: -----

#1 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

How to work with word document?

Posted 11 January 2013 - 08:45 AM

Hello,

I've just starte to program on C# and trying to develop an application that translits the text from Word document and outputs a new Word document.

I've placed a button to give the user the ability tu choose file from his computer using the OpenFileDialog class. Now I'm confused how to read text from Word document and then do some operations on it like TRANSLIT and then save the output to another new Word file.
I think to use Microsoft.Office.Interop.Word namespace, but seems that it is not available in Visual Studio Express..

Any other solutions?

thank you

Is This A Good Question/Topic? 0
  • +

Replies To: How to work with word document?

#2 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 218
  • Joined: 25-April 11

Re: How to work with word document?

Posted 11 January 2013 - 08:53 AM

I don't know the Express Version of VS but I don't think this has anything to do with. Just follow theese instructions: http://msdn.microsof...y/kh3965hw.aspx

And here you could find some help, if you are new to programming.
http://www.dotnetperls.com/word
Was This Post Helpful? 1
  • +
  • -

#3 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 11 January 2013 - 11:19 AM

View PostAnthonidas, on 11 January 2013 - 08:53 AM, said:

I don't know the Express Version of VS but I don't think this has anything to do with. Just follow theese instructions: http://msdn.microsof...y/kh3965hw.aspx

And here you could find some help, if you are new to programming.
http://www.dotnetperls.com/word


Thank you very much, but this is for the Ofice 2010, how can I develop add-in for Office 2007 and 2003?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,397
  • Joined: 12-December 12

Re: How to work with word document?

Posted 11 January 2013 - 11:32 AM

Choose the Project Menu, Add Reference, and click into the COM section or tab. Find and tick Microsoft Word 11.0 (or 12.0) Object Library. (14.0 would be for Office 2010.)

The following code is for VB.Net but is almost identical for C#.

Imports Microsoft.Office.Interop

Module Module1

    Sub Main()
        Dim word As Word.Application
        Dim doc As Word.Document

        Try
            word = New Word.Application
            word.Visible = True
            doc = word.Documents.Add
            'doc = word.Documents.Open("C:\Something\Somewhere.docx")

            word.Selection.TypeText("Hello Word!")
            word.Selection.TypeParagraph()
            word.Selection.TypeParagraph()
            word.Selection.TypeText("Goodbye Word")
            doc.Paragraphs(1).Range.Font.Bold = True
            doc.SaveAs("C:\Users\Andrew\Documents\Somewhere")
            word.Quit()

        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        Finally
            doc = Nothing
            word = Nothing
        End Try
    End Sub

End Module



This is for Visual Studio and Express - it's VSTO that is not available in the Express version.

This post has been edited by andrewsw: 11 January 2013 - 11:40 AM

Was This Post Helpful? 1
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,397
  • Joined: 12-December 12

Re: How to work with word document?

Posted 11 January 2013 - 12:17 PM

The following works in C# but needs a bit of tidying:

using System;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace ConsoleAppWord {
    class Program {
        static void Main(string[] args) {
            Application wdApp = new Application();
            Document wdDoc;

            try {
                wdApp.Visible = true;
                //wdDoc = wdApp.Documents.Add();
                wdDoc = wdApp.Documents.Open("C:\\Users\\Andrew\\Documents\\Somewhere.docx");
                wdApp.Selection.EndKey(6); // wdStory
                wdApp.Selection.TypeText("Hello Word!");
                wdApp.Selection.TypeParagraph();
                wdApp.Selection.TypeParagraph();
                wdApp.Selection.TypeText("Goodbye Word!");
                wdDoc.Paragraphs[1].Range.Font.Bold = 1;
                wdDoc.SaveAs("C:\\Users\\Andrew\\Documents\\Somewhere");
                wdApp.Quit();

            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                throw;
            } finally {
            }
        }
    }
}


Was This Post Helpful? 1
  • +
  • -

#6 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 11 January 2013 - 03:26 PM

Thank you very much, it helped a lot.
But I still have question, how can I send this word document to class?
I actually don't need to open a word file, but I just need to get the chooden file by user and send it to the class as an argument, and then in my class there are some methods that will deal with this word document and produce a new word document.



View Postandrewsw, on 11 January 2013 - 12:17 PM, said:

The following works in C# but needs a bit of tidying:

using System;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace ConsoleAppWord {
    class Program {
        static void Main(string[] args) {
            Application wdApp = new Application();
            Document wdDoc;

            try {
                wdApp.Visible = true;
                //wdDoc = wdApp.Documents.Add();
                wdDoc = wdApp.Documents.Open("C:\\Users\\Andrew\\Documents\\Somewhere.docx");
                wdApp.Selection.EndKey(6); // wdStory
                wdApp.Selection.TypeText("Hello Word!");
                wdApp.Selection.TypeParagraph();
                wdApp.Selection.TypeParagraph();
                wdApp.Selection.TypeText("Goodbye Word!");
                wdDoc.Paragraphs[1].Range.Font.Bold = 1;
                wdDoc.SaveAs("C:\\Users\\Andrew\\Documents\\Somewhere");
                wdApp.Quit();

            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                throw;
            } finally {
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,397
  • Joined: 12-December 12

Re: How to work with word document?

Posted 11 January 2013 - 03:37 PM

Quote

But I still have question, how can I send this word document to class?

You just pass the Document object as an argument to the class(?); but you'll need to ensure that the Word resource is correctly disposed of when you've finished with the document.

Quote

I actually don't need to open a word file, but I just need to get the chooden file by user and send it to the class as an argument, and then in my class there are some methods that will deal with this word document and produce a new word document.


Depends what you mean by deal with this word document. If you will only be renaming or copying it then you wouldn't need Office-Interop anyway. For anything else to do with the content of the document (reading, amending, formatting) you would have to open it(?).
Was This Post Helpful? 0
  • +
  • -

#8 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 11 January 2013 - 03:46 PM

I mean that, I have opened the file with
wdApp.Visible = true;
                
     wdDoc = wdApp.Documents.Open(fileName);


and now I need to send this wdDoc to my class, and inside class I will open the file, get the content, search for string, exchange those strings and save the changes in a new word file.
Was This Post Helpful? 0
  • +
  • -

#9 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 11 January 2013 - 03:51 PM

Actually I don't need to open it, I just need to get the file.

View Postasem0525, on 11 January 2013 - 03:46 PM, said:

I mean that, I have opened the file with
wdApp.Visible = true;
                
     wdDoc = wdApp.Documents.Open(fileName);


and now I need to send this wdDoc to my class, and inside class I will open the file, get the content, search for string, exchange those strings and save the changes in a new word file.

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,397
  • Joined: 12-December 12

Re: How to work with word document?

Posted 11 January 2013 - 04:00 PM

You can either pass the document to your classes' method, so your method would look like this perhaps:

public void ProcessDocument(Document aDoc) {
    // do great things..
}


But if you are going to open the document within this method then you would just pass the method the filename (as a string) and it would do the opening :mellow:/>/>

Erm.. if you don't need to open it.. then this whole Interop business is redundant :whistling:/>. Interesting, but redundant?!

This post has been edited by andrewsw: 11 January 2013 - 04:01 PM

Was This Post Helpful? 2
  • +
  • -

#11 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 12 January 2013 - 05:59 AM

Yes probably you're right, but what shoulf I use then if not Interop?
Yes, I'm gonna send the file to the method, and inside the method I want to search for string inside the document, but don't need to open it.

View Postandrewsw, on 11 January 2013 - 04:00 PM, said:

You can either pass the document to your classes' method, so your method would look like this perhaps:

public void ProcessDocument(Document aDoc) {
    // do great things..
}


But if you are going to open the document within this method then you would just pass the method the filename (as a string) and it would do the opening :mellow:/>/>/>

Erm.. if you don't need to open it.. then this whole Interop business is redundant :whistling:/>/>. Interesting, but redundant?!

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3608
  • View blog
  • Posts: 12,397
  • Joined: 12-December 12

Re: How to work with word document?

Posted 12 January 2013 - 07:56 AM

I suppose it is possible to search the document without opening it (as a Word document). It might be possible to use standard File methods - treating it as a binary file(?). It's really a zipped set of XML documents though..

I can't really elaborate unless you describe what exactly you need to do with the document; you haven't yet made this clear :whistling:.

Nevertheless, I don't see what is wrong with opening the document (keeping it not visible), use the rich method-set from the Word Object Library, then silently close the document. But you must have your reasons?!
Was This Post Helpful? 0
  • +
  • -

#13 asem0525  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-December 07

Re: How to work with word document?

Posted 12 January 2013 - 08:10 AM

another question..

If I want to be able to modify the text from different files, not only word, but Excel, PDF, XPS, HTM and etc, text files, what namespace should I use?

So, from the Form the user chooses the test file, and then this text file uses the method from my class that gets the whole text, searches, exchanges, modifies and produces a new document in the same format.

In this case which namespace or class is best to use?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: How to work with word document?

Posted 12 January 2013 - 11:16 AM

Unless you pick up a 3rd party solution, there isn't a one size fits all namespace or class to handle all those file formats you listed.
Was This Post Helpful? 1
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,479
  • Joined: 29-May 08

Re: How to work with word document?

Posted 12 January 2013 - 01:27 PM

You could always implement those requirements yourself.
What functionality do want, abstract into a interface.

Then implement that interface in various Text Processing modules / plugins. By creating them as separate Class Libraries.

This is how a lot of software is created.
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »