View Full Version : How to find the specific word on many pages and paste it at last page
q1q2q3
03-15-2007, 02:26 AM
I have many product item nos with many pages on a document (word file). However, I don't know how to do it. Please help me for everyone.
My objective is to find it all product item nos at the last page.
I try to write the macro on the word, however the find function can not achieve my plan. It can find the specific word and paste it at last page until finding out all the specific word.
The macro code as follows:
Selection.Find.ClearFormatting
With Selection.Find
.Text = "ZZZ"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdStory
Selection.Paste
Selection.TypeParagraph
fumei
03-15-2007, 08:26 AM
I am not understanding what you are asking for. If you are looking for the SAME word on multiple pages, what do you want on the last page?
You are looking for "ZZZ". Let's say the document has:
Page 3 - "ZZZ"
Page 7 - "ZZZ"
Page 16 - "ZZZ"
So "ZZZ" is on Page 3, 7, and 16.
WHAT do you want on the last page?
q1q2q3
03-15-2007, 10:59 AM
my source file is a text file which is in a chaos. The data is scattered into the different lines and paragraph. On my project, I must reuse the source data, but I don't want to search it by hitting enter key many times.
And then I import it into the word file. After importing it, the spacing and paragraph are very awful. I use the find command to search it all. Unfortuneately, the file is so big, I hit "enter" key many many times. Finally, I want to finish it with macro. It is because I don't know how to write it with VBA. On this forum, I ask expert like you for help.
The "ZZZ" prefix is the common digit which are required for the specific product item no. So my objective is to search all like "ZZZ" item and store it at last page. It is helpful for me to calculate the different items. Perhaps my objective is so strange, it is actual the requirement for my project.
Besides, I don't understand the find key how to write in word file. Once you use it, you only find the first record on it. You don't know how to find the second, third & fourth..... records with VBA
Is my English very poor ? I try my best to express my meaning.
Thank you for your help
mdmackillop
03-15-2007, 11:46 AM
Hi
Welcome to VBAX
Can you post a sample of your text file? Remove any sensitive data and post it using Manage Attachments in the Go Advanced section. You may have to Zip it first
Regards
MD
q1q2q3
03-15-2007, 09:26 PM
my actual file cannot upload to you. I find some situation like text file which contain the html tag and html link. My problem is like this :
I want to find out all the "ZZZ" items and paste into the last page.
Previously, importing the text file into word, it has more html tag which is useless. How do I get rip of it like all command line ? On my plan, all command line has gone and left the hyperlink only in text file.
fumei
03-15-2007, 09:30 PM
Sorry, but I still do not know what you mean by:
So my objective is to search all like "ZZZ" item and store it at last page.
Store what? The number of times you found ZZZ? Take a chunk of text around ZZZ, and move to the end? What do you mean by store? Move it? Count?
I don't know if this will help with what you want to do - as I don't know what you want to do. But maybe it will help a little.Sub FindZZZ()
Dim counter As Long
With ActiveDocument.Content.Find
Do While (.Execute(FindText:="ZZZ", Forward:=True) _
= True) = True
counter = counter + 1
Loop
End With
Selection.EndKey Unit:=wdStory
Selection.TypeText Text:=counter & " instances of ZZZ were found."
End SubThis goes through a document looking for "ZZZ", counting each one, then goes to the end of the document and types in the number of times ZZZ was found.
I know this will not really help with what you are doing, but as I am remain unsure what that is, this is the best I can offer.
fumei
03-15-2007, 09:59 PM
You will have to define what you mean by ZZZitems.
I want to find out all the "ZZZ" items and paste into the last page.
What is a ZZZ item?
Is it multiple paragraphs?
ZZZ - text text text text.
New paragraph with more text more text more text
Is it one paragraph?
ZZZ123456
In other words, you can search for ZZZ easily, but you want to expand something, you want to include something that is not in your search criteria. That has to be explicitly defined.
fumei
03-15-2007, 10:07 PM
Here is something that may get you ahead.Sub FindZZZ()
Dim lngEnd As Long
lngEnd = ActiveDocument.Range.End
Selection.HomeKey Unit:=wdStory
With Selection.Find
Do While (.Execute(FindText:="ZZZ", Forward:=True) _
= True) = True
If Selection.Range.Start > lngEnd Then GoTo Finish
ActiveDocument.Bookmarks.Add _
Name:="Temp", Range:=Selection.Range
With Selection
.Expand Unit:=wdSentence
.Copy
.EndKey Unit:=wdStory
.Paste
.TypeParagraph
.GoTo what:=wdGoToBookmark, Name:="temp"
.Collapse Direction:=wdCollapseEnd
.Move Unit:=wdCharacter
End With
ActiveDocument.Bookmarks("Temp").Delete
Loop
End With
Finish:
End SubWhat this does.
1. Picks up the current end of the document Long value. This is used because the code ADDS text at the end of the document. This text contains the search text. If there was no set place to stop the code would continue forever.
2. While search .Execute is True (ie. ZZZ is found), check to see if the selection is greater than the original end of the document. If it is, quit. If it is not;
3. make a temporary bookmark ("Temp")
4. expand the Selection to include the current sentence. This could be current paragraph if that is more applicable.
5. copy the Selection
6. go to the end of the document and Paste
7. go back to the temp bookmark
8. collapse the range, and move forward one character. This puts the Selection back to where it was, ready for the next search
9. delete the temp bookmark
Result? This code makes a copy of every sentence with ZZZ in it, and pastes that copy (in order) at the end of the document.
q1q2q3
03-16-2007, 02:48 PM
Thank you for your expertise advise.
However, I want to ask more question on this program.
It is embedded into the macro which is successful running. If I want to paste the "ZZZ" items which is not so long ----Only 16 characters
( "ZZZ" + 13 DIGITS). What can I use some specific command on it ?
And then I copy and paste this procedure into Excel macro which is running or not. If not, How can I change the command and statement on it which can be running on Excel's macro ?
Thank you for your help again
fumei
03-16-2007, 09:57 PM
Hmmm. I am not going to hold your hand on this. YOU have to do something.
Think. The clues to picking up whatever you want is in the code. You want to take each found ZZZ and add the next 13 characters? This can be very easily done. Look at the code I posted and maybe you can figure it out. I hope you have IntelliSense turned on in the VBE. I am not going to just post it for you. Sorry.
Hint: type Selection, then "dot" ( Selection.) Take a look at all the possible methods, and think...."I want to have 13 more characters....I want to keep the start of the Selection, but move the end 13 characters to the right..."
There, if you can not figure it out from that.....hmmmm.
If you want to do it, try to do it, and if you have specific problems...ask.
As for running in/from/to Excel code. Probably. As I can't see what that code is....I don't know. In principle, sure, you can do any of this from Excel, and bring the information back into Excel.
q1q2q3
03-16-2007, 11:31 PM
I want some book about VBA for my reference. Perhaps I can look at the help index, it is not very clear on its explanation.
Based on your solution and recommendation, it is not provided on its help as follows :
Do While (.Execute(FindText:="ZZZ", Forward:=True) _
= True) = True
It is so strange for me to think and search this statement, because I have no concept on this usage and statement.
Being an expert, you ignore some steps and statements for novice's hint. And then novice treats the hints for their treasury.......
fumei
03-17-2007, 05:52 AM
.Execute(FindText:="ZZZ", Forward:=True) - means to execute the Find, looking for ZZZ, and looking forward from the current location.
.Execute(FindText:="ZZZ", Forward:=True) = True - means that yes, TRUE...ZZZ has been found
So,
Do While (.Execute(FindText:="ZZZ", Forward:=True) = True) (yes it has been found) = True
means do the following stuff for as long as the search DOES find the search string. Or rather, that it is true that the search execution returned a true (the string has been found).
I must point out that this statement has nothing to do with trying to get those next 13 characters...
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.