HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Extract String From Text (Word)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textwordextractfromstring

Problem

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:=wdCharact

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#155378, answer score: 2

Revisions (0)

No revisions yet.