51 Replies - 1060 Views - Last Post: 19 January 2013 - 09:25 AM
#1
How to work with word document?
Posted 11 January 2013 - 08:45 AM
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
Replies To: How to work with word document?
#2
Re: How to work with word document?
Posted 11 January 2013 - 08:53 AM
And here you could find some help, if you are new to programming.
http://www.dotnetperls.com/word
#3
Re: How to work with word document?
Posted 11 January 2013 - 11:19 AM
Anthonidas, on 11 January 2013 - 08:53 AM, said:
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?
#4
Re: How to work with word document?
Posted 11 January 2013 - 11:32 AM
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
#5
Re: How to work with word document?
Posted 11 January 2013 - 12:17 PM
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 {
}
}
}
}
#6
Re: How to work with word document?
Posted 11 January 2013 - 03:26 PM
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.
andrewsw, on 11 January 2013 - 12:17 PM, said:
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 {
}
}
}
}
#7
Re: How to work with word document?
Posted 11 January 2013 - 03:37 PM
Quote
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
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(?).
#8
Re: How to work with word document?
Posted 11 January 2013 - 03:46 PM
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.
#9
Re: How to work with word document?
Posted 11 January 2013 - 03:51 PM
asem0525, on 11 January 2013 - 03:46 PM, said:
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.
#10
Re: How to work with word document?
Posted 11 January 2013 - 04:00 PM
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
Erm.. if you don't need to open it.. then this whole Interop business is redundant
This post has been edited by andrewsw: 11 January 2013 - 04:01 PM
#11
Re: How to work with word document?
Posted 12 January 2013 - 05:59 AM
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.
andrewsw, on 11 January 2013 - 04:00 PM, said:
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
Erm.. if you don't need to open it.. then this whole Interop business is redundant
#12
Re: How to work with word document?
Posted 12 January 2013 - 07:56 AM
I can't really elaborate unless you describe what exactly you need to do with the document; you haven't yet made this clear
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?!
#13
Re: How to work with word document?
Posted 12 January 2013 - 08:10 AM
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?
#14
Re: How to work with word document?
Posted 12 January 2013 - 11:16 AM
#15
Re: How to work with word document?
Posted 12 January 2013 - 01:27 PM
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.
|
|

New Topic/Question
Reply




MultiQuote




|