Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 09-11-2024, 02:11 AM
vivka vivka is offline VBA Run Time Error 91 Windows 7 64bit VBA Run Time Error 91 Office 2016
Expert
 
Join Date: Jul 2023
Posts: 302
vivka is on a distinguished road
Default

Hi, Shelley Lou! 1) Your code works without a hitch (no errors returned) on my machine,
2) There were some duplicate / redundant code lines in your code, so after fine-tuning the code (I hope, I really did it) it may look as follows:
Code:
Sub FormatManualNumbering()
Dim rng As range
Dim rngEnd As Long

Application.ScreenUpdating = False

    If selection.Type = wdSelectionIP Then
       MsgBox Prompt:="You have not selected any text!"
       Exit Sub
    End If
    Set rng = selection.range
    rngEnd = rng.End

    selection.range.InsertBefore vbCr
    With rng.Find
       .ClearFormatting
       .Replacement.ClearFormatting
       .Forward = True
       .Wrap = wdFindStop
       .Format = False
       .MatchWildcards = False
'Remove spaces starting paras:
       .text = "^p^w"
       .Replacement.text = "^p"
       .Execute Replace:=wdReplaceAll
'Remove spaces before para signs:
       .text = "^w^p"
       .Replacement.text = "^p"
       .Execute Replace:=wdReplaceAll
'Remove empty paras:
       .MatchWildcards = True
       .text = "^13{2,}"
       .Replacement.text = "^p"
       .Execute Replace:=wdReplaceAll
'Insert a tab before any 1st letter in a para:
       .text = "(^13[!^13]@)([A-Za-z])"
       .Replacement.text = "\1^t\2"
       .Execute Replace:=wdReplaceAll
'Replace two tabs with one tab:
       .text = "^t^t"
       .Replacement.text = "^t"
       .Execute Replace:=wdReplaceAll
'Delete tabs between letters, which have been inserted using
'the code to insert a tab before any 1st letter in a para previously:
       .text = "([A-Za-z])^t([A-Za-z])"
       .Replacement.text = "\1\2"
       .Execute Replace:=wdReplaceAll
    End With
    Set rng = selection.range
    Do
        With rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchWildcards = True
'Find a str between a tab & the nearest previous para sign, i.e.
'a str between a para sign & a tab, excluding other paras in-between:
            .text = "^13[!^13]@^t"
            If .Execute And rng.End <= rngEnd Then
                .text = "[,:; ]"
                .Replacement.text = "."
                .Execute Replace:=wdReplaceAll
            Else: Exit Do
            End If
'Delete all periods immediately before a tab:
            While rng.Characters.Last.Previous = "."
                rng.Characters.Last.Previous.Delete
            Wend
            rng.Collapse wdCollapseEnd
        End With
    Loop
'Reset rng (see the comment above):
    Set rng = selection.range
    With rng.Find
           .ClearFormatting
           .Replacement.ClearFormatting
           .Forward = True
           .Wrap = wdFindStop
           .Format = True
           .MatchWildcards = True
'Insert periods after lone 1st-level numberings:
           .text = "(^13[0-9]{1,})^t"
           .Replacement.text = "\1.^t"
           .Execute Replace:=wdReplaceAll
'Delete extra periods in the doc:
           .text = "[.]{2,}"
           .Replacement.text = "."
           .Execute Replace:=wdReplaceAll
'Delete tab after opening bracket:
           .text = "(^13[(])^t"
           .Replacement.text = "\1"
           .Execute Replace:=wdReplaceAll
'Delete tab after opening double quote (Chr(34) & Chr(147)) after para marks:
           .text = "(^13" & "[" & Chr(34) & Chr(147) & "]" & ")^t"
           .Replacement.text = "\1"
           .Execute Replace:=wdReplaceAll
    End With
'Delete the doc's starting para sign inserted previously:
    selection.range.Characters.First.Delete
 Application.ScreenUpdating = True
MsgBox "Complete"
  
Set rng = Nothing
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Why am I getting this run time error? MaxPower Excel 2 12-31-2023 01:31 AM
Word template with Macro keeps getting an error ''Run-time error 5941'' Marcel Word VBA 3 12-17-2019 04:55 PM
Word Error Message Run time Error 4605 baes10 Word VBA 1 08-30-2018 02:37 PM
Get Run-time Error 11 Jamtart PowerPoint 2 08-31-2012 05:04 AM
Word Visual Basic error - run time error 504 crazymorton Word 11 01-13-2012 04:32 AM

Other Forums: Access Forums

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


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