Select Tool in Macro ??

I

InOverMyHead

I want to "select" everything between two tabs. This will be one step in my
little macro, afterwhich I delete the selected text. But how do I do it? I
can "Find" - Tab - but then I need to turn on Select then Find the second
tab. How do I turn on Select?

Any help would be greatly appreciated. (PS: I'm using Word 2000)

Bob
 
S

Shauna Kelly

Hi Bob

Try something like the following:

Option Explicit

Sub DeleteTextBetweenTabs()

Dim oRange As Word.Range
Dim nHowFarToTheNextTab As Long

'Purpose: Find a tab, and delete all text
'between it and the next tab.
'
'User needs to click in the text
'before the first
'tab, and then run this macro.

'Collapse the selection
Selection.Collapse wdCollapseEnd

With Selection.Find
'Find the first tab
.ClearAllFuzzyOptions
.ClearFormatting
.Text = vbTab
.Format = False
.Forward = True
.Wrap = wdFindStop
.Execute

If .Found Then
Set oRange = Selection.Range

'We want the text *between* tabs, so
'move one character past the tab
oRange.Move Unit:=wdCharacter, Count:=1

'Extend the range to the next tab.
nHowFarToTheNextTab = oRange.MoveEndUntil _
(cset:=vbTab, Count:=wdForward)

If nHowFarToTheNextTab > 0 Then
'If there was a second tab to find, then
'oRange now spans the text between the two tabs.

'Following line is for testing. Remove for real life.
oRange.HighlightColorIndex = wdBlue

'Following is the real-life line. Remove the quote
'mark at the beginning of the line to activate it.
'oRange.Delete

'Position the cursor just after the range
oRange.Collapse wdCollapseEnd
oRange.Select
Else
MsgBox "We couldn't find a second tab"
End If
Else
MsgBox "No tabs found"
End If

End With
End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Greg

Bob,

You don't really need to select the text to delete it. Here is a macro
that finds text between the first and second tabs following the
insertion point and deletes the text while leaving the tabs.

Sub DeleteTextBetweenTabs()
Dim myRange As Range
Set myRange = Selection.Range
myRange.End = ActiveDocument.Range.End
With myRange.Find
.ClearFormatting
.MatchWildcards = True
.Text = "^t" & "*" & "^t"
.Execute
myRange.Start = myRange.Start + 1
myRange.End = myRange.End - 1
myRange.Delete
End With
End Sub
 
I

InOverMyHead

Greg said:
Bob,

You don't really need to select the text to delete it. Here is a macro
that finds text between the first and second tabs following the
insertion point and deletes the text while leaving the tabs.

Sub DeleteTextBetweenTabs()
Dim myRange As Range
Set myRange = Selection.Range
myRange.End = ActiveDocument.Range.End
With myRange.Find
.ClearFormatting
.MatchWildcards = True
.Text = "^t" & "*" & "^t"
.Execute
myRange.Start = myRange.Start + 1
myRange.End = myRange.End - 1
myRange.Delete
End With
End Sub

Thanks for the responses! But it sure sounds more complicated than it otta
be!!?!! . (And to think that in WordPerfect all I had to do was hit one key
for "Select"!!) Let's assume for the moment - just for the sake of this
discussion - that I don't do programming, that I am a newbie (note newsgroup
name) and I expect the Macro function to simply record keystrokes - then
play them back on command. From the responses, may I conclude I'm outa luck
on the delete-between-tabs routine, and that this is beyond the ability of
Bill's programmers to build into this product?

(In fact, I would use variations of the delete-between-tabs routine often,
but each time it would use different from-to points. And I sure don't want
to have to write a program for every macro I use.)

Bob
 
K

Klaus Linke

Hi Bob,

Hmm... Professional Word users will tell you to avoid tabs altogether.
Then why should Word offer a special built-in function to delete stuff between two tabs?

BTW, you can delete the stuff between the next two tabs with Find/Replace, similar to the macro Greg posted:
Edit > Replace, check "Match wildcards",
Find what: (^t)[!^13]@(^t)
Replace with: \1\2
You can use the macro recorder to create a macro from this without writing a line of code.

If you have a "table" done with tabs, you can also use Alt+LeftMouseButton+Drag to select a "column" and delete it.

Greetings,
Klaus
 
I

InOverMyHead

Thanks loads!! BTW - Its not my choice to use tabs - I'm trying to get rid
of them, actually. They often arise in tab delimited files as you no doubt
know. AND I'm not looking to delete text between tabs - that was just an
example. I want to be able to delete text between "Here" (current curser
location) (and probably result of a Find action) and "There" (text string
xxx for example) (and result of another Find action). I was trying macros
(this operation must repeat hundreds of times in a long document) but it
doesn't seem to be able to "Select" and delete like I would expect. Now
comes Klaus with a most reasonable idea - use find and replace! Duh - I
don't know why I didn't think of this! I tried the example and it didn't
work immediately, but it sure gave me some ideas - so I now just do Find -
^t*^t and replace with [blank]. Seems to work, but am I missing something
by not using all those swear words ((^t)[!^13]@(^t)\1\2)??

Thanks again! (Wowser - there's a lot more power in the search&replace than
I had noticed!!)

Bob



Hi Bob,

Hmm... Professional Word users will tell you to avoid tabs altogether.
Then why should Word offer a special built-in function to delete stuff
between two tabs?

BTW, you can delete the stuff between the next two tabs with Find/Replace,
similar to the macro Greg posted:
Edit > Replace, check "Match wildcards",
Find what: (^t)[!^13]@(^t)
Replace with: \1\2
You can use the macro recorder to create a macro from this without writing a
line of code.

If you have a "table" done with tabs, you can also use
Alt+LeftMouseButton+Drag to select a "column" and delete it.

Greetings,
Klaus
 
G

Greg

Bob,

If it performs like you expect then you are not missing anything :)

All those swear words have a purpose. Klaus assumed, as did I, that
you wanted to keep the tabs and delete the text in between. Klaus also
assumed that you would want to limit the found range to a paragraph and
not delete the text say between the first and only tab in paragraph 1
and the first tab in paragraph 2.

Here is want the curse words are saying:

(1) Find a group consisting of a tab
[!13]@ find every possible following character except a paragraph mark
1 or more times
(2) Find a group consisting of a tab.

The
\1\2 in the Replace with tells word to remove all the muck it found and
replace it with the content of groups 1 and 2.

If you are up to some really foul literature, see fellow MVP Graham
Mayors:
http://www.gmayor.com/replace_using_wildcards.htm
 

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