PDA

View Full Version : Word VBA: Selecting a single character



MorleyJ
06-17-2010, 03:14 AM
I'm having a problem which is driving me potty. I have some text in a doc and I'm trying to select potential hyperlinks. The document looks like this:


Testing text
<some hyperlink which this forum has prevented me from posting> some other text

testing

My code successfully identifies the line with the potential hyperlink, then detects where the space is in this line. I then set the selection to the beginning of this line.
After this point anything that I do results in the selection being extended to the full paragraph:
for instance selecting a single character by this method:

Selection.HomeKey Unit:=wdLine
Selection.End = Selection.Start + 1

and by this method:

Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Both select the whole line. There seems to be no way to either select up to the first space, or select even a single character!
Any ideas what's going wrong for me? I've unchecked "Usesmart paragraph selection" in the options and it's made no difference.
Thanks in advance for any help.
J.

Tinbendr
06-17-2010, 10:59 AM
I'm still unsure to your goal, but try this.
Sub SelectHyperLink()
With ActiveDocument.Range
.Collapse wdCollapseStart
.MoveStartUntil "<"
.MoveEndUntil ">"
.MoveEnd wdCharacter
.Select
End With
End Sub

MorleyJ
06-18-2010, 12:35 AM
Thanks so much for the reply.
However - the '<' & '>' characters aren't in the original document - I've used them to denote a substitution of a URL as the BB code here doesn't let me include the literal text, which the BB would try and interpret as a hyperlink and prevent me from posting altogether as my post count <5 ! I've used asterisks below to denote the substitution this time:


Testing text
Original URL: *some URL* some other text

testing

I can autodetect the beginning of the URL, but I can't programmatically select just the URL without automatically selecting any surrounding text.

I'm finding this frustrating because I'm a reasonably experienced programmer, and Word seems to have a will of its own, doing things I've not told it too!

Tinbendr
06-18-2010, 05:02 AM
Turn on the formatting marks and see if you have something odd in the line.

Your code works fine on my machine.

Sorry I couldn't be of more help.

MorleyJ
06-18-2010, 06:46 AM
OK thanks for your help. I've admitted defeat and gone for a parsing option using another language instead outside of Word.

fumei
06-18-2010, 10:44 AM
I am curious.

Original URL: *some URL* some other text


Assuming the text does NOT have the asterik...

What, exactly, is the logic that determines the desired chunk of text is "some URL"?

"There seems to be no way to either select up to the first space, or select even a single character!"

Oh yes there is. Simply describe - exactly - the logic that determines you get:

some URL

rather than:

some U
some URL some
some URL so
some URL some oth

and it should take about 5 seconds to code it.

fumei
06-18-2010, 11:46 AM
Demo attached. Click "Hyperlink Text" on the top toolbar. This uses one method, but there are others. Again, it depends on exactly what logic you use, and what exactly is the situation.

Does the text use the Hyperlink style? How - exactly - are you doing this: "I can autodetect the beginning of the URL"

fumei
06-18-2010, 12:39 PM
Or, how about this? The second "hyperlink" is not actually a hyperlink, as it is plain-text. But it looks like a hyperlink. It is not one, but it MAY be something you are trying to get...hard to say as you are not very specific.

fumei
06-18-2010, 01:20 PM
BTW:

Do you need to use Selection? Generally this is not a good thing.

Hint re: Selection - look at MoveEnd.

And while you state:Selection.HomeKey Unit:=wdLine
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
selects the whole line, I have to question that. It should not. It:

1. moves the Selection to the start of the line.
2. selects the next ONE character...not the whole line.

Waybearee
06-21-2010, 06:30 PM
I love Word Slinger but have advanced to levels of play too high to be fun. How do I start over again at the beginning level? I havent found a way to do this. Please help

MorleyJ
06-22-2010, 02:07 AM
Hi,
Thanks for all the replies. I should probably give you the full story:

What I was actually trying to do was make Word recognise all URLs in a document as hyperlinks (the opposite of pressing Ctrl-Shift and F9). The problem is the document in question contains many thousands of URLs, and to go to each one individually and hit enter, which the usual nice reliable way of doing this was out of the question.
I was trying to write a macro to do this programmatically instead, by looping through all the URLs in the document, selecting them and using: ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range etc.
to add the hyperlink.
Working out something was a URL used very noddy code indeed - looking for "h t t p : / /". This bit worked fine.
I fell at the next hurdle though, trying to get the selection, by selecting up to the next space/line throw. I realise this assumes no white space in the URL itself. This was an assumption I could afford to make in this case.

In spite of what you say fumei, for whatever reason the code:
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
resulted in the whole line being selected. I'm not making it up. I can reliably reproduce it. I didn't think this behaviour was correct so I came to this forum to see if anyone could enlighten me as to why Word was misbehaving for me, and you've all been very helpful.
I'll check out the example word files people have uploaded and report back.
I must confess I've now solved my original problem using VB.net to produce a web page rather than a word doc, parsing out the URLs and making them into hyperlinks by wrapping them in anchor tags.
Curiosity pushes me onwards though, and I need to find out why doing it in Word still doesn't work for me!

Cheers,

Justin

MorleyJ
06-22-2010, 02:27 AM
Re-reading your replies I should make something else clear: The document is entirely plain text. None of the URLs are hyperlinks already. Turning on paragraph/formatting marks reveals nothing odd.

fumei - I've downloaded the attached examples, but the VBA is password protected. I'd quite like to see what you've done in there if that's possible?

fumei
06-23-2010, 09:18 AM
" The document is entirely plain text. "

Then I reiterate my question. HOW do you determine what is a URL? if it isplain text Word has no idea ANY of the text is a "URL".

In my second example I did it by exactly what you mention.

I search for "http", use MoveEnd to move the end to the first space (because URL do NOT have spaces in them).

As for your comment that
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend


resulted in the whole line being selected. I'm not making it up. I can reliably reproduce it.

Sorry, but I can NOT duplicate this. Demo attached. Click "Not For Me" on the top toolbar. The full code is:
Sub NotForMe()
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End Sub
Select whatever you want:

a single character
no character
in the middle
at the end
at the start

It does not matter. No, it does NOT select the line. It does exactly what it is told to do: extend the Selection one character to the right.

Click "Not For Me" three times; the Selection is extend three characters.

If you say you can replicate this, could you please post a file. I would like to see it.

fumei
06-23-2010, 09:21 AM
Waybearee, why are you posting this?

Dave Berry
06-29-2010, 05:09 PM
Never mind - my unit variable had a ZERO in it.

fumei
06-30-2010, 10:43 AM
Then it probably means you have NULL to say.