Free Support Forum - aspose.com

Find key words in the document

Hello,



I am new to Aspose.Words. I have a document that contains several key
words that start with . For example Notional,
Sponsor, they are all one word. Is it possible to find those
words in the document by using aspose.words?



Thank you



Alan

Hey Alan,

Thank you for your question. Find and replace functionality is described in the following topic:

http://www.aspose.com/wiki/default.aspx/Aspose.Words/WorkingWithRange.html,

see the Find and Replace section. Basically, the concrete implementation depends on why you need those keywords and what you are going to do next. If you tell me that, maybe I could suggest a more suitable technique for you.

Hi,



thank you for the quick reply. The purpose of keyword is to automate
the document be DB driven. For example, a document contains sponsor and notional
information. For all the sponsors, the documents are the same except sponsor name and
notional amount. We could like to create a template, that has key words
Sponsor and Notional in
it
, and use ASpose.words to replace those key words with database
field.



I looked range example from your web site last night. It looks
like that it almost does what we need except two things.

  1. Is it possible to
    replace a word with a table or a chart?
  2. At the example, you listed
    “[s|m]ad” which will find all “sad” and “mad” in doc. Is it possible to find all
    the words that start with ? ie. Notional,
    Sponsor, fee, tradedate.



    Thank you

1) Tables - if you mean Microsoft Word tables then yes, of course, but no Excel tables or OLE charts are available to insert.

2) Sure - use the power of regular expressions to find anything you want Smile [:)]

The sample below replaces all occurrences of the words starting with with a simple table:

public void TestBad()

{

Document doc = new Document("Source.doc");

doc.Range.Replace(new Regex(@"\\w+"), new ReplaceEvaluator(ReplaceEvaluator), true);

doc.Save("Result.doc");

}

private ReplaceAction ReplaceEvaluator(object sender, ReplaceEvaluatorArgs args)

{

DocumentBuilder builder = new DocumentBuilder(args.MatchNode.Document);

builder.MoveTo(args.MatchNode);

// Build a table or anything you need here.

builder.InsertCell();

builder.Write("Cell 1");

builder.InsertCell();

builder.Write("Cell 2");

return ReplaceAction.Replace;

}

Thank you SO much for the help. The sample makes the logic much clear now.



I got another question, hope it’s the last question for the day.
:slight_smile: Sometime my template could be opened by another user for
editing through MS-Word, is it possible for Aspose to open a word
document with readonly attribute while it’s being opened by others? I
know MS-word could do a Documents.Open ReadOnly = true, I haven’t found
the same function in aspose.words yet.



I appreciate for your help!!



Alan

Hmm. I think I haven't got the point. If you are planning to open a document using Aspose.Words while it is opened in Microsoft Word in read-only mode, it will open okay. If the mode is not read-only, Microsoft Word will lock the file and Aspose.Words will throw an exception. But it does not make sense to introduce read-only mode in Aspose.Words because it does not lock the source file. It reads the file, loads it into the model in memory and closes the input stream.

Did I misunderstand you?

I see your points.

Well, my case was “Person A is currently using MS-word modifying the
template. Person B starts the application that uses aspose to read
template, replace some keywords”. In this case, person B will get an
exception thrown from aspose. I was asking if there is a readonly
attribute in aspose.words open function, that allows user to open a
document regardless whether its been locked.



Thank you

Alan


Okay, then I understood you correctly. But no read-only attribute would help here because Microsoft Word process captures the file while it’s opened and Windows raises an error when trying to access the file from wherever else. I mean there’s no way to overcome this at all.