Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-12-2014, 07:36 AM
Marko Marko is offline Help required with Selection.Find to search a text file. Windows 7 64bit Help required with Selection.Find to search a text file. Office 2003
Novice
Help required with Selection.Find to search a text file.
 
Join Date: Oct 2014
Posts: 2
Marko is on a distinguished road
Default Help required with Selection.Find to search a text file.

I am trying to write a macro to search through a large text file to find page numbers. These numbers are on their own lines with no other text. I want to check that all the line numbers are there and in the correct order. I want to start at the top of the first page and then search for the first number. Then search down from the first number to find the next, and so on. I have started with the following macro.



'Goto the top of page 1
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"

For n = 1 To 100
t = "^p" & n & "^p"

With Selection.Find
.ClearFormatting
.Text = t
.Forward = True
.Wrap = wdFindStop
End With

If Selection.Find.Execute = False Then Stop

Next n

This will only find the first line number. If I change the .Wrap line to wdFindContinue it will go back and search the whole document, not just from where the previous search stopped. So this will not show if the page numbers are out of sequence.

If anyone can point me in the right direction it would be greatly appreciated. I am using Word 2003 and Windows 7.
Reply With Quote
  #2  
Old 10-12-2014, 03:49 PM
macropod's Avatar
macropod macropod is offline Help required with Selection.Find to search a text file. Windows 7 64bit Help required with Selection.Find to search a text file. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
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

You could use a macro like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long
i = 1: j = i
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "^13[0-9]@^13"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    .Start = .Start + 1
    .End = .End - 1
    If .Text <> j Then .Text = j
    j = j + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox j - i & " page #s validated."
End Sub
As coded, the macro ensures all the numbers are in sequence, starting at 1. To start at a different number, change the 'i = 1' value.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 10-18-2014, 06:32 AM
Marko Marko is offline Help required with Selection.Find to search a text file. Windows 7 64bit Help required with Selection.Find to search a text file. Office 2003
Novice
Help required with Selection.Find to search a text file.
 
Join Date: Oct 2014
Posts: 2
Marko is on a distinguished road
Default

Thank you. After a bit of research I can now see what this is doing and how it works. I appreciate the help.

Cheers.
Marko
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to Find and Format Text string defined using Inputbox within selection sistemalan Word VBA 7 10-03-2014 07:28 AM
Help required with Selection.Find to search a text file. Find what box in Find and replace limits the length of a search term Hoxton118 Word VBA 7 06-10-2014 05:05 AM
Help required with Selection.Find to search a text file. Selection of all Text for a specific page in word is spanning selection across pages ramsgarla Word VBA 9 12-05-2012 03:23 AM
Help! Find text in doc file & copy to another file northstarman Word 1 11-05-2010 08:15 AM
Automatic find replace after selection in dropdown vsempoux Word 0 10-28-2009 08:45 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:46 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