Sign in to follow this  
Followers 0
BigDaddyO

How to Get Selected text from Word or Excel?

4 posts in this topic

#1 ·  Posted (edited)

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

_Selected()

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")
        Exit
    EndIf
    $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")
        Exit
    EndIf
    $c_Text = $oExcel.Selection.text
Else
$c_Text = ControlCommand($w_Title, "", $Control, "GetSelected", "")
EndIf
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)
    Else
        MsgBox(0, "Ping Successfull", "Ping of " & $c_Text2 & " replied in " & $Ping & "ms")
    EndIf
EndIf
EndFunc
Edited by MikeOsdx

Share this post


Link to post
Share on other sites



I know you could do it from a CSV file. Maybe that will work instead of a XLS.


[quote] Gilbertson's Law: Nothing is foolproof to a sufficiently talented fool.Sandro Alvares: Flaxcrack is please not noob! i can report you is stop stupid. The Post[/quote]I made this: FWD & MD5PWD()

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

OK,

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.

Example.

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?

Mike

Edited by MikeOsdx

Share this post


Link to post
Share on other sites

Have you tried sending just CTRL+C? .. should work on all apps wherever you can select text

HTH

:lmao:

Share this post


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
Sign in to follow this  
Followers 0