Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 19th, 2008, 08:18 PM
asinclair asinclair is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 asinclair User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 34 sec
Reputation Power: 0
Find/Replace in a word document

I am writing a workflow in which a word file has certain markers replaced by predefined values and saved under a different name, but I can't seem to get the syntax right to actually do the find/replace action.

I have tried using document.range.find.execute methods, as well as selection.find.execute methods, and neither work.

The problem seems that i can't get the correct syntax to pass the wdReplaceAll parameter. In VBA documentation the syntax would be Execute(replace:=wdReplaceAll) but that doesn't compile in pure vbscript.

Sample Code:
vb Code:
Original - vb Code
  1. Sub ReplaceText(tWhat, tWith)
  2.     Set myRange = objDoc.ActiveDocument.Range(0,objDoc.ActiveDocumen  t.Characters.Count)
  3.     Wscript.echo myRange.text
  4.     ' the above DOES pop-up the expected value.
  5.     With myRange.Find
  6.         .Replacement.ClearFormatting
  7.         .ClearFormatting
  8.             .Text = tWhat
  9.             .Replacement.Text = tWith
  10.             .Forward = True
  11.             .Wrap = wdFindContinue
  12.             .Format = False
  13.         .Execute("wdReplaceAll")
  14.     End With
  15. End sub


Moderator's Note: Syntax highlighting added by Nilpo.

Last edited by Nilpo : June 4th, 2008 at 10:36 PM.

Reply With Quote
  #2  
Old May 20th, 2008, 02:56 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,903 Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)  Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 2 Days 10 h 50 m 9 sec
Reputation Power: 969
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
First off, you need to define the wdReplaceAll constant. It does not become available when you instantiate the Word object.
vb Code:
Original - vb Code
  1. Const wdReplaceAll = 2
You can also simplify the next statement.
vb Code:
Original - vb Code
  1. Set myRange = objDoc.ActiveDocument.Range(0,objDoc.ActiveDocumen  t.Characters.Count)
You're using a Range to select the entire contents of the document. This can be achieved by calling the Range method without any parameters.
vb Code:
Original - vb Code
  1. Set myRange = objDoc.ActiveDocument.Range()
The problem here seems to be a little syntax issue in the Execute method. Here's a simple Find/Replace example. You can use either a selection or a range at your own discretion.
vb Code:
Original - vb Code
  1. Set objWord = CreateObject("Word.Application")
  2. objWord.Visible = False
  3. Set objDoc = objWord.Documents.Open("mydoc.doc")
  4.  
  5. Sub ReplaceText(tWhat, tWith)
  6.     Const wdFindContinue = 1
  7.     Const wdReplaceAll  = 2
  8.     Set objSelection = objWord.Selection 'or Set objRange = objDoc.ActiveDocument.Range()
  9.    
  10.     With objSelection.Find 'or With objRange.Find
  11.         .Text = tWhat
  12.         .Forward = True
  13.         .MatchWholeWord = True
  14.         .Wrap = wdFindContinue
  15.         .Format = False
  16.    
  17.         .Replacement.Text = tWith
  18.         .Execute ,,,,,,,,,,wdReplaceAll
  19.     End With
  20. End Sub
Be careful using the ClearFormatting methods. They can lead to some unexpected results and your saved document may have some expected formatting removed.
__________________
Don't like me? Click it.

Scripting problems? Windows questions? Ask the Windows Guru!

Stay up to date with all of my latest content. Follow me on Twitter!

Help us help you! Post your exact error message with these easy tips!

Reply With Quote
Reply

Viewing: ASP Free ForumsSystem AdministrationWindows Scripting > Find/Replace in a word document


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek