473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Search and Replace in a MS Word Document with javacript

Hi
All
I got a MS Word Document and I´d 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
17 11683
gits
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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
Thanks for reply me.


It´s 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
5,390 Expert Mod 4TB
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
I have to do this into my MS Word Documento not variable javasript.

Thanks
Dec 13 '07 #7
gits
5,390 Expert Mod 4TB
the variable is an example ... put obj.Content here ...
Dec 13 '07 #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
5,390 Expert Mod 4TB
: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
@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
5,390 Expert Mod 4TB
what does:

Expand|Select|Wrap|Line Numbers
  1. alert(typeof docText);
say?
May 7 '09 #12
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
5,390 Expert Mod 4TB
after assigning the docText variable what does the alert i showed you above tell you? is it 'string'?
May 7 '09 #14
It says that docText is an "object"
May 8 '09 #15
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
5,390 Expert Mod 4TB
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
16,027 Expert Mod 8TB
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

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

Similar topics

1
by: Les Juby | last post by:
A year or two back I needed a search script to scan thru HTML files on a client site. Usual sorta thing. A quick search turned up a neat script that provided great search results. It was fast,...
4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
14
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like...
4
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional...
0
by: Anton | last post by:
Hi All, Is it possible to read through a word document, find a number of strings (using wildcards) and prompt the user to enter values for them. I have a document management database that builds...
5
by: Carstonio | last post by:
I use ASP to display links to Word documents on an intranet. Is there a way in ASP to do text searches on the documents' contents? I want the results to have the link to the Word document plus two...
2
by: Ola K | last post by:
Hi guys, I wrote a script that works *almost* perfectly, and this lack of perfection simply puzzles me. I simply cannot point the whys, so any help on it will be appreciated. I paste it all here,...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
5
by: silmana | last post by:
Hi i have this script that i want to use as php or html but i cant find the problem, could anyone solve the problem, i dont know why i cannot use it in php or html file // OBS! Några saker måste...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.