Consulting

Results 1 to 4 of 4

Thread: Need Help with the following macros

  1. #1
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location

    Need Help with the following macros

    Hello all,

    I have a document with over 100 pages and on it I have four (4) different macros (see below) but none of them are working.

    Inside the document you will find a series of Multiple Choice Question with their corresponding choices of A , B, C and D.

    Right after these choices, the document is showing their corresponding answers which are showing as follows:

    Answer(A) is ..........
    Answer(B) is ..........
    Answer(C) is ..........
    Answer(D) is ........

    Basically, I would like the macro to insert or add a paragraph break before the word "Answer(A), "Answer(B)". "Answer (C)", Answer(D)".


    Thank you all your your time and assistant. I hope I expressed my ideas correctly if not please let me know. Thanks!

    Respectfully,




    
    Sub insSections1()
        ' Declare variables
        Dim rng As Range
        Dim aStory As Range
        
        ' Set the initial range to search in the entire document
        Set aStory = ActiveDocument.Content
        
        ' Execute find and replace for "Answer"
        With aStory.Find
            .ClearFormatting
            .Text = "Answer"
            .Forward = True
            .Wrap = wdFindContinue ' Stop at the first occurrence
            .Execute
        End With
        
        ' Check if "Answer" is found
        If aStory.Find.Found Then
            ' Move to the beginning of the found range
            aStory.Collapse wdCollapseStart
            ' Insert a page break before the found range
            aStory.InsertBreak Type:=wdPageBreak
        End If
    End Sub
    
    
    
    
    Sub insSections2()
        ' Declare variables
        Dim rng As Range
        Dim aStory As Range
        
        ' Set the initial range to search in the entire document
        Set aStory = ActiveDocument.Content
        
        ' Execute find and replace for "Answer (B)"
        With aStory.Find
            .ClearFormatting
            .Text = "Answer(B) "
            .Forward = True
            .Wrap = wdFindContinue ' Stop at the first occurrence
            .Execute
        End With
        
        ' Check if "Answer" is found
        If aStory.Find.Found Then
            ' Move to the beginning of the found range
            aStory.Collapse wdCollapseStart
            ' Insert a page break before the found range
            aStory.InsertBreak Type:=wdPageBreak
        End If
    End Sub
    
    
    
    
    
    
    Sub insSections3()
        ' Declare variables
        Dim rng As Range
        Dim aStory As Range
        
        ' Set the initial range to search in the entire document
        Set aStory = ActiveDocument.Content
        
        ' Execute find and replace for "Answer (C)"
        With aStory.Find
            .ClearFormatting
            .Text = "Answer(C) "
            .Forward = True
            .Wrap = wdFindContinue ' Stop at the first occurrence
            .Execute
        End With
        
        ' Check if "Answer" is found
        If aStory.Find.Found Then
            ' Move to the beginning of the found range
            aStory.Collapse wdCollapseStart
            ' Insert a page break before the found range
            aStory.InsertBreak Type:=wdPageBreak
        End If
    End Sub
    
    
    Sub insSections4()
    
    
    'this code will insert a paragraph or break page before the word Answer
    Dim sOt As Variant
    Dim aStory As StoryRanges
    
    
    Set aStory = ActiveDocument.StoryRanges
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindContinue
    .MatchCase = True
    .Text = "Answer)"
    .Replacement.Text = "^mAnswer)"
    .Execute Replace:=wdReplaceAll
    
    
    
    
    End With
    If Selection.Find.Found Then
        For Each sOt In aStory
            Selection.MoveLeft unit:=wdCharacter, Count:=1
            Selection.InsertBreak Type:=wdSectionBreakNextPage
            
        Next
    End If
    End Sub
    Attached Files Attached Files

  2. #2
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    You may want to look at Question and Answer Styles and either skip the macros or use them to assign styles to your paragraphs.

  3. #3
    VBAX Regular
    Joined
    Apr 2011
    Posts
    72
    Location
    Hi Chas,

    Thank you for the recommendation. I was looking into Question and Answer styles but not sure if that will work automatically or by default when setting up a style for Answers especially when the document have over 100+ pages.

    Somewhere (I can't remember where) I found the below code that was put together by Greg Maxey and I believe it was amended by Charles Kenyion (I think that was you). It works but it is inserting a lot of page breaks to the document. I would like to know if you can give a try and see how the results are and come out with a solution to the page breaks. I will keep searching to see if I find a solution. Thanks!


    Sub ScratchMacro()
    
    
    'A basic Word macro coded by Greg Maxey amended by Charles Kenyon
    
    
    Dim oRng As Range
    
    
    Application.ScreenUpdating = False
    
    
    Set oRng = ActiveDocument.Range
    
    
    With oRng.Find
    
    
    .Text = "Answer"
    
    
    While .Execute
    
    
    oRng.Paragraphs(1).PageBreakBefore = True
    
    
    Wend
    
    
    End With
    
    
    Application.ScreenUpdating = True
    
    
    lbl_Exit:
    
    
    Exit Sub
    
    
    End Sub

  4. #4
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    123
    Location
    Yes, that was me although I have no memory of it.
    What it does is set any paragraph with the word "Answer" in it to start on a new page. That is overkill for your document.

    Adding empty paragraphs is a VERY bad habit.
    See Shauna Kelly's 2.2 Why you should press Enter only once to end a paragraph .

    Apply a style that has the space-before formatting you need. Your entire document is formatted using the Normal style which is just plain sloppy.
    Read The Importance of Styles in Microsoft Word and spend some time learning to use styles. Writing macros to do your formatting is crazy.
    I've downloaded your document will play around with it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •