![]() |
|
#1
|
|||
|
|||
![]()
My data is 1-3 lines unique code that is first element in data segment. May Not be new paragraph. First field is 1-5 digits comma and then 1 digit and comma
Original File Line 1 123,2,text1, text2,.... Line 2 ....text 10 Line 3 145,3,text1....text 5 Line 4 156, 2, text 1...text5 Line 5 ......................text10 Line 6 ......................text15 ........... Line 7000 4160,3,text1......text4 Desired Output - Search for ([0-9],[0-9]) and insert (001) before search term and (002) before next search term Output File Line 1 (001) 123,2,text1, text2,.... Line 2 ....text 10 Line 3 (002) 145,3,text1....text 5 Line 4 (003) 156, 2, text 1...text5 Line 5 ......................text10 Line 6 ......................text15 Line 7 (004) 160,3,text1......text4 Maybe 1000 pages I used https://www.msofficeforums.com/word-...ight-text.html as template and have just seach working. Code:
Sub Wildcard_Search_Insert_Nums() Application.ScreenUpdating = False Options.DefaultHighlightColorIndex = wdYellow With ActiveDocument.Content.Find .ClearFormatting .Text = "([0-9],[0-9])" With .Replacement .Text = "TEST^&XXX" .ClearFormatting .Highlight = True End With .Forward = True .Wrap = wdFindContinue .Format = True .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub with n=n+1 inside loop? Really frustrated. Would appreciate any help. Obviously can use C program to do this but want functionality for all users, not 3 machines out of hundreds that have compilers. |
#2
|
||||
|
||||
![]()
With your example, which is not consistently formatted, e.g. Line 4 format has an extra space compared with Line 3, the following should do the job
Code:
Dim oRng As Range Dim i As Long Set oRng = ActiveDocument.Range i = 1 With oRng.Find Do While .Execute(FindText:="[0-9]{1,5},[ 0-9]{1,2},", MatchWildcards:=True) oRng.Text = "(" & Format(i, "000") & ") " & oRng.Text oRng.Collapse 0 i = i + 1 Loop End With
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
Thanks so much! Can you explain the oRng.Collapse 0 command? The concept of the .Collapse command is not clear to me, I understand the collapsestart and collapseend (don't like the collapse name), but why anything else?
What is the difference between the two search methods above? The first is concurrent find and replace and the second is one find at a time? Is that why first method is the preferred method? I'll have to get Mastering VBA for Office book to learn the concepts unless you have a better suggestion? |
#4
|
||||
|
||||
![]()
oRng.Collapse 0 is the numeric equivalent of oRng.Collapse Direction:=wdCollapseEnd
(1 would be the equivalent of wdCollapseStart). Collapse is used here to allow the search to restart from the end of the current found text range.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
![]() ![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Issue with wildcard search | mysterytramp | Word | 0 | 05-13-2015 10:40 AM |
search and replace a field containing text and numbers | H28Sailor | Word | 6 | 09-11-2014 01:00 AM |
Issue with Wildcard Search and Replace | linusthedog | Word | 3 | 09-04-2014 03:32 PM |
Where is the error in my wildcard search? | Ulodesk | Word | 10 | 06-30-2014 01:46 PM |
Wildcard search help. | Kempston | Word | 0 | 11-13-2009 03:58 AM |