rsrasc
02-25-2024, 08:45 AM
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
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