Microsoft Excel Webinar

Results 1 to 16 of 16

Thread: Word VBA: Selecting a single character

  1. #1

    Word VBA: Selecting a single character

    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:
    VB:
    Selection.HomeKey Unit:=wdLine 
    Selection.End = Selection.Start + 1 
    
    
    Formatting tags added by mark007
    and by this method:
    VB:
    Selection.HomeKey Unit:=wdLine 
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
    
    
    Formatting tags added by mark007
    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.

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    983
    Location
    I'm still unsure to your goal, but try this.
    VB:
    Sub SelectHyperLink() 
        With ActiveDocument.Range 
            .Collapse wdCollapseStart 
            .MoveStartUntil "<" 
            .MoveEndUntil ">" 
            .MoveEnd wdCharacter 
            .Select 
        End With 
    End Sub 
    
    
    Formatting tags added by mark007
    +------+
    | David |
    +------+

  3. #3
    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!

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    983
    Location
    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.
    +------+
    | David |
    +------+

  5. #5
    OK thanks for your help. I've admitted defeat and gone for a parsing option using another language instead outside of Word.

  6. #6
    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.

  7. #7
    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"

  8. #8
    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.

  9. #9
    BTW:

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

    Hint re: Selection - look at MoveEnd.

    And while you state:
    VB:
    Selection.HomeKey Unit:=wdLine 
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
    
    
    Formatting tags added by mark007
    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.

  10. #10

    Word VBA Selecting a single character

    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

  11. #11
    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: [FONT='Calibri','sans-serif']ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range etc.[/FONT]
    [FONT='Calibri','sans-serif']to add the hyperlink.[/FONT]
    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:
    VB:
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
    
    
    Formatting tags added by mark007
    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

  12. #12
    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?

  13. #13
    " 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
    VB:
    Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
    
    
    Formatting tags added by mark007
    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:
    VB:
    Sub NotForMe() 
        Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend 
    End Sub 
    
    
    Formatting tags added by mark007
    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.

  14. #14
    Waybearee, why are you posting this?

  15. #15
    Never mind - my unit variable had a ZERO in it.

  16. #16
    Then it probably means you have NULL to say.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •