#1
|
|||
|
|||
Iterative / sequential find and replace
Hi,
I frequently have lists, each entry of the list is a one-sentence paragraph ending in a full-stop (period), and is prefixed by a non-sequential number or letter or combination, followed by a full-stop and then a tab. So, it might look like this (tabs are shown as arrows, for clarity): 1.→People like to help in online forums. 1a.→Hopefully they will be able to help this time. x.→I'll be a little stuck without some help with this. The reason that the prefix is non-sequential and unpredictable is that the order of the list is shuffled about a lot during editing, and for reasons I can't explain here, I cannot use MS Word's autonumbering feature. Once the list has been finalized, I have to renumber the entries sequentially, like this: 1.→People like to help in online forums. 2.→Hopefully they will be able to help this time. 3.→I'll be a little stuck without some help with this. Can anyone suggest how to do this? The lists are consistent enough that I think it should be possible to find "^p*.^t", but (assuming this is correct), I don't know how to get the replace bit to work the way I want. Any help would be most appreciated! Thanks, Paul |
#2
|
||||
|
||||
Hi Paul,
Try the following macro: Code:
Sub ReNumber() Application.ScreenUpdating = False Dim i As Long With ActiveDocument For i = 1 To .Paragraphs.Count .Paragraphs(i).Range.Words.First = i Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
Thanks for the reply. Unfortunately I got the error: "Run-time error '5941': The requested member of the collection does not exist." You probably realise that I don't know what I'm doing, but I clicked "Debug" and the following line was highlighted: .Paragraphs(i).Range.Words.First = i Paul *EDIT* I've now looked at the text that was left, and have seen that a key piece of information was missing from my description. Some list items have carriage-returns within them. I realise that this is important because the macro you provided replaced (as I assume it was intended to) the first word of each new line with a sequential number - including those lines within list items. I think that the find term "^p*.^t" (or similar) is probably key here - I think that it's the only reliable way to identify the start of each list item. |
#4
|
||||
|
||||
The problem doesn't have anything to do with intra-paragraph returns, but with the fact you have empty paragraphs. To work around that, change:
.Paragraphs(i).Range.Words.First = i to: If .Paragraphs(i).Range.Words.Count > 2 Then .Paragraphs(i).Range.Words.First = i
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Continuing Paul's approach, I would suggest adding a sequence field rather than a fixed number. This way, if needed, you still rearrange the list.
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oPar As Word.Paragraph Dim oRng As Word.Range For Each oPar In ActiveDocument.Range.Paragraphs If Not oPar.Range.Words(1) Like vbCr Then Set oRng = oPar.Range.Words(1) oRng.Select ActiveDocument.Fields.Add oRng, wdFieldSequence, "Numbered", False End If Next oPar End Sub Last edited by macropod; 11-06-2012 at 01:04 PM. Reason: Added code tags & formatting |
#6
|
|||
|
|||
Hi Paul & Greg,
Thank you both for your responses so far. Unfortunately, although I've tried Paul's edited macro and Greg's alternative macro, they both result in the same problem as before - all paragraphs are renumbered, including those within a list item, whereas I only want to renumber the paragraphs that start [something].[tab], replacing the [something] with a sequential number. Thanks again in advance, Paul ***EDIT*** I note that because there is NEVER a full-stop (period) in the middle of a list item, perhaps I could state the problem as "I want to replace the 'word' that preceeds every second full-stop with a sequential number". |
#7
|
|||
|
|||
This should work:
HTML Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey Dim oPar As Word.Paragraph Dim oRng As Word.Range For Each oPar In ActiveDocument.Range.Paragraphs If Not oPar.Range.Words(1) Like vbCr Then If oPar.Range.Words(2) = vbTab Then Set oRng = oPar.Range.Words(1) ActiveDocument.Fields.Add oRng, wdFieldSequence, "Numbered", False End If End If Next oPar End Sub |
#8
|
|||
|
|||
That one didn't seem to do anything at all!
|
#9
|
|||
|
|||
Sorry, I forgot about your "." before the tab. Try one of these. At this, I'm not sure either is very efficient, but both seem to work with your examples and with paragraphs within the enumerated items.
HTML Code:
Sub ScratchMacroII() 'A basic Word macro coded by Greg Maxey Dim oPar As Word.Paragraph Dim oRng As Word.Range For Each oPar In ActiveDocument.Range.Paragraphs Set oRng = oPar.Range oRng.Collapse wdCollapseStart oRng.MoveEndUntil Cset:=vbTab, Count:=wdForward If oRng.InRange(oPar.Range) And oRng.Start <> oRng.End And oRng.Characters.Last = "." Then Set oRng = oPar.Range.Words(1) ActiveDocument.Fields.Add oRng, wdFieldSequence, "Numbered", False End If Next oPar End Sub Sub ScratchMacroI() 'A basic Word macro coded by Greg Maxey Dim oPar As Word.Paragraph Dim oRng As Word.Range For Each oPar In ActiveDocument.Range.Paragraphs If Not oPar.Range.Words(1) Like vbCr Then If oPar.Range.Words.Count > 2 Then If oPar.Range.Words(2) = "." And oPar.Range.Words(3) = vbTab Then Set oRng = oPar.Range.Words(1) ActiveDocument.Fields.Add oRng, wdFieldSequence, "Numbered", False End If End If End If Next oPar End Sub |
#10
|
|||
|
|||
Greg - they both work perfectly!
Thank you. I don't know how much time you spent on my problem, but you have just saved me at least 30 mins per week for the next two years - I really appreciate it. Paul |
#11
|
|||
|
|||
Paul,
Less than 30 minutes. The lesson (if you care to learn one) is to always fully define the scope of the task up front. Moving the goal posts after the start of the game can become tiresome and frustrating. I'm pleased that I could help. Good luck. |
#12
|
|||
|
|||
Hi again,
I've noticed that at least one of these macros results in field codes in my document, rather that simple text. If I press alt+F9 I see {SEQ Numbered} where each of the new numbers is located. This is a little problematic for reasons I won't go into here. Is there any way to avoid this? Thanks, Paul |
#13
|
||||
|
||||
All of the macros Greg provided are based on the use of SEQ fields. You can either revert to the code I provided (as amended) or unlink the fields that Greg's macro creates. Either action should only be done once all you're other editing has finished. If you take the unlink approach and there are no other fields in the body of the document, you can unlink them with a macro as simple as:
Code:
Sub LockNums() ActiveDocument.Fields.Unlink End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#14
|
|||
|
|||
Great - thank you.
|
Tags |
find and replace |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |
Is there a way to use "find/replace" to find italics words? | slayda | Word | 3 | 09-14-2011 02:16 PM |
Find and Replace | kjxavier | Word | 6 | 08-19-2011 09:56 PM |
Find and Replace | kjxavier | Word | 1 | 08-12-2011 08:57 AM |
Help with find and replace or query and replace | shabbaranks | Excel | 4 | 03-19-2011 08:38 AM |