Column Select Mode

M

Mike Petrie

I'm trying to get a column select mode type effect. I can use CTRL+SHIFT+F8
and the cursor keys or mouse to get a selection and copy that to the
clipboard, for instance. However, if I create a macro to turn the mode on
and then leave the user to mouse around it doesn't work - the mode is
deselected (or never gets selected?) I used the code from the Macro Recorder
as a basis - it looks like this:

Sub ColSelMode()
'
If Selection.ColumnSelectMode = False Then
Selection.ColumnSelectMode = True
Else
Selection.ColumnSelectMode = False
Selection.End = Selection.Start
End If

End Sub

If I have some cursor navigation commands following the End If, it's fine!
Any ideas?


The second and related problem is that I use a couple of bookmarks to
delimit a range in the document. This is fine for a normal selection, but
I'd like to be able to use the same two markers to delimit the NE/SW corners
of a column selection block. Unfortunately the colomn select mode doesn't
support ranges (or should that be the other way around), and also,
navigation seems to be limited to cursor up/down a line and L/R a character,
you don't seem to be able to jump to a point. I'd like to be able to go to
the first bookmark, turn on column mode, jump to the second one, and then
run any of a series of other commands based on what the user has typed -
copy, paste, move, delete, write to file, etc. Does anyone know of a work
around for such a scenario.


Thanks!
Mike
 
D

Doug Robbins - Word MVP

Hi Mike,

Do it this way:

Dim myrange As Range, endline As Long, lines As Long, chars As Long

ActiveDocument.Bookmarks("there").Range.Select
endline = Selection.Information(wdFirstCharacterLineNumber)
Set myrange = Selection.Bookmarks("\line").Range
myrange.End = ActiveDocument.Bookmarks("there").Range.End
chars = myrange.Characters.Count
ActiveDocument.Bookmarks("here").Range.Select
lines = endline - Selection.Information(wdFirstCharacterLineNumber)
With Selection
.Collapse Direction:=wdCollapseStart
.ColumnSelectMode = True
.MoveDown Unit:=wdLine, Count:=lines, Extend:=wdExtend
.MoveRight Unit:=wdCharacter, Count:=chars, Extend:=wdExtend
.Copy
.ColumnSelectMode = False
End With

Note that the .MoveRight must come after the .MoveDown as the space
occuppied on the screen by the number of characters on each line can be
different.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
K

Klaus Linke

Hi Mike,

I ran into that bug too.

Running a macro always turns off the column select mode.
And if you turn on the column select mode in a macro, it gets turned off
again when the macro finishes.

There doesn't seem to be anything you can do about it :-(

If you have selected some block in your macro, and your macro has stopped,
the user will have to use Ctrl+Shift+F8 again to turn the mode on again,
and to make changes to the selected block.

If you have some start and end points (rng.Start, rng.End, given by the
current Selection in the macro below) and want to select the "block", you
could use something like
Dim rng As Range
Dim iStartLine As Long
Dim iEndLine As Long
Dim iStartCol As Long
Dim iEndCol As Long
Set rng = Selection.Range.Duplicate
iStartLine = rng.Information(wdFirstCharacterLineNumber)
iStartCol = rng.Information(wdFirstCharacterColumnNumber)
Selection.Collapse (wdCollapseEnd)
iEndLine = Selection.Information(wdFirstCharacterLineNumber)
iEndCol = Selection.Information(wdFirstCharacterColumnNumber)
rng.Select
Selection.Collapse (wdCollapseStart)
Selection.ColumnSelectMode = True
Selection.MoveLeft Unit:=wdCharacter, Count:=iStartCol
Selection.MoveDown Unit:=wdLine, Count:=(iEndLine - iStartLine)
Selection.MoveRight Unit:=wdCharacter, Count:=iEndCol - 1

As said above, the user would have to click Ctrl+Shift+F8 again to change
the block.

Regards,
Klaus
 
M

Mike Petrie

Thanks Doug & Klaus,

Two variations to play with. I'll have a go at getting this working over the
weekend - back again after that.

Thanks
Mike
 
M

Mike Petrie

Doug,

I have used your code to get most of what I was looking for. I didn't try
Klaus' version.

I have a couple more questions though:

* On some occasions I'll want to copy the marked block to another location,
and some to move it. Is there any way to do this without using the clipboard
as you can with ranges?

* If I move the block, I'd like to move the original bookmarks that
delimited it, so that it's still delimited by them. At the moment they both
get moved (cut and paste) but both end up at the start of the block after
pasting it back into the document.

What I'm after is, bookmarks in [brackets]...

original text
[start]blah blah blah
blah[end] blah blah
blah blah blah

[dest] <-- current cursor position, I set a bookmerk here to record it

copy -->

[start]blah blah blah
blah[end] blah blah
blah blah blah

[dest]blah
blah

move -->

blah blah
blah blah
blah blah blah

[dest][start]blah
blah[end]


