Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count patterns in MS Word document with Javascript

Status
Not open for further replies.

Petemush

Technical User
Jun 21, 2002
255
GB
Hello,

I have a word document in which I want to account the occurences of certain patterns of text such as "PW", "DW", "MH" etc.

I want the results displayed on a webpage so I figured since the machines this will run on don't have perl installed that javascript would be the next best thing. Unfortunately never used Javasript before so hence I'm asking for help!

So far by experimenting and using a providing website I've managed to retrieve the MS Word document text with the following code:

<html>

<head><title>snook.ca load document</title>

<script language=&quot;JavaScript&quot;>

<!--//

function loadworddoc(){

var doc = new ActiveXObject(&quot;Word.Application&quot;); // creates the word object

doc.Visible=false; // doesn't display Word window

doc.Documents.Open(&quot;X:\\DEV\\Retail Systems\\Projects\\COPOS\\60 - System Test Specifications\\Working Documents\\Registers\\Issues Log Registers Phase 1.doc&quot;); // specify path to document



//copy the content from my word document and throw it into my variable

var txt;

txt = doc.Documents(&quot;X:\\DEV\\Retail Systems\\Projects\\COPOS\\60 - System Test Specifications\\Working Documents\\Registers\\Issues Log Registers Phase 1.doc&quot;).Content;



document.all.myarea.value = txt;

document.all.myarea.value.substring(50, 70);

doucment.write(txt, &quot;<BR>&quot;);

doc.quit(0); // quit word (very important or you'll quickly chew up memory!)

}

//-->

</script>

</head>

<body>

<p><input type=button onClick=&quot;loadworddoc();&quot; value=&quot;Load&quot;>

<p><textarea name=myarea cols=50 rows=5>nothing here yet</textarea>

</body>

</html>


Just not sure how to search through what I've got and provide some results. Tried looking at regex but couldn't see anything that would do it.

Any ideas?

Cheers,

Pete
 
The first line should read:

I have a word document in which I want to count the occurences of certain patterns of text such as &quot;PW&quot;, &quot;DW&quot;, &quot;MH&quot; etc.
 
I'm guessing that at this point
Code:
txt = doc.Documents(&quot;X:\\DEV\\Retail Systems\\Projects\\COPOS\\60 - System Test Specifications\\Working Documents\\Registers\\Issues Log Registers Phase 1.doc&quot;).Content; 
doc.quit(0);
you have all the text from the word doc in the variable txt?

Now you can do all the pattern matching you like with regular expression matching on that variable.

See here ( for a javascript regexp intro, or here might be good: (JScript rather than JavaScript but could be useful).

Good luck

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks for the link, hopefully I'll be able to muddle through now!

Cheers,

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top