![]() |
#1
|
|||
|
|||
![]()
I have two word documents. The first one is named "Questions" and the second one is named "Answers".
I would like to have a macro so I can import the information from the second file "Answers" to the first file named "Questions". Both documents have the same numbers of questions and answers (55--but I would like to have it for an unlimited numbers of Q&As, if needed). Here is a sample of one question in the first document. 1. Why macros are important? a. A macro is a collection of commands. b. Thy can automate almost anything. c. Macros save time and expand the capabilities of the programs you use every day. d. All of the above are correct. Here is sample answer in the second document: 1. (d) The requirement is to identify the reason why macros in word document are important. What I would like to see is the following output from the macro (the question follow by the answer choice after letter "d". The wording should read "Answer: D" (capital letter)- or any letter based on the answer choice showing for the same number in the second document): 1. Why macros are important? a. A macro is a collection of commands. b. Thy can automate almost anything. c. Macros save time and expand the capabilities of the programs you use every day. d. All of the above are correct. Answer: D 1. (d) The requirement is to identify the reason why macros in word document are important. Any ideas or suggestions are welcome. As always your assistance is appreciated. Let me know if more information is required. Cheers |
#2
|
|||
|
|||
![]()
Are you going to be doing this, or is this supposed to be some sort dynamic process? What I mean is, what CAUSES this process? Are you moving the content from Answers to Questions as a one-time thing? Making Questions into one document?
|
#3
|
|||
|
|||
![]()
Thank you for you question, and the answer are two. I have about 60 word documents containing only multiple choice questions (questions with the option a, b, c and d), and I have a different word file with the answer choices (together with the explanations).
If there is macro that could help me automate this--putting the questions, adding the answer choice, and the explanations--that will be great--kind of dynamic process. Yes, I would like to move the content from the Answer file to the Question file, as a one thing, and maybe converting the Question file into one document. If I can't get a macro, my choice will be to do it manually. I hope this clarify your question. PS. If there are any other options to accomplish this, I open for ideas/suggestions. Thanks! |
#4
|
|||
|
|||
![]()
rsrasc, I'm trying to private-mail you, but I'm not sure that it's working. Have you received anything from me?
|
#5
|
|||
|
|||
![]()
Hi Larry,
Yes, I received your private mail. Thanks! |
#6
|
||||
|
||||
![]()
Larry: Please keep forum-related discussions in the forum.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
My kudos to Larry Sulky for the below code. I personally asked Larry for help and he responded to me with grace and attitude well beyond expectation.
The code was designed to group a series of questions (multiple choice questions) with their answers. My original intention was to put the questions and the answer choices and explanations in two different word documents so we could combined them together in one file. With this in mind, Larry developed this amazing code to be used with just one file. The intention was to have all the questions (the test file has 169 questions) listed all the way to the end of the file (questions with the a, b, c, and d option). After all the questions were listed we add it all the answer explanations to all the questions, and the format of the answer explanation was as follow: 168. (a) The requirement is to identify the correct statement with respect to the primary orientation of operational auditing Based on this, Larry Sulky, with his amazing code was able to come out with a solution to my request, and here is how the output looks now. ================================================= 168. Operational auditing is primarily oriented toward a. Future improvements to accomplish the goals of management. b. The accuracy of data reflected in management’s financial records. c. The verification that a company’s financial statements are fairly presented. d. Past protection provided by existing internal control. Answer: A 168. (a) The requirement is to identify the correct statement with respect to the primary orientation of operational auditing ================================================== = If the code is not listed in the appropriate format, please accept my apologies. Try to followed Macropod advise, but can't get to find it. Cheers! Code:
Sub CollateQandA() ' Initialise common Find/Replace parameters. With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Format = False .MatchCase = True .MatchWholeWord = False .MatchWildcards = True .MatchSoundsLike = False .MatchAllWordForms = False End With ' Ensure that there is a final dummy "question" by finding the first real answer ' and inserting a dummy there. Selection.HomeKey Unit:=wdStory With Selection.Find .Text = "[^013^l][0-9]@.[^s ]@[(][a-z]@[)]" .Replacement.Text = "" .Forward = True End With If Not Selection.Find.Execute Then Goto Finish Selection.HomeKey Unit:=wdLine Selection.TypeParagraph Selection.TypeText ("0000. PLACEHOLDER QUESTION.") ' Ensure that there is a final dummy "answer". Selection.EndKey Unit:=wdStory Selection.TypeParagraph Selection.TypeText ("0000. (z) PLACEHOLDER ANSWER.") ' Ensure that there is a hard return before the first line of text. Selection.HomeKey Unit:=wdStory Selection.TypeParagraph Do Dim myNumberStart As Integer Dim myNumberStop As Integer Dim myNumber As String Dim myLetterStart As Integer Dim myLetterStop As Integer Dim myLetter As String ' Hard returns and line breaks seem to be used interchangeably, so look for both. ' Also look for space or non-breaking space. ' Find the next correct answer. With Selection.Find .Text = "[^013^l][0-9]@.[^s ]@[(][a-z]@[)]" .Replacement.Text = "" .Forward = True End With If Not Selection.Find.Execute Then Goto Finish ' Get the number and letter of the answer. myNumberStart = 2 myNumberStop = InStr(1, Selection.Text, ".") myLetterStart = InStr(1, Selection.Text, "(") + 1 myLetterStop = InStr(1, Selection.Text, ")") myNumber = Mid(Selection.Text, myNumberStart, myNumberStop - myNumberStart) myLetter = UCase(Mid(Selection.Text, myLetterStart, myLetterStop - myLetterStart)) ' Get the whole answer and cut it. Selection.Collapse direction:=wdCollapseStart With Selection.Find .Text = "[!^013^l]@[^013^l]" .Replacement.Text = "" .Forward = True End With If Not Selection.Find.Execute Then Goto Finish Selection.Cut ' Find the matching question. With Selection.Find .Text = "[^013^l]" & myNumber & "." .Replacement.Text = "" .Forward = False End With If Not Selection.Find.Execute Then ' If no match, mark the answer as "UNMATCHED" and paste it at the bottom. 'MsgBox "Missing question #" & myNumber & ".", , "Uh-oh" Selection.EndKey Unit:=wdStory Selection.TypeParagraph Selection.TypeText "UNMATCHED " Selection.Paste Selection.HomeKey Unit:=wdStory ' Go back to the top to find the next answer. Goto EndLoop ' Skip the rest of the loop processing, since we have no match. End If ' Find the NEXT question, which marks the end of the CURRENT question. Selection.Collapse direction:=wdCollapseEnd With Selection.Find .Text = "[^013^l][0-9]@." .Replacement.Text = "" .Forward = True End With If Not Selection.Find.Execute Then Goto Finish ' Insert the Answer marker and paste the actual answer. Selection.Collapse direction:=wdCollapseStart Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeText ("Answer: " & myLetter) Selection.TypeText Text:=Chr(11) ' Insert a line break, since that seems to be preferred. Selection.TypeText "MATCHED " Selection.Paste EndLoop: Loop Finish: Selection.HomeKey Unit:=wdStory '''Selection.Delete Unit:=wdCharacter, Count:=1 With Selection.Find .Text = "[^013^l]MATCHED " .Replacement.Text = "^l" .Forward = True End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "0000. (z) PLACEHOLDER ANSWER." .Replacement.Text = "" .Forward = True .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll Selection.Delete Unit:=wdCharacter, Count:=1 With Selection.Find .Text = "0000. PLACEHOLDER QUESTION." .Replacement.Text = "" .Forward = True .MatchWildcards = False End With Selection.Find.Execute Replace:=wdReplaceAll '''Selection.Delete Unit:=wdCharacter, Count:=1 Selection.HomeKey Unit:=wdStory Bye: ' Just for debug purposes. End Sub Last edited by macropod; 04-03-2014 at 05:34 PM. Reason: Added code tags & formatting |
#8
|
||||
|
||||
![]()
rsrac: kindly read https://www.msofficeforums.com/faq.p...ontact_members
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
Hi, Paul ---
I read the FAQ that you referenced but I'm not clear on one point of protocol: if two members are testing and debugging code, should every iteration of that code be posted here? I ask because some of the things that rsrasc and I were working through were small, silly glitches, so each iteration of the code was 99% the same as the previous one. Cheers! --- larry |
#10
|
|||
|
|||
![]()
rsrasc, Thanks for the kind words. There are optimisations that could be made that I didn't bother with, since your files are pretty small. One that I should have made, however, is to stop screen updating while the macro is running. The macro jumps around a lot, which takes screen refresh time. At the beginning of the macro, you could add:
Code:
Application.ScreenUpdating = False Code:
Application.ScreenUpdating = True |
#11
|
|||
|
|||
![]()
I will add the new code and will continue testing the macro. Out of 60 documents I'm down now to 27 more. What a great feeling!
Dear Macropod, Please consider this thread closed and I would like to see the stamp "solved". It is so nice to see it. Out of four (4) request, so far all of them have been resolved. Hummm, good score! I'm hoping that any future request can be solved thru the forum so I don't have to damage my scoring average!. The only reason I asked Larry for support it was because no one responded to my request. If this is a sin, I apologize for that. I hope things will get better in the future. Sincerely, Roberto |
#12
|
||||
|
||||
![]()
Larry,
The question here really wasn't one of debugging code. Indeed, until post #7, where the final code was posted, there hadn't been any discussion of code. What had happened is that rsrasc had been asked a question, which had been answered. The next we hear of - less than 24 hours later is that you two had taken the discussion off-line. That effectively means no-one else can contribute. I'm certainly not going to waste my time trying to work out a solution for a problem when I know that's happening and I doubt fumei or anyone else would either. As it is, even though rsrasc is happy enough with it, the solution you came up with is quite inefficient. Had the code been discussed in the forum, we could have contributed to making it much more efficient... rsrasc: re: Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
custom icon, undo/redo for macro, permanent macro | Rapier | Excel | 0 | 08-05-2013 06:30 AM |
How do I assign a macro to a button when the macro is in my personal workbook? | foolios | Excel Programming | 2 | 07-27-2011 02:41 PM |