How to retrieve the last word of a text written in M S Word? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

How to retrieve the last word of a text written in M S Word?

18 Aug 2017, 01:30

Hello dear friends..
I want that when I press f1 key then it should show the last word of the text written in ms word document. I think it can be done by com module but I do not know how to use it in ms word (However I know the use of com component in ms excel.)
Please look at following example-

Suppose this is the text which is written in ms word-

Code: Select all

A special thanks to Jonathan Bennett, whose generosity in releasing AutoIt v2 as 
free software in 1999 served as an inspiration and time-saver for myself and 
many others worldwide. In addition, many of AutoHotkey's enhancements to 
the AutoIt v2 command set, as well as the Window Spy and the old script compiler, 
were adapted directly from the AutoIt v3 source code. So thanks to Jon 
and the other AutoIt authors for those as well.

Finally, AutoHotkey would not be what it is today without these other individuals
Now you can see that in the above text individuals is the last word of the entire text. So I want that when I pressf1 key then it should show the word individuals in the msgbox.

Please help me. Thanks..
I don't normally code as I don't code normally.
Guest

Re: How to retrieve the last word of a text written in M S Word?

18 Aug 2017, 02:22

Can't you just use the clipboard?

Code: Select all

^F1::
ClipSave:=ClipboardAll
Send ^a
Sleep 100
Send ^c
Sleep 100
RegExMatch(clipboard,"U)(\w+)\s*\Z",outputvar)
MsgBox % ">" outputvar1 "<"
Clipboard:=ClipSave
Return
User avatar
Hasso
Posts: 5
Joined: 11 Feb 2014, 00:45

Re: How to retrieve the last word of a text written in M S Word?

18 Aug 2017, 03:32

Code: Select all

#IfWinActive Word
^F1::
clipboard=
send ^{END}^+{LEFT}^c
clipwait 2
;if the last character in the text is a punctuation character: 
if strlen(clipboard)=1
{
send {LEFT}+^{LEFT}^c
clipwait 2
}
MsgBox %clipboard%
return
#IfWinActive
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to retrieve the last word of a text written in M S Word?

19 Aug 2017, 05:07

Guest wrote:Can't you just use the clipboard?

Code: Select all

^F1::
ClipSave:=ClipboardAll
Send ^a
Sleep 100
Send ^c
Sleep 100
RegExMatch(clipboard,"U)(\w+)\s*\Z",outputvar)
MsgBox % ">" outputvar1 "<"
Clipboard:=ClipSave
Return
thanks dear guest for your kind support...

but dear guest i want com module codes for this purpose.....

thanks a lot....
I don't normally code as I don't code normally.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: How to retrieve the last word of a text written in M S Word?  Topic is solved

19 Aug 2017, 07:49

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
OnExit, CleanUp

    Word_Document := "R:\Temp\A special thanks to Jonathan Bennett.doc"
    oWord := ComObjCreate("Word.Application")
    oWord.Documents.Open(Word_Document)

    WORDS := oWord.ActiveDocument.Words
    MsgBox, % WORDS.Item(WORDS.Count - 2).Text "`n"
            . WORDS.Item(WORDS.Count - 1).Text "`n"
            . WORDS.Item(WORDS.Count - 0).Text "`n"

ExitApp


CleanUp:
oWord.Quit()
ExitApp
I hope that helps.
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to retrieve the last word of a text written in M S Word?

21 Aug 2017, 02:59

wolf_II wrote:Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
OnExit, CleanUp

    Word_Document := "R:\Temp\A special thanks to Jonathan Bennett.doc"
    oWord := ComObjCreate("Word.Application")
    oWord.Documents.Open(Word_Document)

    WORDS := oWord.ActiveDocument.Words
    MsgBox, % WORDS.Item(WORDS.Count - 2).Text "`n"
            . WORDS.Item(WORDS.Count - 1).Text "`n"
            . WORDS.Item(WORDS.Count - 0).Text "`n"

ExitApp


CleanUp:
oWord.Quit()
ExitApp
I hope that helps.
thanks a lot dear wolf_II.... :) :)
you are really awesome... :salute: :salute:
you proved your skills this time also.... :bravo: :bravo:
thank you once again... :thumbup: :thumbup:
I don't normally code as I don't code normally.
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to retrieve the last word of a text written in M S Word?

23 Aug 2017, 00:31

wolf_II wrote:Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
OnExit, CleanUp

    Word_Document := "R:\Temp\A special thanks to Jonathan Bennett.doc"
    oWord := ComObjCreate("Word.Application")
    oWord.Documents.Open(Word_Document)

    WORDS := oWord.ActiveDocument.Words
    MsgBox, % WORDS.Item(WORDS.Count - 2).Text "`n"
            . WORDS.Item(WORDS.Count - 1).Text "`n"
            . WORDS.Item(WORDS.Count - 0).Text "`n"

ExitApp


CleanUp:
oWord.Quit()
ExitApp
I hope that helps.
Dear wolf_II.....
i wish to know a couple of things more...
as i want to retrieve all text of the word document then what would be the codes?
as i tried to use these codes but they are not working-

Code: Select all

f1::

    oWord := ComObjActive("Word.Application")
    Words := oWord.ActiveDocument.Words
    MsgBox, % Words.Item(Words.count).Text   ; neither this one is working
    MsgBox, % Words.Item.Words.Text  ; nor this one is working
    MsgBox, % Words.Text  ; nor this one is working
    MsgBox, % oWord.ActiveDocument.Text  ; and nor this one is working
Kindly tell me what would be the correct codes to retrieve all text of a word document??

secondly i wish to know how can i retrieve specific line's specific word in ms word document?
for example, i wish to retrieve third line's fifth word then how can i do that?

please help me..
i am highly thankful to you....
I don't normally code as I don't code normally.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: How to retrieve the last word of a text written in M S Word?

23 Aug 2017, 00:55

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
OnExit, CleanUp

    Word_Document := "R:\Temp\A special thanks to Jonathan Bennett.doc"
    oWord := ComObjCreate("Word.Application")
    oWord.Documents.Open(Word_Document)

    AllWords := oWord.ActiveDocument.Words
    Loop, % AllWords.Count
        Result .= AllWords.Item(A_Index).Text "`n"
    MsgBox, % Result

ExitApp


CleanUp:
oWord.Quit()
ExitApp
I hope that helps.

Second question: I don't know, sorry.
User avatar
Sabestian Caine
Posts: 528
Joined: 12 Apr 2015, 03:53

Re: How to retrieve the last word of a text written in M S Word?

30 Aug 2017, 07:03

wolf_II wrote:Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
OnExit, CleanUp

    Word_Document := "R:\Temp\A special thanks to Jonathan Bennett.doc"
    oWord := ComObjCreate("Word.Application")
    oWord.Documents.Open(Word_Document)

    AllWords := oWord.ActiveDocument.Words
    Loop, % AllWords.Count
        Result .= AllWords.Item(A_Index).Text "`n"
    MsgBox, % Result

ExitApp


CleanUp:
oWord.Quit()
ExitApp
I hope that helps.

Second question: I don't know, sorry.

Thank you so much dear wolf_II :thumbup: :thumbup:
I don't normally code as I don't code normally.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], Joey5, Nerafius, RandomBoy and 152 guests