Sets CF_HTML data in clipboard which can then be pasted with Ctrl+v
Sample: SetClipboardHTML("<div><b>Welcome to AutoHotkey</b></div>",, "Welcome to AutoHotkey")
Parameters:
- HtmlBody : Body of the HTML, without <body></body> tags. Paste will work only in html capable input.
- HtmlHead : Optional Head content of the HTML, without <head></head> tags.
You may use this for limited CSS styling with supported editors like MS Word.
For browser input (Gmail/YahooMail etc.), you may use limited inline styling in Body. - AltText : Optional alternative (unicode) text for html incapable editors like Notepad, PSPad etc.
Example: These following snippets are a modified version of intro example given in docs under HotStrings V1 / Hotstrings V2.
Code: Select all
#Requires AutoHotkey v1.1.33+
#SingleInstance Force
Hotstring(":*X:]d", "DatePaste")
Return
DatePaste:
FormatTime, DTH,, '<i>'dddd'</i>', '<b>'dd-MMM-yyyy'</b>' h:mm tt
FormatTime, DTT,, dddd, dd-MMM-yyyy h:mm tt
SetClipboardHTML(DTH,, DTT)
SendInput ^v
Return
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
Hotstring(":*X:]d", DatePaste)
DatePaste(*)
{
Local DTH := FormatTime(,"'<i>'dddd'</i>', '<b>'dd-MMM-yyyy'</b>' h:mm tt")
, DTT := FormatTime(,"dddd, dd-MMM-yyyy h:mm tt")
SetClipboardHTML(DTH,, DTT)
SendInput("^v")
}
The function
Code: Select all
SetClipboardHTML(HtmlBody, HtmlHead := "", AltText := "") ; SetClipboardHTML() v0.72 by SKAN for ahk,ah2 on D393/D66T
{ ; @ autohotkey.com/r?t=80706
Static CF_UNICODETEXT := 13
, CF_HTML := DllCall("User32\RegisterClipboardFormat", "str","HTML Format")
, Fix := SubStr(A_AhkVersion, 1, 1) = 2 ? [,,1] : [,1] ; StrReplace() parameter fix for AHK V2 vs V1
Local pMem := 0, Res1 := 1, Bytes := 0, LF := "`n"
, hMem := 0, Res2 := 1, Html := ""
If Not DllCall("User32\OpenClipboard", "ptr",A_ScriptHwnd)
Return 0
Else DllCall("User32\EmptyClipboard")
If HtmlBody != ""
{
Html := "Version:0.9" LF "StartHTML:000000000" LF "EndHTML:000000000" LF "StartFragment:000000000"
. LF "EndFragment:000000000" LF "<!DOCTYPE>" LF "<html>" LF "<head>" LF HtmlHead LF "</head>" LF "<body>"
. LF "<!--StartFragment -->" HtmlBody "<!--EndFragment -->" LF "</body>" LF "</html>"
, Html := StrReplace(Html, "StartHTML:000000000", Format("StartHTML:{:09}", InStr(Html, "<html>")) , Fix*)
, Html := StrReplace(Html, "EndHTML:000000000", Format("EndHTML:{:09}", InStr(Html, "</html>")) , Fix*)
, Html := StrReplace(Html, "StartFragment:000000000", Format("StartFragment:{:09}", InStr(Html, "<!--StartFrag")) , Fix*)
, Html := StrReplace(Html, "EndFragment:000000000", Format("EndFragment:{:09}", InStr(Html, "<!--EndFragme"),,0), Fix*)
, Bytes := StrPut(Html, "utf-8")
, hMem := DllCall("Kernel32\GlobalAlloc", "int",0x42, "ptr",Bytes+4, "ptr")
, pMem := DllCall("Kernel32\GlobalLock", "ptr",hMem, "ptr")
, StrPut(Html, pMem, Bytes, "utf-8")
, DllCall("Kernel32\GlobalUnlock", "ptr",hMem)
, Res1 := DllCall("User32\SetClipboardData", "int",CF_HTML, "ptr",hMem)
}
If AltText != ""
{
Bytes := StrPut(AltText, "utf-16")
, hMem := DllCall("Kernel32\GlobalAlloc", "int",0x42, "ptr",(Bytes*2)+8, "ptr")
, pMem := DllCall("Kernel32\GlobalLock", "ptr",hMem, "ptr")
, StrPut(AltText, pMem, Bytes, "utf-16")
, DllCall("Kernel32\GlobalUnlock", "ptr",hMem)
, Res2 := DllCall("User32\SetClipboardData", "int",CF_UNICODETEXT, "ptr",hMem)
}
DllCall("User32\CloseClipboard")
Return !! (Res1 And Res2)
}