Find Text between Markers - III

S

sjwopg

I found some discussion about this topic, but I'm not getting it. Here is my
specific issue:

[FMR Data]
12/01/07 $1,500,000 $12,500
12/01/08 $1,200,000 $10,000
12/01/09 $1,680,000 $14,000
[FMR Data1]

I need to take the text between [FMR Data] and [FMR Data1] and copy it to a
different section of the document.

The spaces in the text represent tab stops. The selection would be the
beginning of the line with 12/01/07 through the end of the line with
12/01/09, including tab stops.

I've got the copying and pasting down, but selecting the text is problematic.

I am quite proficient using VBA in Access and Excel, and I realize that the
solution has to be quite simple. I must be having a senior moment. Your help
is really appreciated.

Thanks,

Steve
 
G

Greg Maxey

Something like this:

Sub ScratchMacro()
Dim oRng As Range
Dim myRange As Range
Dim i As Long
Dim y As Long
Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = "[FMR Data]"
.Wrap = wdFindStop
If .Execute Then
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = "[FMR Data1]"
y = Len(.Text)
If .Execute Then
oRng.Select
Set myRange = oRng.Duplicate
myRange.Start = i + 1
myRange.End = oRng.End - y
End If
End If
End With
ActiveDocument.Bookmarks("Test").Range.InsertAfter myRange.Text
End Sub

If there are multiple instances of text to be found you would need change
the If .Execute statement to a While .Execute and then sort out where you
want each instance put.
 
S

sjwopg

Thanks. It works great. I won't have to look for the same markers, but may
have to look for a bunch of different markers and do the same thing. My
thought is to set a couple of variables for the beginning and ending markers,
along with a bookmark variable. Then I'll keep looping through the code
changing the variables. I use this concept all the time with Access.

I stepped through the code and noticed that the i and y variables were
numbers. Are those the actual position of those characters in the document?

Thanks again. I'm sure I'll have a few more questions. I'm also going to run
through the Learn VBA in 15 minutes tutorial on the MVPS site.

Steve

Greg Maxey said:
Something like this:

Sub ScratchMacro()
Dim oRng As Range
Dim myRange As Range
Dim i As Long
Dim y As Long
Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = "[FMR Data]"
.Wrap = wdFindStop
If .Execute Then
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = "[FMR Data1]"
y = Len(.Text)
If .Execute Then
oRng.Select
Set myRange = oRng.Duplicate
myRange.Start = i + 1
myRange.End = oRng.End - y
End If
End If
End With
ActiveDocument.Bookmarks("Test").Range.InsertAfter myRange.Text
End Sub

If there are multiple instances of text to be found you would need change
the If .Execute statement to a While .Execute and then sort out where you
want each instance put.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

sjwopg said:
I found some discussion about this topic, but I'm not getting it. Here is
my
specific issue:

[FMR Data]
12/01/07 $1,500,000 $12,500
12/01/08 $1,200,000 $10,000
12/01/09 $1,680,000 $14,000
[FMR Data1]

I need to take the text between [FMR Data] and [FMR Data1] and copy it to
a
different section of the document.

The spaces in the text represent tab stops. The selection would be the
beginning of the line with 12/01/07 through the end of the line with
12/01/09, including tab stops.

I've got the copying and pasting down, but selecting the text is
problematic.

I am quite proficient using VBA in Access and Excel, and I realize that
the
solution has to be quite simple. I must be having a senior moment. Your
help
is really appreciated.

Thanks,

Steve
 
G

Greg Maxey

Steve,

Yes. i and y are numerical values. You are basically finding the first
tag. Collasping the range to the end of the found range. Finding the
second tag. Settting a duplicate range. Setting the start of the duplicate
range to the position in the document represented by i + 1, then backing up
the duplicate range by the length of the second tag. That leaves a range
the bounds the text between the tags.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Thanks. It works great. I won't have to look for the same markers,
but may have to look for a bunch of different markers and do the same
thing. My thought is to set a couple of variables for the beginning
and ending markers, along with a bookmark variable. Then I'll keep
looping through the code changing the variables. I use this concept
all the time with Access.

I stepped through the code and noticed that the i and y variables were
numbers. Are those the actual position of those characters in the
document?

Thanks again. I'm sure I'll have a few more questions. I'm also going
to run through the Learn VBA in 15 minutes tutorial on the MVPS site.

Steve

Greg Maxey said:
Something like this:

Sub ScratchMacro()
Dim oRng As Range
Dim myRange As Range
Dim i As Long
Dim y As Long
Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = "[FMR Data]"
.Wrap = wdFindStop
If .Execute Then
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = "[FMR Data1]"
y = Len(.Text)
If .Execute Then
oRng.Select
Set myRange = oRng.Duplicate
myRange.Start = i + 1
myRange.End = oRng.End - y
End If
End If
End With
ActiveDocument.Bookmarks("Test").Range.InsertAfter myRange.Text
End Sub

