![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
I'm preparing an answer key for my students and my document looks like this:
Q1:XXXXXXXX? 1. XXX 2. XXX 3. XXX 4. XXX 2 (option 2. is the correct answer to the 1st question) (a blank line) Q2:XXX? ...(repeat) Is that possible to write a macro to color the respective correct options into red based on the key provided on every 7th line and then delete all those lines? Does anyone have a hint? Last edited by puff; 12-17-2017 at 02:57 PM. |
#2
|
||||
|
||||
![]()
If the document is exactly as you describe then the following should work. It does however rely on there being the same exact number of lines for each question and answer.
Code:
Sub MarkAnswers() Dim oRng As Range, oAns As Range Dim iQuest As Integer, iAns As Integer Const sNumList As String = "0123456789" On Error GoTo lbl_Exit For iQuest = 1 To ActiveDocument.Paragraphs.Count Step 7 'step through the questions Set oRng = ActiveDocument.Paragraphs(iQuest).Range 'get the first paragraph of the question oRng.MoveEnd wdParagraph, 6 'Get the last empty paragraph of the block Set oAns = oRng.Paragraphs(6).Range 'set a range to the answer line oAns.Collapse 1 'collapse the range to its start oAns.MoveEndWhile sNumList 'move the end of the range to the end of the answer number If Len(oAns.Text) > 0 Then 'if there is a number on that line iAns = oAns.Text + 1 'add a 1 to that number - to correspond to the answer number oRng.Paragraphs(iAns).Range.Font.ColorIndex = wdRed ' colour that paragraph red End If Next iQuest 'and process the next question lbl_Exit: 'clear up Set oRng = Nothing Set oAns = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
Thank you Graham! It colors the right options nicely. Yet after the coloring, it does not delete the lines that provide the answers (in the example, the line of "2 (option 2. is the correct answer to the 1st question)" need to be deleted). I tried to do so by adding "oAns.Delete" after "End If" but then it only deletes the number, not the whole line/paragraph.
|
#4
|
||||
|
||||
![]()
Sorry. I missed that bit
![]() Add the following two lines before Next iQuest i.e. Code:
oAns.End = oAns.Paragraphs(1).Range.End - 1 oAns.Text = "" Next iQuest 'and process the next question Code:
Sub ClearBlanks() Dim oPara As Paragraph On Error Resume Next For Each oPara In ActiveDocument.Paragraphs If Len(oPara.Range) = 1 Then If Len(oPara.Range.Next.Paragraphs(1).Range) = 1 Then oPara.Range.Delete Next oPara lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
![]()
They work perfectly! Thank you so much!
|
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Change Color (Being provided between the two codes) | bnyamin | Word VBA | 4 | 05-16-2017 10:55 PM |
Macro- find and replace from preselected options based on frequency (like an Ad libs) | blondie | Word VBA | 0 | 02-27-2017 01:41 PM |
![]() |
jrfoley3 | Excel | 3 | 08-26-2016 10:49 PM |
![]() |
jaradani | Word | 4 | 10-12-2014 09:42 PM |
Changing bar color automatically | Mahmuz | PowerPoint | 0 | 03-28-2012 11:49 PM |