View Full Version : Dynamic search for a stirng in word doumnets using javascript

12-28-2011, 01:50 PM

In one folder for ex: C:\NewFolder , I have so many word documents in this folder. Now what I have to do is, need to search for any string and it should search for that string in all documents inside that folder and should show output as the name of the docs where it found (same like windows search output) and i should be able to open those docs.

I want to do this using Javascript. I'm new to javascripting.

Kindly help me

12-28-2011, 02:16 PM
Wouldn't something like this be easier than writing your own code to do this: http://answers.microsoft.com/en-us/windows/forum/windows_7-files/in-windows-7-i-want-to-search-for-all-files/aadfe1f1-4a33-406b-8e72-bb920efa4f30

12-28-2011, 02:42 PM
Wouldn't something like this be easier than writing your own code to do this: http://answers.microsoft.com/en-us/windows/forum/windows_7-files/in-windows-7-i-want-to-search-for-all-files/aadfe1f1-4a33-406b-8e72-bb920efa4f30

thanks for your reply.... Here what i'm doing is storing all incident reports in another page written in javascript... so i want this in javascript only.
plz help me.

12-28-2011, 03:06 PM
I imagine you could write/run some Windows Scripting Host (WSH) to open up all the docs in a directory and search for text in those docs. But I'm not sure that you could run that in IE with the default security settings. So you'd be better off just double clicking the JS file in your local OS, and running it from there. But if you go that route, how should the results of the search be presented to you (if not in a browser, then where?)

Old Pedant
12-28-2011, 08:20 PM
Well, anything you could do from WSH you should be able to do from the browser. Yes, you have security issues. Even if you run this via the inTRAnet, which you should, MSIE will ask you to allow the "unsafe scripting", but assuming that isn't too annoying and assuming you say "allow", then it will work the same as WSH but give you the opportunity to present the data in the browser.

Naturally, this only works with MSIE, as you have to instantiate the Word DLL as an ActiveX object.

The hard part, whether from browser or WSH, is learning all the ActiveX methods and properties that you will need to manipulate Word from scripting.

Because the docs for Word are written for VBA (Visual Basic for Applications...the language used internal to Word), you are likely to find it easier to translate them from VBA to VBScript, so you might want to consider using VBScript in the browser for all of this.

It's not a trivial task. But neither is it rocket science. It's just a lot (and I do mean a lot) of sometimes non-obvious operations that you must script. Perusing the VBA docs for Word is the best place to start.

Old Pedant
12-28-2011, 08:22 PM
And this really isn't a task for someone new to scripting. Especially if you insist on using JavaScript so that you have to translate all the VBA docs to JavaScript equivalents.

12-29-2011, 07:42 AM
Seems to me that this would be easiest accomplished with some C# and ASP.NET. Like VB and VBScript, these technologies were made for Windows; in my opinion, C# is superior to VB, VBScript, or JScript; and .NET should expose all the Windows objects, methods, and properties that you need.

And there should be no issues mixing your existing JS with C# and ASP.NET.

But to continue programming all of that in JS seems like a chore, as Old Pedant said. The only thing worse than using JS for what you are doing is to write a .bat file or something, lol :)

Old Pedant
12-29-2011, 09:34 PM
Actually, the only interface that is defined for Word is indeed the ActiveX interface, using the Word.dll library. And the only documentation for the API is given for VBA. (As I said, Visual Basic for Applications--VBA--is the language used internally by Word [and PowerPoint and Excel and Access].)

So strange as it seems, the best language to "script" Word in is VBScript. It's not a perfect match for VBA. VBA allows typed variables, so you can (example) do Dim doc As Word.Document whereas in VBScript you would just do Dim doc. But then in either case, you might follow up with Set doc = New Word.Document() so once you have the objects dimensioned and initialized, the subsequent code should be identical or nearly so.

And, yes, I do think this is better accomplished using WSH. But of course WSH can use VBScript as the scripting language just as well (and in a few cases better than) JScript.

12-30-2011, 05:55 AM
Use a server side language this isn't a task for java script. There are lots of librarys out there for reading word documents. I guess if you really wanted the info in java script you could use ajax, read the doc spit something back.

12-30-2011, 07:44 AM
You absolutely can perform a search in Word documents with .NET (and C# -- or VB, or even JScript!) using the Microsoft.Office.Interop.Word (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word(v=office.11).aspx) namespace: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.find(v=office.11).aspx

See also:


Edit: So if you open up each of your Word documents in your directory and run a Microsoft.Office.Interop.Word.Find.Execute (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.find.execute(v=office.11).aspx), then it will return a boolean value indicating if the search term was or was not found.

So with .NET, all you have to do is iterate through the contents of the directory, open each word document, do a Find.Execute, and then return the doc names that returned true to the browser window. Seems pretty straightforward.

Old Pedant
12-30-2011, 09:08 AM
Well, shows how out of date I am!

Last time I looked for something like this was indeed in 2001 or 2002 and the interop libraries didn't exist at that time. (Happened to be a project where I needed to script Excel, but the interop is conceptually the same for both.)

So I 100% agree with you. This is by far the best and easiest way to do the task.

Assuming that the documents to be searched are on the server, there's no excuse for using anything else.

12-30-2011, 09:14 AM
Also, if he's not using an independent web server, he could install IIS on his local Windows machine. Then everything could be done locally.