If there are multiple instances of text to be found you would need
change the If .Execute statement to a While .Execute and then sort
out where you want each instance put.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

sjwopg said:
I found some discussion about this topic, but I'm not getting it.
Here is my
specific issue:

[FMR Data]
12/01/07 $1,500,000 $12,500
12/01/08 $1,200,000 $10,000
12/01/09 $1,680,000 $14,000
[FMR Data1]

I need to take the text between [FMR Data] and [FMR Data1] and copy
it to a
different section of the document.

The spaces in the text represent tab stops. The selection would be
the beginning of the line with 12/01/07 through the end of the line
with 12/01/09, including tab stops.

I've got the copying and pasting down, but selecting the text is
problematic.

I am quite proficient using VBA in Access and Excel, and I realize
that the
solution has to be quite simple. I must be having a senior moment.
Your help
is really appreciated.

Thanks,

Steve
 
S

sjwopg

I get it. You find the starting point, then the ending point and create a
range between the two numbers.

I added some additional data and markers creating the following code:

Public Sub GetFmr()
Dim oRng As Range
Dim myRange As Range
Dim i As Long
Dim y As Long
Dim m1 As String
Dim m2 As String
Dim bm As String
Dim X As Integer

X = 0
m1 = "[FMR Data]"
m2 = "[FMR Data1]"
bm = "dv8"


Start:
Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = m1
.Wrap = wdFindStop
If .Execute Then
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = m2
y = Len(.Text)
If .Execute Then
oRng.Select
Set myRange = oRng.Duplicate
myRange.Start = i + 1
myRange.End = oRng.End - y
End If
End If
End With
ActiveDocument.Bookmarks(bm).Range.InsertAfter myRange.Text
If X = 0 Then
X = X + 1
m1 = "[BS Data]"
m2 = "[BS Data1]"
bm = "dv13"
GoTo Start
End If
End Sub

I hate to hard code variables, but I'm told there will be a standard set of
markers. It occurred to me that I should put some error trapping on a not
found condition. Maybe if myRange is 0, then go to a continue label. Also,
when I added the extra lines at the end, the macro seems to put an extra line
in the target bookmark.

My plan is to put this in a macro that runs when the document is opened the
first time. I think that kind of macro is AutoOpen in Word. Anyway, I'd set a
constant with a value of 0. The macro would only run if the value was 0. When
it finishes running, I'd set the constant to 1.

Thanks,

Steve

Greg Maxey said:
Steve,

Yes. i and y are numerical values. You are basically finding the first
tag. Collasping the range to the end of the found range. Finding the
second tag. Settting a duplicate range. Setting the start of the duplicate
range to the position in the document represented by i + 1, then backing up
the duplicate range by the length of the second tag. That leaves a range
the bounds the text between the tags.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Thanks. It works great. I won't have to look for the same markers,
but may have to look for a bunch of different markers and do the same
thing. My thought is to set a couple of variables for the beginning
and ending markers, along with a bookmark variable. Then I'll keep
looping through the code changing the variables. I use this concept
all the time with Access.

I stepped through the code and noticed that the i and y variables were
numbers. Are those the actual position of those characters in the
document?

Thanks again. I'm sure I'll have a few more questions. I'm also going
to run through the Learn VBA in 15 minutes tutorial on the MVPS site.

Steve

Greg Maxey said:
Something like this:

Sub ScratchMacro()
Dim oRng As Range
Dim myRange As Range
Dim i As Long
Dim y As Long
Set oRng = ActiveDocument.Range
ActiveDocument.Range(0, 0).Select
With oRng.Find
.Text = "[FMR Data]"
.Wrap = wdFindStop
If .Execute Then
i = oRng.End
oRng.Collapse direction:=wdCollapseEnd
.Text = "[FMR Data1]"
y = Len(.Text)
If .Execute Then
oRng.Select
Set myRange = oRng.Duplicate
myRange.Start = i + 1
myRange.End = oRng.End - y
End If
End If
End With
ActiveDocument.Bookmarks("Test").Range.InsertAfter myRange.Text
End Sub

If there are multiple instances of text to be found you would need
change the If .Execute statement to a While .Execute and then sort
out where you want each instance put.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

I found some discussion about this topic, but I'm not getting it.
Here is my
specific issue:

[FMR Data]
12/01/07 $1,500,000 $12,500
12/01/08 $1,200,000 $10,000
12/01/09 $1,680,000 $14,000
[FMR Data1]

I need to take the text between [FMR Data] and [FMR Data1] and copy
it to a
different section of the document.

The spaces in the text represent tab stops. The selection would be
the beginning of the line with 12/01/07 through the end of the line
with 12/01/09, including tab stops.

I've got the copying and pasting down, but selecting the text is
problematic.

I am quite proficient using VBA in Access and Excel, and I realize
that the
solution has to be quite simple. I must be having a senior moment.
Your help
is really appreciated.

Thanks,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top