Moving to the next tab in VBA

J

Jill106

Hello,
I am new to using VBA in Word. Can someone please tell me
how I would select a range starting at a fixed character
and go to the next tabstop?

For example - go from character 40 to the next tab stop
position?

Any help would be very much appreciated! Thanks all.
 
H

Helmut Weber

Hi,
this one will take you to first tab after
the 40th character in the paragraph the start
of the selection is in:
It is assumed, that there is a tab after
character 40 and there is a sufficient number
of characters, of course.

Sub test789()
Dim rTmp As Range
Set rTmp = Selection.Paragraphs(1).Range
rTmp.start = rTmp.start + 40
With rTmp.Find
.Text = Chr(9)
.Execute
rTmp.Select
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
J

Jill106

Thanks Helmut!

But how would I select the range including the sentence in
it?

For example, if I have the phrase: 123 Maple Street and I
know this starts at Character 80. The next sentence starts
somewhere in the middle of the page at a tab stop position
as such:

123 Maple Street Next Sentence

How would I select a range with just 123 Maple Street?

I hope I am explaining this correctly. Thanks!!
 
H

Helmut Weber

Hi Jill,
something like

rTmp.Select
Selection.Range.Sentences(1).Select

though, I would abandon such an approach,
as transferring fuzzy natural language concepts
like "sentence" or "word" to a text processor
doesn't work. Applies even to characters in a way,
because of space, tab, line break, paragraph, end of cell,
end of row, section break, etc. It might work, sometimes,
but it is almost impossible, to exclude the cases,
when is is going to fail.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg Maxey

Jill,

Helmut has schooled me numerous times and may be along to offer
improvements. You can try the following. Which your cursor in the
paragraph in question, run the macro. Enter the starting character in the
input box, the ending character is a Tab.

Sub ScratchMacro()
Dim myRng As Range
Dim oChar
Dim rStop As Long
Dim rStart As Long

oChar = Chr(9)
Set myRng = Selection.Paragraphs(1).Range
rStart = InputBox("Enter the starting character position", "Start")

rStop = InStr(myRng, oChar)
myRng.SetRange Start:=myRng.Start + rStart, End:=rStop
myRng.Select
End Sub
 
H

Helmut Weber

Hi Submariner Greg,
I am along, but don't see any way of improvement.
Mission accomplished.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg

Jill,

Helmut has schooled me numerous times and may be along to offer
improvements. You can try the following. Which your cursor in the
paragraph in question, run the macro. Enter the starting character in
the input box, the ending character is a Tab.

Sub ScratchMacro()
Dim myRng As Range
Dim oChar
Dim rStop As Long
Dim rStart As Long

oChar = Chr(9)
Set myRng = Selection.Paragraphs(1).Range
rStart = InputBox("Enter the starting character position", "Start")

rStop = InStr(myRng, oChar)
myRng.SetRange Start:=myRng.Start + rStart, End:=rStop
myRng.Select
End Sub
 
G

Greg

Jill,

Helmut has schooled me numerous times and may be along to offer
improvements. You can try the following. Which your cursor in the
paragraph in question, run the macro. Enter the starting character in
the input box, the ending character is a Tab.

Sub ScratchMacro()
Dim myRng As Range
Dim oChar
Dim rStop As Long
Dim rStart As Long

oChar = Chr(9)
Set myRng = Selection.Paragraphs(1).Range
rStart = InputBox("Enter the starting character position", "Start")

rStop = InStr(myRng, oChar)
myRng.SetRange Start:=myRng.Start + rStart, End:=rStop
myRng.Select
End Sub
 
G

Greg

Jill,

Helmut has schooled me numerous times and may be along to offer
improvements. You can try the following. Which your cursor in the
paragraph in question, run the macro. Enter the starting character in
the input box, the ending character is a Tab.

Sub ScratchMacro()
Dim myRng As Range
Dim oChar
Dim rStop As Long
Dim rStart As Long

oChar = Chr(9)
Set myRng = Selection.Paragraphs(1).Range
rStart = InputBox("Enter the starting character position", "Start")

rStop = InStr(myRng, oChar)
myRng.SetRange Start:=myRng.Start + rStart, End:=rStop
myRng.Select
End Sub
 
G

Greg Maxey

Group,

I am sorry for the spate of identical posts that appeared under this thread.
I had tried several times to post from the Google Groups interface and kept
getting a server error. I finally gave up and posted from home through OE.
Apparently my earlier attempts found thier way through the ether and
arrrived late.
 
G

Greg

Jill,

Helmut has schooled me several times and may be back to offer further
advice here. Try:

Sub ScratchMacro()
Dim rTmp As Range
Dim rStop
Dim oChar
oChar = Chr(9)
Set rTmp = Selection.Paragraphs(1).Range
rStop = InStr(rTmp, oChar)
rTmp.SetRange Start:=rTmp.Start + 80, End:=rStop
rTmp.Select

End Sub
 
G

Greg

Jill,

Helmut has schooled me numerous times and may be along to offer
improvements. You can try the following. Which your cursor in the
paragraph in question, run the macro. Enter the starting character in
the input box, the ending character is a Tab.

Sub ScratchMacro()
Dim myRng As Range
Dim oChar
Dim rStop As Long
Dim rStart As Long

oChar = Chr(9)
Set myRng = Selection.Paragraphs(1).Range
rStart = InputBox("Enter the starting character position", "Start")

rStop = InStr(myRng, oChar)
myRng.SetRange Start:=myRng.Start + rStart, End:=rStop
myRng.Select
End Sub
 
G

Greg

Jill,

Helmut has schooled me numerous times and may be along to offer
improvements. You can try the following. Which your cursor in the
paragraph in question, run the macro. Enter the starting character in
the input box, the ending character is a Tab.

Sub ScratchMacro()
Dim myRng As Range
Dim oChar
Dim rStop As Long
Dim rStart As Long

oChar = Chr(9)
Set myRng = Selection.Paragraphs(1).Range
rStart = InputBox("Enter the starting character position", "Start")

rStop = InStr(myRng, oChar)
myRng.SetRange Start:=myRng.Start + rStart, End:=rStop
myRng.Select
End Sub
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Group,

I am sorry for the spate of identical posts that appeared under this
thread. I had tried several times to post from the Google Groups
interface and kept getting a server error. I finally gave up and
posted from home through OE. Apparently my earlier attempts found
thier way through the ether and arrrived late.

I hate that new Google Group Beta schmbeta.
I went there today to do a search and it positively sucks.
We do not get the left-hand side panel with the threads anymore (in the
results), you cannot do a search in a generic group like word.vba without
going through hoops, you have to see a single message when you want to check
out a search result (before you could click on the "see whole thread"
hyperlink, so on...
I definitely hated my experience and I hope that this Beta crap dies of its
own horrible death and that they go back to the way it was before... If they
want to do Yahoo/MSN type groups, then they should have a separate thingy...
The person responsible for that concept shift should have its head examined
and should be fired.

In case they insist on keeping it like that, does anybody know of a good
Usenet search engine site?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Concur with all. It is horrible. I sent them feedback but I got no
response other than the auto response indicating that they appreciate the
feedback.
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Concur with all. It is horrible. I sent them feedback but I got no
response other than the auto response indicating that they appreciate
the feedback.

I am on my way to do the same.
Maybe that if they get enough negative feedback, they'll do something
positive! Or am I too idealist here?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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