Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-05-2012, 04:59 AM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 11-05-2012, 02:08 PM
macropod's Avatar
macropod macropod is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
For installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-05-2012, 11:33 PM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 11-06-2012, 01:49 AM
macropod's Avatar
macropod macropod is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #5  
Old 11-06-2012, 05:18 AM
gmaxey gmaxey is offline Iterative / sequential find and replace Windows XP Iterative / sequential find and replace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by macropod; 11-06-2012 at 01:04 PM. Reason: Added code tags & formatting
Reply With Quote
  #6  
Old 11-06-2012, 06:36 AM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default

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".
Reply With Quote
  #7  
Old 11-06-2012, 06:45 AM
gmaxey gmaxey is offline Iterative / sequential find and replace Windows XP Iterative / sequential find and replace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 11-06-2012, 07:06 AM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default

That one didn't seem to do anything at all!
Reply With Quote
  #9  
Old 11-06-2012, 07:37 AM
gmaxey gmaxey is offline Iterative / sequential find and replace Windows XP Iterative / sequential find and replace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 11-06-2012, 09:29 AM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 11-06-2012, 10:06 AM
gmaxey gmaxey is offline Iterative / sequential find and replace Windows XP Iterative / sequential find and replace Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,422
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 12-05-2012, 11:41 PM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default

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
Reply With Quote
  #13  
Old 12-06-2012, 12:09 AM
macropod's Avatar
macropod macropod is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #14  
Old 12-10-2012, 03:33 AM
paulkaye paulkaye is offline Iterative / sequential find and replace Windows 7 64bit Iterative / sequential find and replace Office 2007
Advanced Beginner
Iterative / sequential find and replace
 
Join Date: Nov 2011
Posts: 84
paulkaye is on a distinguished road
Default

Great - thank you.
Reply With Quote
Reply

Tags
find and replace

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterative / sequential find and replace Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
Iterative / sequential find and replace Is there a way to use "find/replace" to find italics words? slayda Word 3 09-14-2011 02:16 PM
Iterative / sequential find and replace Find and Replace kjxavier Word 6 08-19-2011 09:56 PM
Iterative / sequential find and replace Find and Replace kjxavier Word 1 08-12-2011 08:57 AM
Iterative / sequential find and replace Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:27 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft