From Notepad to Word .doc and back Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LeFunk
Posts: 86
Joined: 29 Aug 2016, 03:12

From Notepad to Word .doc and back

16 Oct 2018, 07:49

I'm trying to create a script that:
- sends Notepad text to Word
- applies a macro to the text
- returns the text to Notepad
Opening Word and creating a new document each time was slow, so I created a file test.doc and kept it open for the conversion. But now I can't send any text to it:

Code: Select all

ifwinactive Notepad
{
Escape::
ControlGetText, NotepadText, Edit1, Untitled -

FilePathDOC := "c:\test.doc"
oWord.Documents.Open(FilePathDOC)
oWord := ComObjCreate("Word.Application")
oWord.Visible := 1, oWord.Activate
oWord.Selection.TypeText(NotepadText)
Send ^!i ;shortcut for the macro
Sleep 200
Send ^a^c

ControlSetText, Edit1, %clipboard%, Untitled -
Return
}
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: From Notepad to Word .doc and back

16 Oct 2018, 07:58

Can you explain what you are trying to achieve using Word? What purpose is the macro fulfilling? There might be a way to do everything in AutoHotkey without the use of Word.

Sorry I didn't answer your original request. I just thought I would see if you can provide more information in the case we can make your task easier than your current methods...
eelrod
Posts: 65
Joined: 10 Apr 2018, 11:17

Re: From Notepad to Word .doc and back

16 Oct 2018, 08:07

Does it successfully execute your ^!i macro? Something that might help is to clear out your clipboard before Send ^a^c and stick a ClipWait after it.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: From Notepad to Word .doc and back

16 Oct 2018, 08:39

Also curious as to what the macro is supposed to accomplish. It can't be that sophisticated considering you're sending it back to notepad afterwards.
LeFunk
Posts: 86
Joined: 29 Aug 2016, 03:12

Re: From Notepad to Word .doc and back

18 Oct 2018, 11:05

TheDewd wrote:
16 Oct 2018, 07:58
Can you explain what you are trying to achieve using Word? What purpose is the macro fulfilling? There might be a way to do everything in AutoHotkey without the use of Word.

Sorry I didn't answer your original request. I just thought I would see if you can provide more information in the case we can make your task easier than your current methods...
Sorry for not answering sooner, I had problems with my connection.

The macro applies word's auto-correct to the text. I found that 95% the suggestions were correct and I'm trying to use word's auto-correct in Notepad (and why not, other programs) as well. The Word macro (not really relevant, but just in case someone might need it):


Sub IseParandi()
Dim rng As Range
Dim i As Long

For i = 1 To ActiveDocument.Range.SpellingErrors.Count
Set rng = ActiveDocument.Range.SpellingErrors(i)

If rng.GetSpellingSuggestions.Count <> 0 Then
rng = rng.GetSpellingSuggestions.Item(1).Name & "ZXQ"
End If

Next i

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find
.Text = "ZXQ"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue

End With

Selection.Find.Execute Replace:=wdReplaceAll
End Sub


I made a rudimentary ahk script that sent the text to word, applied the macro and got it back to notepad, but it also created a new word document window each time, so it was very slow and cumbersome. I wondered, if there's a more clever solution to it.
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: From Notepad to Word .doc and back

18 Oct 2018, 14:03

Code: Select all

; Launch Word when script first loads
wordApp := ComObjCreate("Word.Application")
wordApp.Visible := true
wordDoc := wordApp.Documents.Add
return

