By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

Search and Replace in a MS Word Document with javacript

P: 8
Hi
All
I got a MS Word Document and Id like to do Search and Replace on it with Javascript.

Is there any function javascript that do this, to send me?

[EDIT]



Thanks
Dec 13 '07 #1
Share this Question
Share on Google+
17 Replies


gits
Expert Mod 5K+
P: 5,328
hi ...

welcome to TSDN ...

please DON'T post your email-address in the forums ... i've removed it for your own security ... i think you won't get spammed :) and at least it's against the posting rules ...

to your problem: why do you want to do that? word has its own search and replace method. btw. i think there is no possibility to do that with javascript since javascript is strictly bound to the browser and a webpage (html, xml) ...

kind regards
Dec 13 '07 #2

P: 8
I think there is one way, but I'm not sure:
Look at this example:

In C:\\Java\\MyDocument.doc, I add " Add My Texto " in the beginning of file.
So i thought there is one way to replace in the variable docText and save again the file.

This javascript open word document get the docText and add that text : (" Add My Texto " in beginning of file.


Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var w=new ActiveXObject('Word.Application');
  3. var obj;
  4. if (w != null)
  5. {
  6. w.Visible = true;
  7. obj=w.Documents.Open("C:\\Java\\MyDocument.doc");
  8. var docText = new String(obj.Content);
  9. w.Selection.TypeText(" Add My Texto ");
  10. w.Documents.Save();
  11.  
  12. }
  13.  
  14. </script>
Dec 13 '07 #3

gits
Expert Mod 5K+
P: 5,328
in this code you use an activeX-object ... it's IE specific stuff ... and therefore i cannot test it to help you :) ... but could you post what obj.Content is? is it plain text of the docs content?
Dec 13 '07 #4

P: 8
Thanks for reply me.


Its plain ms word document content, but I need to know how to replace text into my Ms Word document content, example replace 'A' for 'B'.

How can I do this?

Thsnks
Dec 13 '07 #5

gits
Expert Mod 5K+
P: 5,328
you may use a regExp for that ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. var text = 'AB BBB BCA';
  2.  
  3. text = text.replace(/A/g, 'B');
  4.  
  5. alert(text);
  6.  
kind regards
Dec 13 '07 #6

P: 8
I have to do this into my MS Word Documento not variable javasript.

Thanks
Dec 13 '07 #7

gits
Expert Mod 5K+
P: 5,328
the variable is an example ... put obj.Content here ...
Dec 13 '07 #8

P: 8
Here is my content:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var w=new ActiveXObject('Word.Application');
  3. var obj;
  4. if (w != null)
  5. {
  6. w.Visible = true;
  7. obj=w.Documents.Open("C:\\Java\\MyDocument.doc");
  8.  
  9. //===========here is my content coming from MyDocument.doc===
  10. var docText = obj.Content;
  11.  
  12.  
  13.  
  14. //====here i save the content===================
  15. w.Documents.Save();
  16.  
  17. }
  18.  
  19. </script>
Dec 13 '07 #9

gits
Expert Mod 5K+
P: 5,328
:D so now try this:

Expand|Select|Wrap|Line Numbers
  1. var w = new ActiveXObject('Word.Application');
  2. var obj;
  3.  
  4. if (w != null) {
  5.     w.Visible = true;
  6.     obj = w.Documents.Open("C:\\Java\\MyDocument.doc");
  7.  
  8.     //===========here is my content coming from MyDocument.doc===
  9.     var docText = obj.Content;
  10.  
  11.     docText = docText.replace(/A/g, 'B');
  12.  
  13.     //====here i save the content===================
  14.     w.Documents.Save();
  15. }
  16.  
kind regards
Dec 13 '07 #10

P: 4
@gits
Hello All,

I tried the above code but its giving the following error:
"Object doesnot support this property or method" on the line where there is a replace code written.
Could anybody please look into it..

With Best Regards,
Rohit
May 7 '09 #11

gits
Expert Mod 5K+
P: 5,328
what does:

Expand|Select|Wrap|Line Numbers
  1. alert(typeof docText);
say?
May 7 '09 #12

P: 4
Hello all,

I have attached the following code. Here i have a word doc by the name Qualification_Report.doc. I want to replace all 'p' occurances in that by B. But it gives the following error "Object doesnot support this property or method". Could anyone please look into it.

Expand|Select|Wrap|Line Numbers
  1. var oApplication=new ActiveXObject("Word.Application");
  2.             oApplication.Documents.Open("c:\\Qualification_Report.doc");
  3.             var oDocument= oApplication.ActiveDocument;
  4.  
  5.              var docText = oDocument.Content;
  6.  
  7.              docText = docText.replace(/p/g,'B');
Thanks in advance
Rohit
May 7 '09 #13

gits
Expert Mod 5K+
P: 5,328
after assigning the docText variable what does the alert i showed you above tell you? is it 'string'?
May 7 '09 #14

P: 4
It says that docText is an "object"
May 8 '09 #15

P: 4
I converted it into string.. then the changes are applied on the javascript but are not reflected in the word document.

That is all the 'p' is getting converted and can be checked using alert statement. But 'p' remain as it is in the word document.(even after saving the file using the command .Documents.save())
May 8 '09 #16

gits
Expert Mod 5K+
P: 5,328
may be that you need to save it to the word-document first through a method of the ActiveX control first? ... i would suggest to look/ask in the msdn forum ... unless anybody here could give you a better suggestion ...

kind regards
May 8 '09 #17

acoder
Expert Mod 15k+
P: 16,027
I think Content is a range, so you would need to use the .text property. If you set that to the replaced string, perhaps it will work.
May 8 '09 #18

Post your reply

Sign in to post your reply or Sign up for a free account.