I've tried various things I found in the help file including collapsing,
pulling my hair out (only the first was detailed!) - any ideas?


Thanks!
Mike
 
D

Doug Robbins - Word MVP

Hi Mike,

If you have a bookmark defining the location to where you want to move the
text, the following will effectively do that:

Dim myrange As Range, endline As Long, lines As Long, chars As Long

ActiveDocument.Bookmarks("there").Range.Select
endline = Selection.Information(wdFirstCharacterLineNumber)
Set myrange = Selection.Bookmarks("\line").Range
myrange.End = ActiveDocument.Bookmarks("there").Range.End
chars = myrange.Characters.Count
ActiveDocument.Bookmarks("here").Range.Select
lines = endline - Selection.Information(wdFirstCharacterLineNumber)
With Selection
.Collapse Direction:=wdCollapseStart
.ColumnSelectMode = True
.MoveDown Unit:=wdLine, Count:=lines, Extend:=wdExtend
.MoveRight Unit:=wdCharacter, Count:=chars, Extend:=wdExtend
'Set myrange = .Range
.Copy
.Delete
.ColumnSelectMode = False
End With

ActiveDocument.Bookmarks("nowhere").Range.Paste
ActiveDocument.Bookmarks("nowhere").Select
Selection.MoveDown wdLine, lines
Selection.MoveRight wdCharacter, chars
ActiveDocument.Bookmarks.Add "there", Selection.Range
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Mike Petrie said:
Doug,

I have used your code to get most of what I was looking for. I didn't try
Klaus' version.

I have a couple more questions though:

* On some occasions I'll want to copy the marked block to another location,
and some to move it. Is there any way to do this without using the clipboard
as you can with ranges?

* If I move the block, I'd like to move the original bookmarks that
delimited it, so that it's still delimited by them. At the moment they both
get moved (cut and paste) but both end up at the start of the block after
pasting it back into the document.

What I'm after is, bookmarks in [brackets]...

original text
[start]blah blah blah
blah[end] blah blah
blah blah blah

[dest] <-- current cursor position, I set a bookmerk here to record it

copy -->

[start]blah blah blah
blah[end] blah blah
blah blah blah

[dest]blah
blah

move -->

blah blah
blah blah
blah blah blah

[dest][start]blah
blah[end]


I've tried various things I found in the help file including collapsing,
pulling my hair out (only the first was detailed!) - any ideas?


Thanks!
Mike



Doug Robbins - Word MVP said:
Hi Mike,

Do it this way:

Dim myrange As Range, endline As Long, lines As Long, chars As Long

ActiveDocument.Bookmarks("there").Range.Select
endline = Selection.Information(wdFirstCharacterLineNumber)
Set myrange = Selection.Bookmarks("\line").Range
myrange.End = ActiveDocument.Bookmarks("there").Range.End
chars = myrange.Characters.Count
ActiveDocument.Bookmarks("here").Range.Select
lines = endline - Selection.Information(wdFirstCharacterLineNumber)
With Selection
.Collapse Direction:=wdCollapseStart
.ColumnSelectMode = True
.MoveDown Unit:=wdLine, Count:=lines, Extend:=wdExtend
.MoveRight Unit:=wdCharacter, Count:=chars, Extend:=wdExtend
.Copy
.ColumnSelectMode = False
End With

Note that the .MoveRight must come after the .MoveDown as the space
occuppied on the screen by the number of characters on each line can be
different.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
go
 
M

Mike Petrie

Hi Doug,

Thanks again - and swift!

With some fiddling I now get my bookmarks where I want with the move.
However, I've noticed another problem. If I have three coulmns, for example:

Column 1 Column 2 Column 3
Column 1 [Column 2 Column 3
Column 1 Column 2 Column 3
Column 1 Column 2 Column 3
Column 1 Column 2 ] Column 3

It's all OK if I mark items in the first column, but if I block the second
coulmn as shown by [ and ] the selected block seems to be more like that
blocked below:


Column 1 Column 2 Column 3
Column 1 [Column 2 Column 3
Column 1 Column 2 Column 3
Column 1 Column 2 Column 3
Column 1 Column 2 Column 3]



....so I get column 2 and column 3 copied or moved.



