Reputation: 23283
I have a userform that I'm using, that allows a user to input a number. I will use this number to expand a selection by that many paragraphs.
The idea is, I have a document with hundreds of definitions. These start with "[word] - means [etc.]", so I can look for "means", select the paragraph that it is in, then ask the user if they want to add this paragraph to the Index.
Occasionally, definitions are spread over two paragraphs. I'd like the user to be able to input "2", and have the two paragraphs selected.
I've got the form working, except I'm stuck on how to loop this. I'm not too familiar with Word VBA (but am with Excel), so am not sure where I'm going wrong.
The focus is the Case 0
block, see my comments. This is where I want to expand the selection by X paragraphs.
Sub find_Definitions()
Dim defText As String, findText$
Dim oRng As Word.Range, rng As Word.Range
Dim myForm As frmAddDefinition
Set myForm = New frmAddDefinition
Dim addDefinition$, expandParagraph&
expandParagraph = 1
Set oRng = ActiveDocument.Range
findText = InputBox("What text would you like to search for?")
With oRng.Find
.Text = findText
While .Execute
Start_Find:
Set rng = oRng.Paragraphs(1).Range
rng.Select
defText = oRng.Paragraphs(1).Range
myForm.Show
Select Case myForm.Tag
Case 0 ' Expand the paragraph selection
expandParagraph = InputBox("How many paragraphs to extend selection?")
rng.MoveEnd unit:=wdParagraph, Count:=expandParagraph
rng.Select
defText = rng
' Here I want to ask if this is okay, or to expand again
' ?????
ActiveDocument.Indexes.MarkEntry Range:=rng, entry:=defText, entryautotext:=defText
Case 1 ' No, do not add to the index
' do nothing
Case 2 ' Yes, add to index
ActiveDocument.Indexes.MarkEntry Range:=rng, entry:=defText, entryautotext:=defText
Case 3 ' Cancel, exit the sub
MsgBox ("Exiting macro")
GoTo lbl_Exit
End Select
Wend
End With
lbl_Exit:
Unload myForm
Set myForm = Nothing
End Sub
Edit: Using the code above, it will select the highlighted text:
But, I want to include the "Way One" and "Way Two", so I would like to enter, via that inputBox
2, to extend the selection down two paragraphs. How can I get it to do that? This is where I am stuck. Does that make sense?
Upvotes: 0
Views: 225
Reputation: 25663
Well, in Word if you want to extend a Range to include "something more" you generally use one of the Range.MoveEnd
method (there's also a MoveStart
method). In Word, a Range is a group of contiguous characters, running from left-to-right, top-to-bottom in the document. Every Range has a start and an end point; these can be the same position in the document. Think of it like a selection, except a Range is not visible and you can have as many as you like. (So similar to Excel in this respect.) If you want to extend a selection you hold the Shift key and type the Arrow keys; in the object model you use MoveEnd
(and MoveStart
).
These methods take two parameters: Unit
and Count
.
Unit
lets you specify the "something" (character, word, paragraph, story...) as a WdUnits
enum. Count
lets you specify how many "somethings" to extend the end by.
The following code snippet demonstrates how I understand you want to interact with the user. It starts from the current selection, instead of using Find, but that shouldn't be a problem for you
Dim rng As word.Range
Dim nrParas As String
Set rng = Selection.Paragraphs(1).Range
Do
nrParas = InputBox("How many more paragraphs do you need?")
If Not IsNumeric(nrParas) Then Exit Sub 'Or Exit Do if other code follows
If CLng(nrParas) > 1 Then '>1 because 1 is a given
rng.MoveEnd wdParagraph, CLng(nrParas) - 1 '-1 because 1 is a given
End If
'for visual verification
rng.Select
Loop While IsNumeric(nrParas) And nrParas > 0
Upvotes: 1