Modify a Word Macro to inspect section breaks & page breaks

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Modify a Word Macro to inspect section breaks & page breaks

Post by Sam1085 »

Hi,

I'm trying to create a macro to find section breaks and page breaks and then add comments them to separately.

Code: Select all

Sub Test()
Application.ScreenUpdating = False
'Clear Comments
Application.UserName = "Section Breaks Or Page Breaks"
Application.UserInitials = "SBPB"
For Each oCom In ActiveDocument.Comments
    If oCom.Initial = "SBPB" Then
    oCom.Delete
End If
Next oCom
Const message1 As String = "Page Break!"
     With Selection.Find
         .ClearFormatting
            .Text = "^m"
            .MatchWildcards = False
         Do While .Execute
            ActiveDocument.Comments.Add Range:=Selection.Range, Text:=message1
         Loop
     End With
     
Const message2 As String = "Section Break!"
     With Selection.Find
         .ClearFormatting
            .Text = "^b"
            .MatchWildcards = False
         Do While .Execute
            ActiveDocument.Comments.Add Range:=Selection.Range, Text:=message2
         Loop
     End With
Application.ScreenUpdating = True
End Sub
But in above code not define section breaks. It's only inspect page breaks. So, how do I do that for both section breaks and page breaks?

Thanks!
-Sampath-

User avatar
HansV
Administrator
Posts: 78863
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Modify a Word Macro to inspect section breaks & page bre

Post by HansV »

That's a tricky one. It works for me if I loop backwards.

Code: Select all

Sub Test()
    Dim oCom As Comment
    Dim sUser As String
    Dim sInitials As String
    Application.ScreenUpdating = False
    'Clear Comments
    sUser = Application.UserName
    Application.UserName = "Section Breaks Or Page Breaks"
    sInitials = Application.UserInitials
    Application.UserInitials = "SBPB"
    For Each oCom In ActiveDocument.Comments
        If oCom.Initial = "SBPB" Then
        oCom.Delete
    End If
    Next oCom
    Const message1 As String = "Page Break!"
    Selection.EndKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = "^m"
        .MatchWildcards = False
        .Wrap = wdFindStop
        .Forward = False
        Do While .Execute
            ActiveDocument.Comments.Add Range:=Selection.Range, Text:=message1
            Selection.Collapse Direction:=wdCollapseStart
            DoEvents
        Loop
    End With
    Const message2 As String = "Section Break!"
    Selection.EndKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = "^b"
        .MatchWildcards = False
        .Wrap = wdFindStop
        .Forward = False
        Do While .Execute
            ActiveDocument.Comments.Add Range:=Selection.Range, Text:=message2
            Selection.Collapse Direction:=wdCollapseStart
            DoEvents
        Loop
     End With
     Application.UserName = sUser
     Application.UserInitials = sInitials
     Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

User avatar
Sam1085
3StarLounger
Posts: 318
Joined: 23 Aug 2016, 07:43
Location: Sri Lanka

Re: Modify a Word Macro to inspect section breaks & page bre

Post by Sam1085 »

Thank you Hans,

It's works for me. Trick is works very well!!
-Sampath-