|
-
Aug 27th, 2012, 04:04 PM
#1
Thread Starter
New Member
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!
-
Aug 29th, 2012, 01:35 PM
#2
Thread Starter
New Member
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.
-
Aug 29th, 2012, 04:33 PM
#3
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 31st, 2012, 11:59 AM
#4
Thread Starter
New Member
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.
-
Aug 31st, 2012, 12:53 PM
#5
Thread Starter
New Member
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
-
Sep 1st, 2012, 05:52 AM
#6
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|