![]() |
|
#1
|
|||
|
|||
![]()
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 |
#2
|
||||
|
||||
![]()
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] |
#3
|
|||
|
|||
![]()
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 |
#4
|
||||
|
||||
![]()
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 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 |
#5
|
|||
|
|||
![]()
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 |
#6
|
||||
|
||||
![]()
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] |
#7
|
||||
|
||||
![]()
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 |
#8
|
||||
|
||||
![]()
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] |
#9
|
|||
|
|||
![]()
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 |
#10
|
||||
|
||||
![]()
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] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
dirkoo | Word VBA | 2 | 08-14-2013 11:25 AM |
![]() |
Rabski | Word | 1 | 11-13-2012 02:25 PM |
![]() |
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 |