Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2012, 11:07 PM
ubns ubns is offline Macro to replace few words in the document Windows 7 32bit Macro to replace few words in the document Office 2010 32bit
Competent Performer
Macro to replace few words in the document
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default Macro to replace few words in the document

Hi,

We have got a template (which is prepared by the dealer group and we have no control on changing the template).

When document opens I want to word to find and replace all the instances of "monitor" to "review".

Is it possible to do that?



Regards

Umesh Banga
Reply With Quote
  #2  
Old 08-14-2012, 11:58 PM
macropod's Avatar
macropod macropod is offline Macro to replace few words in the document Windows 7 64bit Macro to replace few words in the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Yes, it's possible. It's essentially a matter of adding the requisite code to a Document_Open sub. For example:
Code:
With ActiveDocument.Content.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "monitor"
  .Replacement.Text = "review"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-15-2012, 07:12 PM
ubns ubns is offline Macro to replace few words in the document Windows 7 32bit Macro to replace few words in the document Office 2010 32bit
Competent Performer
Macro to replace few words in the document
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

I have entered this code, normal.dotm - this document. It does not do anything - when I open the document.

I have tried the Find feature to find - "Review" and it is still in the document.

What do you think I am doing wrong?
Reply With Quote
  #4  
Old 08-15-2012, 07:42 PM
macropod's Avatar
macropod macropod is offline Macro to replace few words in the document Windows 7 64bit Macro to replace few words in the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Did you add the code to a Document_Open sub, as I said you should?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 08-15-2012, 08:20 PM
ubns ubns is offline Macro to replace few words in the document Windows 7 32bit Macro to replace few words in the document Office 2010 32bit
Competent Performer
Macro to replace few words in the document
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

Yes it worked..
Reply With Quote
  #6  
Old 08-15-2012, 11:46 PM
ubns ubns is offline Macro to replace few words in the document Windows 7 32bit Macro to replace few words in the document Office 2010 32bit
Competent Performer
Macro to replace few words in the document
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default how to change multiple items

Hi

I have added following code to the code you provided, it is not replacing "folder or on a seperate disc" with blank.

Can you please advice on this?

Code:
Sub Document_Open()
With ActiveDocument.Content.find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "monitor"
  .Replacement.Text = "review"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
With ActiveDocument.Content.find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Replacement.Text = "folder or on a seperate disc"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = False
  .MatchCase = False
  .MatchWholeWord = True
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
With ActiveDocument
  If InStr(.Sections.First.Footers(wdHeaderFooterFirstPage).Range.Text, "Statement of Advice") > 0 Then
   Application.OrganizerCopy Source:=.AttachedTemplate.FullName, Destination:=.FullName, _
   Name:="Custom Breakout", Object:=wdOrganizerObjectStyles
   Application.OrganizerCopy Source:=.AttachedTemplate.FullName, Destination:=.FullName, _
   Name:="Custom Breakout Subheading", Object:=wdOrganizerObjectStyles
   Application.OrganizerCopy Source:=.AttachedTemplate.FullName, Destination:=.FullName, _
   Name:="Custom Page Heading", Object:=wdOrganizerObjectStyles
   Application.OrganizerCopy Source:=.AttachedTemplate.FullName, Destination:=.FullName, _
   Name:="Custom Page Subheading 1.0", Object:=wdOrganizerObjectStyles
   Application.OrganizerCopy Source:=.AttachedTemplate.FullName, Destination:=.FullName, _
   Name:="Custom Paragraph Main Heading 1.0", Object:=wdOrganizerObjectStyles
 End If
End With
End Sub
Reply With Quote
  #7  
Old 08-16-2012, 10:08 PM
ubns ubns is offline Macro to replace few words in the document Windows 7 32bit Macro to replace few words in the document Office 2010 32bit
Competent Performer
Macro to replace few words in the document
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

any suggestions for the above code?
Reply With Quote
  #8  
Old 08-16-2012, 10:33 PM
macropod's Avatar
macropod macropod is offline Macro to replace few words in the document Windows 7 64bit Macro to replace few words in the document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Umesh,

The flaw is pretty obvious. Have a look at what you have for the Find text and the Replacement text ... compare that against the strings used for the Find text and the Replacement text in the first part of the sub.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to replace few words in the document Need VBA For Macro On How To Remove Specific Words netchie Word VBA 6 08-28-2012 03:37 PM
Macro to replace few words in the document Macro for highlighting specific number of words icsjohn Word VBA 2 12-07-2011 06:44 PM
Macro to replace few words in the document Find & Replace words with "/" prefix & suffix tollanarama Word 4 01-25-2011 02:19 AM
Lock words in a document, but allow for input within the document tlinde Word 1 02-09-2010 09:07 PM
How to stop "replace"'ing new words (in desperate need of advice) bme081 Word 6 11-02-2009 05:57 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft