Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-14-2014, 11:27 PM
tariqaleed2 tariqaleed2 is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Novice
Search and Replace vba Word 2003
 
Join Date: Jul 2014
Posts: 14
tariqaleed2 is on a distinguished road
Default Search and Replace vba Word 2003

Search and Replace vba Word 2003Is there a code to search and replace in specified page ?


I want code to replace "in" by "on" only in page 3
Reply With Quote
  #2  
Old 08-15-2014, 01:12 AM
macropod's Avatar
macropod macropod is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
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

Why would you bother with VBA for this when a simple Find/Replace, after selecting page 3, requires no code?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 08-15-2014, 02:44 AM
tariqaleed2 tariqaleed2 is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Novice
Search and Replace vba Word 2003
 
Join Date: Jul 2014
Posts: 14
tariqaleed2 is on a distinguished road
Default

Thank you macropod for responding
i use the code via vba ms access It is part of function , is important to set the page number, which will deal with it, but I must be know on vba Word .
the example below is it work good in vba word 2007 but in word 2003 Does not work as it should
https://www.msofficeforums.com/word-...-document.html
Reply With Quote
  #4  
Old 08-15-2014, 03:27 AM
macropod's Avatar
macropod macropod is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
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

The code in the link does work as it should - just not as you want it to. For your purposes, you could use something based on:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range
Set Rng = ActiveDocument.GoTo(What:=wdGoToPage, Name:=3)
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
With Rng
  With .Duplicate
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "in"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    Do While .Find.Found = True
      If .InRange(Rng) = True Then
        .Text = "on"
        .Collapse wdCollapseEnd
        .Find.Execute
      Else: Exit Do
      End If
    Loop
  End With
End With
Application.ScreenUpdating = False
End Sub
or:
Code:
Sub Demo()
Application.ScreenUpdating = True
Dim Rng As Range
Set Rng = ActiveDocument.GoTo(What:=wdGoToPage, Name:=3)
Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
With Rng.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = "in"
  .Replacement.Text = "on"
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 08-15-2014 at 03:29 AM. Reason: Alernate code
Reply With Quote
  #5  
Old 08-17-2014, 03:01 AM
tariqaleed2 tariqaleed2 is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Novice
Search and Replace vba Word 2003
 
Join Date: Jul 2014
Posts: 14
tariqaleed2 is on a distinguished road
Default

Thank you very much macropod
the Code works on Word 2003 and 2007 excellent
i tested via ms access with 10 pages, is it work good .
currently i tested with 600 pages is it slow
Regards

Last edited by macropod; 08-17-2014 at 03:08 AM. Reason: Deleted unnecessary post of unformatted code
Reply With Quote
  #6  
Old 08-17-2014, 03:10 AM
macropod's Avatar
macropod macropod is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
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

I doubt the slowness has anything much to do with the code, since it's only processing a single page. Rather, it probably reflects the time Word takes to open and, after the Find/Replace, reformat a 600-page document.

PS: There's no need to post the code you've been given in the previous but, when you do post code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 08-17-2014, 05:47 AM
gmayor's Avatar
gmayor gmayor is offline Search and Replace vba Word 2003 Windows 7 64bit Search and Replace vba Word 2003 Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I think I might be inclined (not knowing what else is on the page) to change 'False' to 'True' ?

Code:
.MatchCase = False 
.MatchWholeWord = False
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 08-17-2014, 02:36 PM
macropod's Avatar
macropod macropod is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
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, that might be worthwhile, to avoid false matches, but I doubt it'll make a noticeable difference to the execution speed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 08-19-2014, 03:09 AM
tariqaleed2 tariqaleed2 is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Novice
Search and Replace vba Word 2003
 
Join Date: Jul 2014
Posts: 14
tariqaleed2 is on a distinguished road
Default

Thank you macropod for this interest
i tested, but the way that I want successful with fifty pages and then begin the problems
I really appreciate all this
i change my way to mail merge
Thank you very much
Reply With Quote
  #10  
Old 08-19-2014, 03:34 AM
macropod's Avatar
macropod macropod is offline Search and Replace vba Word 2003 Windows 7 32bit Search and Replace vba Word 2003 Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,371
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

I have no idea what you mean regarding the 600 pages in post 5 or the 50 pages in post 9. You asked for, and were provided with, code to process page 3 only, regardless of the document length. If as now seems to be the case you are trying to change a mailmerge after the event, that is certainly the wrong way to go about things. Changing the mailmerge main document before doing the mailmerge is certainly the way to go. If need be, a few simple field codes can be used to vary the text.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and Replace vba Word 2003 search and replace dirkoo Word VBA 2 08-14-2013 11:25 AM
Search and Replace vba Word 2003 Odd search and replace query Rabski Word 1 11-13-2012 02:25 PM
Search and Replace vba Word 2003 WORD: Rtf and search-replace (regexp/fonts) seteshpl Word 1 09-06-2011 01:35 AM
Search Replace Copy dblack7211 Word 0 05-05-2010 01:19 PM
Search and Replace - Clear Search box JostClan Word 1 05-04-2010 08:46 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:24 PM.


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