Word Macro: find sub only returning the first instance
Hi everyone,
I am not the most VB savvy, but I am trying to learn this for making some macros for my job.
I have a huge word doc full of statements and am trying to search through the document, find specific criteria, copy the entire page to a new document, and move on to the next
found item.
It works but it stops after the first found item. I have tried it with if .Forward = true, is there are better way to do this?
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Page 1 of 2"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
If .Found = True Then
Selection.Cut
docB.Activate
Selection.Paste
docA.Activate
End If
End With
Does anything look amiss? Any help is greatly appreciated!
Re: Word Macro: find sub only returning the first instance
I wonder what the best way to run a subroutine of commands for each found string in the find / replace.
Re: Word Macro: find sub only returning the first instance
you probably need to look at using findnext, i am sure there would be some example in the help file
Re: Word Macro: find sub only returning the first instance
I have been reading the helps, but I cannot find any good examples.
I think that I should probably dim the find string as a variable and then use a
"For Each" statement if
.text="search string" then
.found = true
(run cut / paste)
Exit for (loop)
I still can't get it to loop paste the first find. Any suggestions or URL where there might be some examples?
Any help is greatly appreciated, I am pulling out my hair.
Re: Word Macro: find sub only returning the first instance
Anything look amiss. This only copies the current page not the found page.
Sub Macro1()
' create another doc to copy to / label docs
Dim docA As Document
Set docA = ActiveDocument
Documents.Add Template:="Normal", NewTemplate:=False, DocumentType:=0
ActiveDocument.SaveAs2 FileName:= _
"DocB.docx", FileFormat:= _
wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False, CompatibilityMode:=14
Dim docB As Document
Set docB = ActiveDocument
docA.Activate
Dim SearchTerm As String
SearchTerm = "Page 1 of"
' \page selects the entire page
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
Selection.Find.ClearFormatting
With Selection.Find
.Text = SearchTerm
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute
End With
If Selection.Find.Found Then
Selection.GoTo What:=wdGoToBookmark, Name:="\page"
Selection.Cut
docB.Activate
Selection.Paste
docA.Activate
End If
End Sub
Re: Word Macro: find sub only returning the first instance
as you do things that change the selection etc, you need to search the document continuously for the next occurrance, until no more occurances of the find string match, you can not use wdfindall, but must search until not found with no wrap, so that it will not start again at the beginning