# Extract String From Text (Word)

I need to find a specific string of text from a large source of material. I've just used Excel VBA up to this point, so I don't know the Word objects and I ended up just recording some macros and working with what I had. However, this way is very inefficient. I looked on Stack Overflow, but I didn't understand what they were doing, so I just did it this way.

What I am mostly looking for is the objects used in Word VBA that are equivalent to the Excel 'Range', 'Cells', 'Row', and 'Column'. I looked on MSDN, but there is so much material there that you need to have an idea of what you are looking for, and I couldn't find any other sites that would help.

This is what I wrote, and it did the job, but it is very inefficient.

Sub FindMediaInBraces()

' Separate needed text
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "[Media"
.Replacement.Text = "^p^p[Media"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = ".jpg]"
.Replacement.Text = ".jpg]^p^p"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = ".png]"
.Replacement.Text = ".png]^p^p"
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute Replace:=wdReplaceAll

' Remove text that doesn't start correctly
Dim i As Long

Selection.HomeKey Unit:=wdStory
For i = 1 To ActiveDocument.Paragraphs.count
Selection.MoveDown Unit:=wdParagraph, count:=1, Extend:=wdExtend
With Selection.Find
.Execute FindText:="[Media"
.Forward = True
If .Found = True Then
Selection.MoveDown Unit:=wdParagraph, count:=1
Else
Selection.Delete Unit:=wdCharacter, count:=1
End If
End With
Next

End Sub


## Example Text

Original

Type: MC
Objectives: 2.6
16) [HTML]If <i>y</i> varies directly with <i>x</i>, write an equation for the direct variation. Then find the value. If <i>y</i> = -4 when <i>x</i> = -28, what is the value of <i>x</i> when <i>y</i> = 5?[/HTML]
*a. $y=\frac{1}{7}x;\,\, 35$
b. $y=-\frac{1}{7}x;\,\, -35$
c. $y=7x;\,\, \frac{5}{7}$
d. $y=-7x;\,\, -\frac{5}{7}$

Type: MC
Objectives: 3.3
21) Select the graph that correctly represents the following system of equations:
$y=-\frac{5}{3}x+3$
$y=\frac{1}{3}x-3$
*a. [Media:Media/Images/Problem 21a.jpg]
b. [Media:Media/Images/Problem 21b.jpg]
c. [Media:Media/Images/Problem 21c.jpg]
d. [Media:Media/Images/Problem 21d.jpg]


After Macro

[Media:Media/Images/Problem 21a.jpg]
[Media:Media/Images/Problem 21b.jpg]
[Media:Media/Images/Problem 21c.jpg]
[Media:Media/Images/Problem 21d.jpg]


This is what I found on Stack Overflow, but I don't know the objects to use to apply it, and I don't know if it would actually work in this case.

dim str as string
dim openPos as integer
dim closePos as integer
dim midBit as string

str = "NUMBER(8,3)"
openPos = instr (str, "(")
closePos = instr (str, ")")
midBit = mid (str, openPos+1, closePos - openPos - 1)


A good way to do this would be by .Sentences, assuming your string doesn't appear more than once per sentence. You can use instr to get the location of your strings and then store them in an array. Then print them wherever you want:

Sub GetMediaStrings()
Const BEGIN_STRING As String = "[Media:"
Const END_STRING As String = ".jpg"
Dim searchRange As Range
Dim startString As Long
Dim endString As Long
Dim results() As String
ReDim results(1 To 2)
For Each searchRange In ActiveDocument.Sentences
startString = InStr(1, searchRange, BEGIN_STRING, vbTextCompare)
If startString > 0 Then
endString = InStr(1, searchRange, END_STRING, vbTextCompare)
If endString > 0 Then
results(UBound(results) - 1) = Mid(searchRange, startString, endString - startString + Len(END_STRING))
ReDim Preserve results(1 To UBound(results) + 1)
End If
End If
Next
End Sub


You might also want to do it via .StoryRanges.