ALT+Select in VBA?

E

Ed from AZ

Can the ALT+Select method of selecting a column of text be coded in
VBA? What methods would you use?

Ed
 
G

Graham Mayor

You could select the same specific number of characters from each line, but
you would have to process each line separately - and as 'line' is a vague
concept in Word, you would need to specify what constituted a 'line' in your
column of text. What would be the aim of the exercise?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
E

Ed from AZ

Hi, Graham.
What would be the aim of the exercise?

Our reports are generated in a text-only editor (think NotePad). We
can write in Word, but everything has to go into a text-only frame.
So "tables" are actually columns separated by spaces to align them.
Thankfully, it's a mono-spaced font.

Often, we can pull up an old report, replace one column with the new
data, and have the body of a new report. Were it a table, there would
be no problem. But it's plain text in the report program, so we can't
even create the table in Word and paste it iinto the report frame - it
has no idea what to do with it. Those who know a few things about
Word can use the ALT+Select, but a lot of people don't know it.

So I was thinking about a macro that might be able to help with this.
But it might need to use the ALT+Select in the VBA, and I didn't know
if that existed. BTW, we're using Word 2007.

Ed
 
J

Jay Freedman

Hi, Graham.


Our reports are generated in a text-only editor (think NotePad). We
can write in Word, but everything has to go into a text-only frame.
So "tables" are actually columns separated by spaces to align them.
Thankfully, it's a mono-spaced font.

Often, we can pull up an old report, replace one column with the new
data, and have the body of a new report. Were it a table, there would
be no problem. But it's plain text in the report program, so we can't
even create the table in Word and paste it iinto the report frame - it
has no idea what to do with it. Those who know a few things about
Word can use the ALT+Select, but a lot of people don't know it.

So I was thinking about a macro that might be able to help with this.
But it might need to use the ALT+Select in the VBA, and I didn't know
if that existed. BTW, we're using Word 2007.

Ed

Hi Ed,

The result of the Alt+Select is a noncontiguous selection, similar to
what you get by holding the Ctrl key and selecting random bits of
text. VBA can't handle that
(http://support.microsoft.com/?kbid=288424). A macro can't create such
a selection, and it can't do much with one if it's created in the user
interface.

As Graham suggested, maybe your macro can work out which character
positions on each line are the start and end of the "column" of
interest, and process each line separately. Usually when monospaced
fonts are used, there's a paragraph mark at the end of each line, so
you're really dealing with paragraphs rather than "lines".
 
G

Graham Mayor

I suspect something along the lines of the following might fit the bill,
assuming your text file contains *only* the 'table' - otherwise you would
have to alter the range from the full document to the 'table' part, and
assuming that the gaps are created with multiple spaces and not tabs. If
tabs, change findText:="[ ]{2,}" to findText:="[^t]{1,}"

Dim oDoc As Document
Dim vRow As Variant
Dim oRng As Range
Dim iCol As Long
Dim sCol As String
Set oDoc = ActiveDocument
sCol = ""
iCol = InputBox("Which Column?")
iCol = iCol - 1
oDoc.Range.Find.Execute findText:="[ ]{2,}", _
MatchWildcards:=True, _
ReplaceWith:="¦", _
Replace:=wdReplaceAll
For i = 1 To oDoc.Paragraphs.Count
Set oRng = oDoc.Paragraphs(i).Range
oRng.End = oRng.End - 1
vRow = Split(oRng, "¦")
If i = 1 Then
sCol = vRow(iCol)
Else
sCol = sCol & vbCr & vRow(iCol)
End If
Next i
'sCol contains the chosen column
MsgBox sCol


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
K

Klaus Linke

Hi Ed,

There are two subtly different sorts of that, Alt+Drag and Ctrl+Shift+F8.
The latter allows you to select using Shift plus the arrow keys, once the
mode has been turned on.
You can find out how to turn the mode on with the macro recorder:
Selection.ColumnSelectMode = True

Using that mode, you might be able to automate the selection...
But I agree with the others that it's likely better to parse the content of
the table cells and forget about ColumnSelect.

Greetings,
Klaus
 
K

Klaus Linke

BTW in Word 2003, the ColumnSelectMode is indicated in the status bar below
the document when it's active.
In Word 2007 not, it seems.

Klaus
 
K

Klaus Linke

Klaus Linke said:
BTW in Word 2003, the ColumnSelectMode is indicated in the status bar
below the document when it's active.
In Word 2007 not, it seems.

.... or better, there's an option to display the "Selection mode" when you
right-click on the status bar, but it does not seem to work properly on my
machine.
9 times out of ten, it does not show the column selection mode. In rare
cases when it does, it displays "Auswahl blockieren" (disable selection)
instead of "Blockauswahl" (block selection).

Klaus
 

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