macro to select text from one point to another

E

eugene

Hi,

I would like to mark up a text and have a macro select the pieces of text
from one mark to another (eg all text between * to %), copy it and then
paste it into a new file.

Any suggestions on how this can be done neatly?
(I can do the copy/paste part of it. But I don't know how to instruct
selection between the marks. I would suppose one could use find. But know how
to select when finding.)
 
G

Greg Maxey

Hi,

I would like to mark up a text and have a macro select the pieces of text
from one mark to another (eg all text between * to %), copy it and then
paste it into a new file.

Any suggestions on how this can be done neatly?
(I can do the copy/paste part of it. But I don't know how to instruct
selection between the marks. I would suppose one could use find. But know how
to select when finding.)

Maybe something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oRng2 As Range)
Documents.Add
Selection.Range.Text = oRng2.Text
End Sub
 
E

eugene

Greg,

Thanks loads.

It does what exactly what I need. (Now to figure out exactly what it is
doing, so I can modify it if necessary.)

One thing I am doing away with for sure, and would not generally recommend,
is "Documents.Add." It creates a new document for each piece of text that is
copied which is not what i want. Instead, I will write to a single file.
--
eugene


Greg Maxey said:
Hi,

I would like to mark up a text and have a macro select the pieces of text
from one mark to another (eg all text between * to %), copy it and then
paste it into a new file.

Any suggestions on how this can be done neatly?
(I can do the copy/paste part of it. But I don't know how to instruct
selection between the marks. I would suppose one could use find. But know how
to select when finding.)

Maybe something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oRng2 As Range)
Documents.Add
Selection.Range.Text = oRng2.Text
End Sub
 
G

Greg Maxey

Greg,

Thanks loads.

It does what exactly what I need. (Now to figure out exactly what it is
doing, so I can modify it if necessary.)

One thing I am doing away with for sure, and would not generally recommend,
is "Documents.Add." It creates a new document for each piece of text that is
copied which is not what i want. Instead, I will write to a single file.
--
eugene



Maybe something like this:
Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oRng2 As Range)
Documents.Add
Selection.Range.Text = oRng2.Text
End Sub- Hide quoted text -

- Show quoted text -

Then maybe something like:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oSourceDoc As Word.Document
Dim oTargetDoc As Word.Document
Set oSourceDoc = ThisDocument
Set oTargetDoc = Documents.Add
Set oRng = oSourceDoc.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oTargetDoc, oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oDoc As Document, oRng2 As Range)
oDoc.Range.InsertAfter oRng2.Text & vbCr
End Sub
 
E

eugene

Greg,

Thanks again. i did something similar but less elegant:
I activate an existing document and then
Selection.TypeText Text:=oRng
Selection.TypeParagraph

--
eugene


Greg Maxey said:
Greg,

Thanks loads.

It does what exactly what I need. (Now to figure out exactly what it is
doing, so I can modify it if necessary.)

One thing I am doing away with for sure, and would not generally recommend,
is "Documents.Add." It creates a new document for each piece of text that is
copied which is not what i want. Instead, I will write to a single file.
--
eugene



Greg Maxey said:
I would like to mark up a text and have a macro select the pieces of text
from one mark to another (eg all text between * to %), copy it and then
paste it into a new file.
Any suggestions on how this can be done neatly?
(I can do the copy/paste part of it. But I don't know how to instruct
selection between the marks. I would suppose one could use find. But know how
to select when finding.)
Maybe something like this:
Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oRng2 As Range)
Documents.Add
Selection.Range.Text = oRng2.Text
End Sub- Hide quoted text -

- Show quoted text -

Then maybe something like:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oSourceDoc As Word.Document
Dim oTargetDoc As Word.Document
Set oSourceDoc = ThisDocument
Set oTargetDoc = Documents.Add
Set oRng = oSourceDoc.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oTargetDoc, oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oDoc As Document, oRng2 As Range)
oDoc.Range.InsertAfter oRng2.Text & vbCr
End Sub
 
G

Greg Maxey

Ok, good. I am not so sure that my method is elegant. Someone may be along
to tell us both that we have produced sow's ears. Still if it works it
works.

Initially I thought you wanted each bit of found text in a new separated
document.

Good luck.

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

Greg,

Thanks again. i did something similar but less elegant:
I activate an existing document and then
Selection.TypeText Text:=oRng
Selection.TypeParagraph

Greg,

Thanks loads.

It does what exactly what I need. (Now to figure out exactly what
it is doing, so I can modify it if necessary.)

One thing I am doing away with for sure, and would not generally
recommend, is "Documents.Add." It creates a new document for each
piece of text that is copied which is not what i want. Instead, I
will write to a single file. --
eugene



:
On Feb 13, 11:39 am, eugene <[email protected]>
wrote:
Hi,

I would like to mark up a text and have a macro select the pieces
of text from one mark to another (eg all text between * to %),
copy it and then paste it into a new file.

Any suggestions on how this can be done neatly?
(I can do the copy/paste part of it. But I don't know how to
instruct selection between the marks. I would suppose one could
use find. But know how to select when finding.)

--
eugene

Maybe something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oRng2 As Range)
Documents.Add
Selection.Range.Text = oRng2.Text
End Sub- Hide quoted text -

- Show quoted text -

Then maybe something like:

Sub ScratchMacro()
Dim oRng As Word.Range
Dim oSourceDoc As Word.Document
Dim oTargetDoc As Word.Document
Set oSourceDoc = ThisDocument
Set oTargetDoc = Documents.Add
Set oRng = oSourceDoc.Range
With oRng.Find
.Text = "[\*]*%"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute
'Clip special characters from found text
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
ScratchMacro2 oTargetDoc, oRng
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub
Sub ScratchMacro2(ByVal oDoc As Document, oRng2 As Range)
oDoc.Range.InsertAfter oRng2.Text & vbCr
End Sub
 

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