#IfWinActive, ahk_class Notepad
Esc::
    hNotepadWin := WinExist("A")
    ControlGetText, NotepadText, Edit1, % "ahk_id " hNotepadWin
    try
        wordDoc.StoryRanges(1).Text := NotepadText ; wdMainTextStory = 1
    catch err
    {
        MsgBox, 52, Word, % "Error finding the Word document text. Reload this script?`n`n" err.Message
        IfMsgBox, Yes
            Reload
        return
    }
    while wordDoc.StoryRanges(1).SpellingErrors.Count
    {
        spellingErrorRange := wordDoc.StoryRanges(1).SpellingErrors(1)
        spellingErrorRange.Text := spellingErrorRange.GetSpellingSuggestions.Item(1).Name
    }
    spellingErrorRange := ""
    ControlSetText, Edit1, % StrReplace(wordDoc.StoryRanges(1).Text, "`r", "`r`n"), % "ahk_id " hNotepadWin
return
#If
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: From Notepad to Word .doc and back  Topic is solved

18 Oct 2018, 14:39

An interesting idea to use Word to spell check in other applications.

Here is my take on it.

Code: Select all

OnExit, Word_Shutdown

#F12::
	Clipboard := ""
	Send ^c
	ClipWait
	if !IsObject(wdApp)
		wdApp := ComObjCreate("Word.Application")
	if !IsObject(wdDoc)
		wdDoc := wdApp.Documents.Add
	wdDoc.Content.Text := Clipboard
	NoSuggestion := 0
	while (wdDoc.SpellingErrors.Count - NoSuggestion)
	{
		Error := wdDoc.SpellingErrors(NoSuggestion + 1)
		If Error.GetSpellingSuggestions.Count
			Error.Text := Error.GetSpellingSuggestions.Item(1).Name
		else
			NoSuggestion++
	}
	Result := wdDoc.Content.Text
	StringTrimRight, Clipboard, Result, 1
	Send ^v
return

Word_Shutdown:
	wdApp.Quit(0)
	ExitApp
return
This uses a hidden instance of Word. When Win+F12 is hit the selected text is sent to the clipboard, the clipboard is sent to Word, Word does spelling correction, the new text is retrieved from Word and pasted over the current selection.

The end result is that the spelling is corrected in whatever is selected pretty much anywhere the clipboard is available.

Multi-line selections might be a little funky as different apps might handle new line characters differently than Word.

It also keeps the Word document open in the background after the first call so that it is not having to open Word every time.

I need to play around with it some but this might get added to my list of scripts that I have running at all times.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
LeFunk
Posts: 86
Joined: 29 Aug 2016, 03:12

Re: From Notepad to Word .doc and back

19 Oct 2018, 01:02

Thank you, that's exactly what I tried to do, awesome code!
FanaticGuru wrote:
18 Oct 2018, 14:39
An interesting idea to use Word to spell check in other applications.

Here is my take on it.

Code: Select all

OnExit, Word_Shutdown

#F12::
	Clipboard := ""
	Send ^c
	ClipWait
	if !IsObject(wdApp)
		wdApp := ComObjCreate("Word.Application")
	if !IsObject(wdDoc)
		wdDoc := wdApp.Documents.Add
	wdDoc.Content.Text := Clipboard
	NoSuggestion := 0
	while (wdDoc.SpellingErrors.Count - NoSuggestion)
	{
		Error := wdDoc.SpellingErrors(NoSuggestion + 1)
		If Error.GetSpellingSuggestions.Count
			Error.Text := Error.GetSpellingSuggestions.Item(1).Name
		else
			NoSuggestion++
	}
	Result := wdDoc.Content.Text
	StringTrimRight, Clipboard, Result, 1
	Send ^v
return

Word_Shutdown:
	wdApp.Quit(0)
	ExitApp
return
This uses a hidden instance of Word. When Win+F12 is hit the selected text is sent to the clipboard, the clipboard is sent to Word, Word does spelling correction, the new text is retrieved from Word and pasted over the current selection.

The end result is that the spelling is corrected in whatever is selected pretty much anywhere the clipboard is available.

Multi-line selections might be a little funky as different apps might handle new line characters differently than Word.

It also keeps the Word document open in the background after the first call so that it is not having to open Word every time.

I need to play around with it some but this might get added to my list of scripts that I have running at all times.

FG

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot], mcd, mikeyww, Nerafius and 129 guests