等待Word启动的问题

时间:2014-01-14 09:29:44

标签: notepad++ autohotkey onenote

我正在编写一个小脚本(我的第一个在AutoHotKey中)来复制Notepad ++ - 使用Syntax-Highlighting到OneNote的代码。它首先按下“将HTML复制到剪贴板”条目,将其粘贴到新的MSWord文档并再次从那里剪切,因此结果现在在剪贴板中。

直接将HTML复制到OneNote不起作用,因为那里忽略了换行符(Microsoft的问题,而不是记事本++。请看这里:http://tech.lanesnotes.com/2012/05/pasting-code-to-onenote-with-formatting.html)。但是如果你将它复制到Word而不是OneNote,那么突然它可以工作......

我的问题:到目前为止我编写的代码有效,但只有在Word运行之前。如果必须先启动,脚本将失败并仅在新的Word文档中打印“n”。我非常感谢那些可以帮助我的人。

编辑2: 我忘了提一下:我使用德语版的Notepad ++和Word,所以按键可能不适合你...... - >将第一个发送更改为!p(英文)。

;This script is used to copy notepad++-Syntax-Higlighting
;to clipboard in a format OneNote can read.
SetTitleMatchMode 2
#c::
IfWinActive, Notepad++
{
    send !p
    send n
    send {Right}
    send c
    send c
    send {Enter}


    Run winword.exe
    WinWaitActive, Microsoft Word
    WinActivate
    send ^v
    send ^a
    send ^x
    WinClose
    send n
}

SetTitleMatchMode 1
return

PS。:如果你喜欢这个功能,请随意使用此代码。如果您有更好的想法或建议如何让它变得更好,请告诉我。

3 个答案:

答案 0 :(得分:2)

您可以使用COM对象而不是发送击键。

Wd := ComObjCreate("Word.Application") ;creates a new MS Word object
Wd.Documents.Add() ;adds a document
rng := Wd.ActiveDocument.Range(0,0) ;creates a range object at the
                                    ; start of the doc
rng.Paste() ;pastes the contents of the clipboard into the range object
rng.Copy() ;copies the range object into the clipboard
Wd.Quit(0) ;quits without saving changes

答案 1 :(得分:1)

我相信小加载矩形(有技术名称吗?)算作" Microsoft Word"窗口,它是活动的。我的解决方案,现在对我有用(不保证!),是等待包含默认文件名的Word窗口,如下所示:

WinWaitActive Document1 - Microsoft Word

这对你有用吗?如果德语版本默认为" Document1"以外的其他内容,请将其放在那里。

现在,如果您在运行此脚本时已经打开了Document1,那么这会导致您遇到问题。

修改:这对我也有用:

WinWaitActive ahk_class MsoSplash
WinWaitActive Microsoft Word

第二种解决方案可能更成问题,因为只有在运行winword之前没有打开Word窗口时它才会起作用。

编辑二号:以下内容如何为您服务?

;This script is used to copy notepad++-Syntax-Higlighting
;to clipboard in a format OneNote can read.
SetTitleMatchMode 2
#c::
IfWinActive, Notepad++
{
    send !p
    send n
    send {Right}
    send c
    send c
    send {Enter}

    IfWinNotExist, ahk_class OpusApp
    {
        Run winword.exe
        WinWait, ahk_class MsoSplash
        WinWait, Microsoft Word
    }
    else
    {
        Run winword.exe
    }
    WinActivate, Microsoft Word
    send ^v
    send ^a
    send ^x
    ; WinClose
    Send !{F4}
    send n
}
SetTitleMatchMode 1
return

编辑第三:我将WinClose替换为Send !{F4},这似乎效果更好。

编辑第四:我在条件之后移动了WinActivate,以便它适用于两种情况,如评论中所建议的那样。

答案 2 :(得分:0)

在Notepad ++中,您可以对其进行设置,以便查看所有字符。

点击查看 - >显示符号 - >显示所有字符。

然后,您可以确切地看到哪些字符用于换行符。

然后,将其复制并粘贴到word并返回到记事本中,并比较在行尾显示的特殊字符。

然后您可以使用Notepad ++或AutoHotkey来相应地查找和替换行尾字符。

相关问题