In Word VBA, how to combine one selection to another selection executed from find
I have a document where question and answer have different styles assigned to each (for question "*Questions" and for answer "Normal"), answer can have multiple paragraphs or even image, so basically I need code to find/search a particular word in document and when found, code need to select from start of that question till the end of its answer (or start of next question). I have code in break but somehow could not able to compile it (not able to combine selection form Previous_Question till Next_Question). code goes like this:
Sub Previous_Question() Selection.Find.Style = "*Questions" Selection.Find.Forward = False Selection.Find.Execute End Sub Sub Next_Question() Selection.Find.Style = "*Questions" Selection.Find.Forward = True Selection.Find.Execute End Sub Sub Find_Word() Selection.Find.ClearFormatting With Selection.Find .Text = InputBox(sPrompt, sTitle, sDefault) End With Do While Selection.Find.Execute Selection.StartOf Unit:=wdParagraph Selection.MoveEnd Unit:=wdParagraph sBigString = sBigString + Selection.Text Selection.MoveStart Unit:=wdParagraph Loop Documents.Add DocumentType:=wdNewBlankDocument Selection.InsertAfter (sBigString) End Sub
I usually recommend not using the Selection object in macros, where possible, but you might have a special reason to use the Selection as input by the user.
I tested this code on a blank document after typing =RAND(5,5), Enter. This creates some paragraphs some of which contain the word 'create' which I have set as the default in the Input box.
Open in new window