Jump to content

How to Get Selected text from Word or Excel?

Recommended Posts

I am writing a little script that will be launched via a windows Shortcut HotKey that will grab the selected text from any open window, Verify if it's an IP address by looking for . . . and then Ping that address.

I got it to work from Notepad and WordPad but Office products do not seem to work. Does anybody have a clue on how to read Selected text from Word and Excel, I am hoping I don't have to use COM but i'm starting to think it will come to that.

Below is the current code, I'm using HotKeySet right now just for testing. eventually the _Selected() will be the only code in the script.

EDIT: I figured it out with COM and it was really easy, but it fails the first time through...


Func _Selected()
$w_Title = WinGetTitle("")
$Control = ControlGetFocus($w_Title, "")
if StringInStr($w_Title, "Microsoft Word") Then
    $oWord = ObjGet("", "Word.Application")
    if @error Then
        MsgBox(0, "Word Connection", "Error connecting to the Word COM object")
    $c_Text = $oWord.Selection.text
elseif StringInStr($w_Title, "Microsoft Excel") Then
;Retrieve selected text from Excel
    $oExcel = ObjGet("", "Excel.Application")
    if @error Then
        MsgBox(0, "Excel Connection", "Error connecting to the Excel COM object")
    $c_Text = $oExcel.Selection.text
$c_Text = ControlCommand($w_Title, "", $Control, "GetSelected", "")
if StringInStr($c_Text, ".", 0, 3) Then; There are 3 . so should be an IP address
    $c_Text2 = StringStripWS($c_Text, 8)
;~  MsgBox(0, $w_Title, "Control = " & $Control & @CRLF & "Text = " & $c_Text2)
    $Ping = Ping($c_Text2, 2000)
    If @error Then
        $err = @error
        if $err = 1 then $err = "Host is offline"
        if $err = 2 then $err = "Host is unreachable"
        if $err = 3 then $err = "Bad destination"
        if $err = 4 then $err = "Unknown error"
        MsgBox(0, "Ping Failed", "Ping Returned Error:  " & $err)
        MsgBox(0, "Ping Successfull", "Ping of " & $c_Text2 & " replied in " & $Ping & "ms")
Edited by MikeOsdx
Link to post
Share on other sites


I got it to retrieve from Word and Excel the selected text but it always seems to fail the ObjGet during the first time through.


Launch Excel, select a cell with an IP then hit F4 (my shortcut HotKey).

The ObJGet fails.

Hit F4 to launch the script again and it works fine.

I even tried to put the ObjGet into a loop and if it fails wait 1 second and try again for a max of 5 tries and that didn't work. for some reason the script has to fail, then Exit, and then Launch the script again to connect to the COM object.

WEIRD!!! :lmao:

EDIT: More info, turns out if I launch Excel, change focus to any other window then go back into excel, then launch the script it will work fine...

Any ideas?


Edited by MikeOsdx
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...