Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 01-02-2017, 06:00 PM
bobbyrjw bobbyrjw is offline Windows 7 32bit Office 2003
Novice
 
Join Date: Jan 2017
Posts: 2
bobbyrjw is on a distinguished road
Default Need VBA to renumber chapters

I have a 20k word story that has been edit-chopped.
I want VBA to find each instance of the word "Chapter" and add a number that increments on each iteration, or "find". and I want to change the style to heading 1.
here is the mess I have that is not working.
I didn't know if I can dispense with the replacement text, so I entered the same as the find text.

I'm not sure it is looping all the way through the doc.
I can't find the common practice to delineate code so I will do this;

[begin VBA code]

Sub Makro6()
'
' Makro6 Makro
'
'
Dim chanumb As Integer
chanumb = 1

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find


.Text = "chapter"
.Replacement.Text = "Chapter " & chanumb
.Style = ActiveDocument.Styles("Heading 1")
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchAllWordForms = False

End With
chanumb = chanumb + 1
Selection.Find.Execute Replace:=wdReplaceAll

End Sub

[/end VBA code]

Thank you!
Reply With Quote
  #2  
Old 01-02-2017, 09:50 PM
gmayor's Avatar
gmayor gmayor is online now Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 1,856
gmayor will become famous soon enoughgmayor will become famous soon enough
Default

Use a SEQ field for the chapter numbers then if you change the orrder again you can update the field to change the numbers e.g.

Code:
Sub Makro7()
Dim oRng As Range
Dim oFound As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:="Chapter")
            Set oFound = oRng
            oRng.Collapse 0
            oRng.Fields.Add oRng, wdFieldSequence, "ChapterNum", False
            oRng.InsertBefore Chr(32)
            oFound.End = oRng.End
            oFound.Style = "Heading 1"
            oRng.Collapse 0
        Loop
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-02-2017, 10:10 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 15,228
macropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to allmacropod is a name known to all
Default

Quote:
Originally Posted by bobbyrjw View Post
I have a 20k word story that has been edit-chopped.
I want VBA to find each instance of the word "Chapter" and add a number that increments on each iteration, or "find". and I want to change the style to heading 1.
You might not want to do that if there's a cross-reference somewhere pointing to a different chapter. Besides which, there's a better way.

Assuming you want to use Heading 1 only for Chapter titles, you can assign a multi-level list number to that Style and prefix the list-level number with the word 'Chapter'. Then simply find the chapter headings and apply the Heading 1 Style to them - the numbering will take care of itself (even if you do further edits).
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #4  
Old 01-03-2017, 12:15 AM
bobbyrjw bobbyrjw is offline Windows 7 32bit Office 2003
Novice
 
Join Date: Jan 2017
Posts: 2
bobbyrjw is on a distinguished road
Default

Works magnificently. Thank you!
Reply With Quote
Reply

Tags
chapter titles, vba in microsoft word
Please reply to this thread with any new information or opinions.

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want a Word 2013 table to automatically renumber the list when I add a new row into the middle. ar8294 Word Tables 7 07-03-2016 01:58 PM
Renumber and Multi-Level List functions don't work? ar8294 Word Tables 2 07-03-2016 10:49 AM
How to number chapters and sub-chapters in a few clicks? Icelandic_Boy Word 1 05-24-2016 11:33 AM
Chapters only Table of Contents SQLUSA Word 2 07-04-2012 03:36 PM
Page numbering - Chapters Andrew Thorpe Word 0 08-04-2010 11:04 AM


All times are GMT -7. The time now is 04:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft