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 / November 2007

Tip: Looking for answers? Try searching our database.

Selecting text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Harddrive747 - 14 Nov 2007 16:51 GMT
I have text files that have router information in them.  I need to move a
section of text from the middle of the file to the top.

Here is what I manually do.  I find the text of "running."  I then press the
home key to get it to the left.  I then want to selection (Highlight) the
text to the next iteration of "Device," but I don't want the device involved.

The finding part is simple and the other parts of cutting and moving it to
the top of the document is straight forward.  What I'm having issue with is
getting the highlighting done.  The number of lines between for the section
will vary from file to file.  So I need to be able to select until I find the
word "device" comes up again.

I will include the section of code that I'm trying to do this with:

Selection.MoveDown unit:=wdLine, Count:=597, Extend:=wdExtend
   With ActiveDocument.Content.Find
       Selection.MoveDown unit:=wdLine, Extend:=wdExtend
       .Execute findtext:="Device", Forward:=True
   End With

I tried the stuff in the help files and they work to an extent, but not
fully.  So any help would be greatly appreachated.

Tery
Astrid - 15 Nov 2007 02:55 GMT
Hi Terry,

> Here is what I manually do.  I find the text of "running."  I then press the
> home key to get it to the left.  I then want to selection (Highlight) the
> text to the next iteration of "Device," but I don't want the device involved.

Try something like:

 Selection.HomeKey wdStory
 'get rid of any previous format criteria in the find dialog
 Selection.Find.ClearFormatting
 Selection.Find.Replacement.ClearFormatting
 
 With Selection.Find
   .MatchWildcards = True
   .Text = "(Running)(*)(Device)"
 End With

 If Selection.Find.Execute() Then
   Selection.MoveEnd unit:=wdCharacter, Count:=-(Len("Device"))
 End If

For more info on how to use wildcards while searching:
http://word.mvps.org/FAQs/General/UsingWildcards.htm

Hope this helps,
kind regards,
Astrid
Harddrive747 - 15 Nov 2007 15:01 GMT
Astrid this code did not work at all.  It didn't even attempt to find the
word device.

It also did not select any text.  I can get it to select text until just
before it finds device, but not all the way to the end.

So if anyone has any suggestion, please let me know.  I will keep trying it
myself.

> Hi Terry,
>
[quoted text clipped - 24 lines]
> kind regards,
> Astrid
Astrid - 15 Nov 2007 15:15 GMT
Hi,

It works fine when I try it. I have a file open with the text:

The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over
the lazy dog.
RUNNING Selection should start at the beginning of the word before this
line. The quick brown fox jumps over the lazy dog. Selection should end
before the word at the end of this line. DEVICE The quick brown fox jumps
over the lazy dog.  The quick brown fox jumps over the lazy dog.

For the visibility both words in the text above are in all caps.
Of course instead of RUNNING I used Running and instead of DEVICE I actually
used Device cause the code is case sensitive (Could that be the problem?).

Then I put all the code inside a macro called Test and run it, for me it
selects all the text I would expect:

Running Selection should start at the beginning of the word before this
line. The quick brown fox jumps over the lazy dog. Selection should end
before the word at the end of this line.

Is this not the same on your system?

> Astrid this code did not work at all.  It didn't even attempt to find the
> word device.
[quoted text clipped - 33 lines]
> > kind regards,
> > Astrid
Harddrive747 - 15 Nov 2007 16:03 GMT
No this does not happen on my system.  I am using Word 2003 and when I run
the program it just doesn't do anything.

The code that I was able to get it to do what I want, but I don't know how
to get to to stop is

Do
   Selection.MoveDown unit:=wdLine, Extend:=wdExtend
Loop

this will select the text, but I can't figure out how to get the loop to
break at the part that I need it to.  I have tried selection.find and it will
find the word, but then it unselects everything that I have selected.  I need
to break this loop.  I am thinking of trying to count the lines between the
two, but I'm not sure to get it to count, because where do I tell it to stop.

it's almost like i need to be able to read the charcters and if it matches a
certain string then break the loop.  I will continue to check on it.
   

> I have text files that have router information in them.  I need to move a
> section of text from the middle of the file to the top.
[quoted text clipped - 21 lines]
>
> Tery
Astrid - 15 Nov 2007 21:44 GMT
Could you post a bit of the text that contains the two words so I can try to
find out what is causing the code to work differently on your system then on
mine?

> No this does not happen on my system.  I am using Word 2003 and when I run
> the program it just doesn't do anything.
[quoted text clipped - 41 lines]
> >
> > Tery
Harddrive747 - 16 Nov 2007 18:35 GMT
Here is a short section of the text.  The "....." indicates more text
inbetween.

Device :: <router name> Command :: show running-config

Building configuration...

Current configuration : 17356 bytes
!
.
.
.
end


Device :: <router name> Command :: show standby

> Could you post a bit of the text that contains the two words so I can try to
> find out what is causing the code to work differently on your system then on
[quoted text clipped - 45 lines]
> > >
> > > Tery
Russ - 20 Nov 2007 04:45 GMT
Harddrive747,

I looked at your example and if the pattern holds true, I thought you could
search using these wildcards:

Sub Move_Running_To_Top()
Dim aRange As Word.Range
Set aRange = ActiveDocument.Content

With aRange.Find
   .Text = "Device[!^13]@running*\nend[^13 ]{1,}" 'space character included
   .MatchWildcards = True
   While .Execute
       ActiveDocument.Range(0, 0).InsertAfter aRange.Text
       aRange.Delete
   Wend
End With
End Sub
'========================
To insert found text in the same order that it was found, I made this
adjustment:

Sub Move_Running_To_Top2()
Dim aRange As Word.Range
Dim aRange2 As Word.Range

Set aRange = ActiveDocument.Content
Set aRange2 = ActiveDocument.Range(0, 0)

With aRange.Find
   .Text = "Device[!^13]@running*\nend[^13 ]{1,}" 'space character included
   .MatchWildcards = True
   While .Execute
       aRange2.InsertAfter aRange.Text
       aRange2.Collapse direction:=wdCollapseEnd
       aRange.Delete
   Wend
End With
End Sub

> Here is a short section of the text.  The "....." indicates more text
> inbetween.
[quoted text clipped - 71 lines]
>>>>
>>>> Tery

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Harddrive747 - 21 Nov 2007 15:39 GMT
Russ this worked pretty well, but I had to change a few things and once I
did, it worked great.  So here how I modified it so that I could get it to do
exactly what I wanted.

Sub Move_Running_To_Top()
Dim aRange As Word.Range
Dim aRange2 As Word.Range

'Set aRange = ActiveDocument.Content
'Set aRange2 = ActiveDocument.Range(0, 0)
Selection.find.ClearFormatting
With Selection.find
       .text = "running"
       .Replacement.text = "}"
       .Forward = True
       .Wrap = wdFindContinue
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchAllWordForms = False
       .MatchSoundsLike = False
       .MatchWildcards = True
   End With
   Selection.find.Execute
   Selection.homekey Unit:=wdLine

With Selection.find
   .MatchWildcards = True
   .text = "Device[!^13]@running*\nend[^13 ]{1,}" 'space character included
   While .Execute
       Selection.Cut
       Selection.homekey Unit:=wdStory
       Selection.MoveDown Unit:=wdLine, Count:=1
       Selection.Paste
   Wend
End With
Selection.InsertAfter Chr(13)
Selection.InsertAfter Chr(13)
End Sub

I am now looking to do something else.  I will start a new thread to work on
that one after, I look up to see if someone has talked about it.

> Harddrive747,
>
[quoted text clipped - 111 lines]
> >>>>
> >>>> Tery
Russ - 21 Nov 2007 20:54 GMT
Tery,
Glad you got it working.
Although, it seems curious that in your code you are changing the first
instance of the word 'running' to a '}' character?

And it looks like you didn't want to move the found text to the very
beginning of the document, but maybe after the first paragraph along with a
couple of empty paragraphs, afterwards, for spacing?

That is easily tweaked by using these changes:
'========================
Sub Move_Running_To_Top()
Dim aRange As Word.Range
Set aRange = ActiveDocument.Content

With aRange.Find
   .Text = "Device[!^13]@running*\nend[^13 ]{1,}" 'space character included
   .MatchWildcards = True
   While .Execute
       ActiveDocument.Paragraphs(1).Range.InsertAfter aRange.Text _
           & vbCr & vbCr
       aRange.Delete
   Wend
End With
End Sub
'========================
To insert found text in the same order that it was found, I made this
adjustment:
'========================
Sub Move_Running_To_Top2()
Dim aRange As Word.Range
Dim aRange2 As Word.Range

Set aRange = ActiveDocument.Content
Set aRange2 = ActiveDocument.Paragraphs(1).Range

With aRange.Find
   .Text = "Device[!^13]@running*\nend[^13 ]{1,}" 'space character included
   .MatchWildcards = True
   While .Execute
       aRange2.InsertAfter aRange.Text & vbCr & vbCr
       aRange2.Collapse direction:=wdCollapseEnd
       aRange.Delete
   Wend
End With
End Sub
'========================
I try to use the Range object, rather than the Selection object for speed.
So my code automatically moves everything to the top. Did you want to
**manually** select only certain devices that are running to move to the
top?

P.S.
I unintentionally left a \n in my original code posting because in MacWord I
have to use that instead of any ^13. So if it causes no problems in WinWord,
then ignore it, otherwise change the \n to a ^13 in the wildcard search
strings.

> Russ this worked pretty well, but I had to change a few things and once I
> did, it worked great.  So here how I modified it so that I could get it to do
[quoted text clipped - 161 lines]
>>>>>>
>>>>>> Tery

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
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.