문제

I'm writing a little script (my first in AutoHotKey) to copy Notepad++-code with Syntax-Highlighting to OneNote. It works by first pressing the "Copy HTML to clipboard" entry, pasting this into a new MSWord-document and cut it from there again, so the result is now in the clipboard.

Directly copying the HTML to OneNote doesn't work, because linebreaks are ignored there (Problem of Microsoft and not notepad++. Look here: http://tech.lanesnotes.com/2012/05/pasting-code-to-onenote-with-formatting.html). But if you copy it to Word and than to OneNote, suddenly it works...

My problem: The code I wrote so far works, but only if Word was running before. If it has to startup first, the script fails and only prints an 'n' in a new Word-document. I'm very thankful to anyone who can help me there.

Edit 2: I Forgot to mention: I use the german version of Notepad++ and Word, so the pressed keys may not be the right ones for you... -> changed the first send to !p (english).

;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.: If you like this functionality, feel free to use this code. If you got a better idea or a proposal how to make it better, please tell me.

도움이 되었습니까?

해결책

You can use COM objects instead of sending keystrokes.

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

다른 팁

I believe that the little loading rectangle (is there a technical name for that?) counts as a "Microsoft Word" window, and it is active. My solution, that worked for me just now (no guarantees!), is to wait on a Word window with the default file name included, like so:

WinWaitActive Document1 - Microsoft Word

Does this work for you? If the German version defaults to something other than "Document1", put that there instead.

Now, where this is going to cause you problems is when you already have a Document1 open when you run this script.

Edit: This is also working for me:

WinWaitActive ahk_class MsoSplash
WinWaitActive Microsoft Word

This second solution may be even more problematic, since it will only work if you have no Word windows open before running winword.

Edit number two: How does the following work for you?

;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

Edit number three: I replaced WinClose with Send !{F4}, which seems to work better.

Edit number four: I moved the WinActivate after the condition so that it applies to both cases, as recommended in a comment.

In Notepad++ you can set it so that you can view all characters.

Click View --> Show Symbol --> Show All Characters.

Then you can see exactly which characters are being used for the linebreak.

Then, copy and paste it to word and back into Notepad and compare the special characters that show up at the end of the line.

Then you can use Notepad++ or AutoHotkey to find and replace the end of line characters accordingly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top