Following your code, with changed bookmark names to fit the rest of what I'm
doing, I have a series of case statements for copy, move, cut, delete, save
to file, print (the last two haven't been done yet- a warning).



Copy just does:

Selection.Copy
rngNewPos.FormattedText.Paste



Move does a bit more:

Selection.Copy 'from your code
Selection.Delete

'Remove old start marker or it will stay at the old block
position
ActiveDocument.Bookmarks("there").Delete
Selection.ColumnSelectMode = False
ActiveDocument.Bookmarks("NewPos").Range.Paste
' Reselect block so end marker can be put at the new end
ActiveDocument.Bookmarks("NewPos").Select
Selection.MoveDown wdLine, lines
Selection.MoveRight wdCharacter, chars
' Set the new end marker position

ActiveDocument.Bookmarks.Add "here", Selection.Range

' Go back to the start of the block and reset the start
marker

' to the new start position

GoToMarker "CurPos" ' basically go to bookmark xyz
ActiveDocument.Bookmarks.Add "there"

Is my problem due to the ("\line")? I don't really understand what this is
doing.



Sorry if this is a dumb question.





Regards

Mike





Doug Robbins - Word MVP said:
Hi Mike,

If you have a bookmark defining the location to where you want to move the
text, the following will effectively do that:

Dim myrange As Range, endline As Long, lines As Long, chars As Long

ActiveDocument.Bookmarks("there").Range.Select
endline = Selection.Information(wdFirstCharacterLineNumber)
Set myrange = Selection.Bookmarks("\line").Range
myrange.End = ActiveDocument.Bookmarks("there").Range.End
chars = myrange.Characters.Count
ActiveDocument.Bookmarks("here").Range.Select
lines = endline - Selection.Information(wdFirstCharacterLineNumber)
With Selection
.Collapse Direction:=wdCollapseStart
.ColumnSelectMode = True
.MoveDown Unit:=wdLine, Count:=lines, Extend:=wdExtend
.MoveRight Unit:=wdCharacter, Count:=chars, Extend:=wdExtend
'Set myrange = .Range
.Copy
.Delete
.ColumnSelectMode = False
End With

ActiveDocument.Bookmarks("nowhere").Range.Paste
ActiveDocument.Bookmarks("nowhere").Select
Selection.MoveDown wdLine, lines
Selection.MoveRight wdCharacter, chars
ActiveDocument.Bookmarks.Add "there", Selection.Range
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
Mike Petrie said:
Doug,

I have used your code to get most of what I was looking for. I didn't try
Klaus' version.

I have a couple more questions though:

* On some occasions I'll want to copy the marked block to another location,
and some to move it. Is there any way to do this without using the clipboard
as you can with ranges?

* If I move the block, I'd like to move the original bookmarks that
delimited it, so that it's still delimited by them. At the moment they both
get moved (cut and paste) but both end up at the start of the block after
pasting it back into the document.

What I'm after is, bookmarks in [brackets]...

original text
[start]blah blah blah
blah[end] blah blah
blah blah blah

[dest] <-- current cursor position, I set a bookmerk here to record it

copy -->

[start]blah blah blah
blah[end] blah blah
blah blah blah

[dest]blah
blah

move -->

blah blah
blah blah
blah blah blah

[dest][start]blah
blah[end]


I've tried various things I found in the help file including collapsing,
pulling my hair out (only the first was detailed!) - any ideas?


Thanks!
Mike



Doug Robbins - Word MVP said:
Hi Mike,

Do it this way:

Dim myrange As Range, endline As Long, lines As Long, chars As Long

ActiveDocument.Bookmarks("there").Range.Select
endline = Selection.Information(wdFirstCharacterLineNumber)
Set myrange = Selection.Bookmarks("\line").Range
myrange.End = ActiveDocument.Bookmarks("there").Range.End
chars = myrange.Characters.Count
ActiveDocument.Bookmarks("here").Range.Select
lines = endline - Selection.Information(wdFirstCharacterLineNumber)
With Selection
.Collapse Direction:=wdCollapseStart
.ColumnSelectMode = True
.MoveDown Unit:=wdLine, Count:=lines, Extend:=wdExtend
.MoveRight Unit:=wdCharacter, Count:=chars, Extend:=wdExtend
.Copy
.ColumnSelectMode = False
End With

Note that the .MoveRight must come after the .MoveDown as the space
occuppied on the screen by the number of characters on each line can be
different.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
I'm trying to get a column select mode type effect. I can use
CTRL+SHIFT+F8
and the cursor keys or mouse to get a selection and copy that to the
clipboard, for instance. However, if I create a macro to turn the
mode
on
and then leave the user to mouse around it doesn't work - the mode is
deselected (or never gets selected?) I used the code from the Macro
Recorder
as a basis - it looks like this:

Sub ColSelMode()
'
If Selection.ColumnSelectMode = False Then
Selection.ColumnSelectMode = True
Else
Selection.ColumnSelectMode = False
Selection.End = Selection.Start
End If

End Sub

If I have some cursor navigation commands following the End If, it's fine!
Any ideas?


The second and related problem is that I use a couple of bookmarks to
delimit a range in the document. This is fine for a normal
selection,
but
I'd like to be able to use the same two markers to delimit the NE/SW
corners
of a column selection block. Unfortunately the colomn select mode doesn't
support ranges (or should that be the other way around), and also,
navigation seems to be limited to cursor up/down a line and L/R a
character,
you don't seem to be able to jump to a point. I'd like to be able to
go
to
the first bookmark, turn on column mode, jump to the second one, and then
run any of a series of other commands based on what the user has typed -
copy, paste, move, delete, write to file, etc. Does anyone know of a work
around for such a scenario.


Thanks!
Mike
 

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