Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2007

Tip: Looking for answers? Try searching our database.

Find Text between Markers - III

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sjwopg - 26 Oct 2007 01:16 GMT
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
Greg Maxey - 26 Oct 2007 02:36 GMT
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.

Signature

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
[quoted text clipped - 26 lines]
>
> Steve
sjwopg - 26 Oct 2007 17:07 GMT
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

> Something like this:
>
[quoted text clipped - 58 lines]
> >
> > Steve
Greg Maxey - 26 Oct 2007 20:07 GMT
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.

Signature

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
[quoted text clipped - 80 lines]
>>>
>>> Steve
sjwopg - 26 Oct 2007 21:25 GMT
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

> Steve,
>
[quoted text clipped - 89 lines]
> >>>
> >>> Steve
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.