OpenOffice has a series of filters that allow it to work with documents in a variety of different formats; the binary version of Word's document format - .doc - is one of these so you could use the application. My only concern with OpenOffice is that sometimes the way it renders more complex documents differs from the way Word would render them and I worry that the filters may not be fully able to cope with complex documents although I have no evidence to support that conclusion. The other problem with OpenOffice from your point of view would be the learning curve associated with UNO - it is quite steep and the documentation does not help at all IMO.
As you are working on a PC, is it running Windows? If so then you may want to look at OLE. This technique would allow you to control an instance of Word itself and to use that to search and replace for you. Recently, I got together some SWT/OLE code and posted it here; http://rapidshare.com/files/262148932/Word_-_Search_and_Replace.rar in response to a request from another list member. It contains the SWT/OLE code to perform search and replace operations and you might be interested in compiling and running it. If you want the code, follow that link, choose the free download option and you will be able to download the rar archive. Unzip this to a folder on your PC and then look at the readme.rtf file as it explains how to set up your PC so that you can compile and then run the java code. If you do run the code, don't be surprised if it seems to run quite slowly. The code has to load and run Word first then open the file, search for and replace the terms and then save it all away again - the delay can seem quite disconcerting the first time. Remember though, it will only run under Windows and on a PC that has Word installed. Also, this is not production code, merely test/demonstration code so do not use it on anything important until you have throughly tested it please. Finally, I will be looking through some old folders/discs today and will dig out the HWPF search/replace code I was working on and post it here once I do. Yours Mark B karthik-33 wrote: > > Thanks for the reply mark. > I dont think i need to preserve text formatting, but i would like to try > your code and see how it works. > I think that would help me too. > > I cant go the open office since my business requirement is to use > microsoft > word documents. > I will be using this search and replace function in the same PC as that of > the application. > > If you can send me that code, i will try and let u know how it works. > > Thanks > Karthik > > > On Fri, Aug 7, 2009 at 11:49 AM, MSB <[email protected]> wrote: > >> >> Can I ask two questions please? >> >> Do you need to preserve the formatting applied to the text? If not, then >> I >> think that somewhere I have a piece of HWPF code that does a search and >> replace. I am not at all certain about the state of the code and cannot >> remember if I hit the same problem as you - and I may well have - but I >> am >> willing to look it out if you think it might help. >> >> Secondly, do you have to use HWPF/XWPF? The API is still immature and it >> is >> really only suitable for realtively simple tasks. Better alternatives >> might >> be OpenOffice which you can 'control' through it's UNO API or Word itself >> that can be manipulated using OLE. You can ONLY use OLE if you are >> working >> on a windows based PC and you have Word installed on that PC. OpenOffice >> is >> more flexible but it still cannot be used - at least as far as I am aware >> - >> as a document server, so it is best to have that application installed on >> the PC you will be using for the search/replace operation. >> >> Yours >> >> Mark B >> >> >> karthik-33 wrote: >> > >> > I have microsoft office 2007 and while saving the document, i save it >> as >> > microsoft 2003 document. >> > Iam trying to replace the text using replaceText method in Paragraph. >> > It works fine when the replacement text and search text are of equal >> > length. >> > It corrupts the document, when the length of the string is either >> greater >> > or >> > less. >> > If anyone has gone through the issue and resolved or have any idea. >> Please >> > let me know, it will be useful for me.. >> > Iam not sure what is causing the problem to corrupt the document >> > >> > Code is: >> > >> > String replaceTxt = "Replacement"; >> > String searchText = "Orginial"; >> > POIFSFileSystem ps = new POIFSFileSystem (new >> > FileInputStream("C:/Document.doc")); >> > HWPFDocument doc = new HWPFDocument (); >> > Range range = doc.getRange(); >> > for(int x=0;x<range.numSections();x++) >> > { >> > Section s = range.getSection(x); >> > for(int y=0;y<s.numParagraphs();y++) >> > { >> > Paragraph p = s.getParagraph(y); >> > String paraText = p.text(); >> > int offset = paraText.indexOf(searchText ); >> > if(offset != -1) >> > { >> > p.replaceText(searchText,replaceTxt,offset); >> > >> > } >> > } >> > >> > } >> > >> > >> >> -- >> View this message in context: >> http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24867251.html >> Sent from the POI - User mailing list archive at Nabble.com. >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: [email protected] >> For additional commands, e-mail: [email protected] >> >> > > > -- > karthik > > -- View this message in context: http://www.nabble.com/Replace-Text-Problem-%28Document-Corrupt%29---POI-HWPFDocument-tp24864855p24875568.html Sent from the POI - User mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
