Jump to content
Sign in to follow this  
Suppir

Clipboard to HTML ?

Recommended Posts

Suppir

Hello!

I have some text copyed from MS Word in clipboard. Is the any way to get HTML file from this text? (maybe some UDF exists?)

Share this post


Link to post
Share on other sites
Ascend4nt

Since Word seems to add HTML to its Clipboard output formats, maybe _ClipGetHTML might help out? ;)

Share this post


Link to post
Share on other sites
Suppir

Ascend4nt, wow! That is realy good, thanks!

Share this post


Link to post
Share on other sites
Malkey

This example is working on my XP and using MS Word 2002.

To use this script, either pre-copy, or, highlight the data to be put into the htm file before you press the "F2" key.

#include <Word.au3>

HotKeySet("{ESC}", "Terminate")
HotKeySet("{F2}", "ClipBrdToHtm") ; Press F2

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

While 1
    Sleep(20)
WEnd

; Highlight part or all of a Word Doc file from within MS Word; or
; Have the clipboard pre-copied with nothing highlighted; or
; Use SciTE instead of MS Word.
; Then, press the F2 key when this script is running.
; The text with format, image and/or table that was copied will be put into "Temp.htm" file.
Func ClipBrdToHtm()
    Send("^c") ; Copy highlighted text
    If ClipGet() <> "" Then
        Local $sFileName = @ScriptDir & "\Temp.htm"

        $oWordApp = _WordCreate()
        $oDoc = _WordDocGetCollection($oWordApp, 0)
        _WordDocSaveAs($oDoc, $sFileName, 10) ; Doc file saved in filtered HTML format.

        WinActivate("Temp.htm")
        WinWaitActive("Temp.htm")
        Send("^v") ; Paste
        _WordDocSave($oDoc)
        ;_WordQuit($oWordApp) ; Uncomment for personal preference - use or not?

        ; For demonstrational purposes only.
        ShellExecute($sFileName) ; Will open file with application associated with file extension (.htm)
        Sleep(2000)
        FileDelete($sFileName); Clean up file for this example.
    EndIf
EndFunc ;==>ClipBrbToHtm

Func Terminate()
    Exit 0
EndFunc ;==>Terminate

Share this post


Link to post
Share on other sites
Suppir

This example is working on my XP and using MS Word 2002.

To use this script, either pre-copy, or, highlight the data to be put into the htm file before you press the "F2" key.

#include <Word.au3>

HotKeySet("{ESC}", "Terminate")
HotKeySet("{F2}", "ClipBrdToHtm") ; Press F2

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

While 1
    Sleep(20)
WEnd

; Highlight part or all of a Word Doc file from within MS Word; or
; Have the clipboard pre-copied with nothing highlighted; or
; Use SciTE instead of MS Word.
; Then, press the F2 key when this script is running.
; The text with format, image and/or table that was copied will be put into "Temp.htm" file.
Func ClipBrdToHtm()
    Send("^c") ; Copy highlighted text
    If ClipGet() <> "" Then
        Local $sFileName = @ScriptDir & "\Temp.htm"

        $oWordApp = _WordCreate()
        $oDoc = _WordDocGetCollection($oWordApp, 0)
        _WordDocSaveAs($oDoc, $sFileName, 10) ; Doc file saved in filtered HTML format.

        WinActivate("Temp.htm")
        WinWaitActive("Temp.htm")
        Send("^v") ; Paste
        _WordDocSave($oDoc)
        ;_WordQuit($oWordApp) ; Uncomment for personal preference - use or not?

        ; For demonstrational purposes only.
        ShellExecute($sFileName) ; Will open file with application associated with file extension (.htm)
        Sleep(2000)
        FileDelete($sFileName); Clean up file for this example.
    EndIf
EndFunc ;==>ClipBrbToHtm

Func Terminate()
    Exit 0
EndFunc ;==>Terminate

That is nice too!

Share this post


Link to post
Share on other sites
Suppir

Is it possible to do some replaces in clipboard before generating HTML?

Replaces with regular expressions.

Share this post


Link to post
Share on other sites
Suppir

Something like this.

ClipPut(StringRegExpReplace(ClipGet(), "[^[:lower:]]", ""))
; or
ClipPut(StringReplace(ClipGet(), "this", "THIS"))

no-no-no. Clipboard consists of data, that we copyed from MS Word. It is not raw(plain) text, but formatted text with tables, styles, etc.

And I have to do replaces with regular expressions in this text (because MS Word does not support RCRE).

If we just do what you've written, all tables and styles will dissapper.

Edited by Suppir

Share this post


Link to post
Share on other sites
Ascend4nt

If _ClipGetHtml() returns an array, you can do all the StringRegExpReplace/StringReplace's you want.

For example:

$aHTML=_ClipGetHTML()
If @error Then Exit
$sHTML=StringRegExpReplace($aHTML[0],"replace(this)","that")
FileWrite(@DesktopDir&"\test.html",$sHTML)

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.