How to check font type of Clipboard?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Posts: 847
Joined: 06 Aug 2017, 08:21
Location: Germany

How to check font type of Clipboard?

22 Jun 2018, 06:26

Hi guys,

I know how it is possible to let AHK find out whether the Clipboard contains text or not.

Code: Select all

if not (DllCall("IsClipboardFormatAvailable", "Uint", 1) OR DllCall("IsClipboardFormatAvailable", "Uint", 13)) ; if there is no text:
  MsgBox, No text was copied!
  ExitApp ; Close script
However, I would like to know if/how it's possible to find out the format of the text.
Would be interesting if AHK could check if it's underlined, italic or bold.
(Font color is not that important, yet interesting to know if that would be possible as well.)

I tried different types of this:

Code: Select all

if Clipboard contains bbold
  MsgBox, text is bold
(Also tried ^bbold^, ^bold)

It would be very helpful because I copy a lot of text from Excel.
Depending on its font format, I could use different functions.

I ignore every thread which contains the P-word :x
(Hint: It rhymes on Shython)
Please do not support cheaters!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602 - OS: Windows 7 Pro 64 Bit - AHK version:
Posts: 47
Joined: 20 Apr 2014, 06:55

Re: How to check font type of Clipboard?

22 Jun 2018, 07:09

1 and 13 are plain text formats, which means no formating, only character codes. There is also 7, CF_OEMTEXT. They are apparently not the formats you need. You can use InsideClipboard to see what formats Excel puts on the clipboard when you copy from it.
Posts: 1993
Joined: 11 Jan 2017, 17:59

Re: How to check font type of Clipboard?

22 Jun 2018, 10:18

tested with word 2016

Code: Select all

	try MsgBox % "Clipboard " (hasBold() ? "contains " : "doesn't contain ") "bold."
	catch e
		MsgBox % "time to handle ur errors: " e.message

hasBold() {
	Formats := {}

	if !DllCall("CountClipboardFormats")
		throw Exception("There are no clipboard formats.", -1)

	if !DllCall("OpenClipboard", "Ptr", 0)
		throw Exception("Cannot open clipboard.", -1)

	if !fmtID := DllCall("EnumClipboardFormats", "UInt", 0)
		throw Exception("Cannot enumerate clipboard format.", -1)

		VarSetCapacity(lpszFormatName, 100)
		DllCall("GetClipboardFormatName", "UInt", fmtID, "Ptr", &lpszFormatName, "Int", 100)
		fmtName := StrGet(&lpszFormatName)
		if (fmtName != "")
			Formats[fmtName] := fmtID
		fmtID := DllCall("EnumClipboardFormats", "UInt", fmtID)

	if !Formats.HasKey("HTML Format")
		throw Exception("Clipboard HTML Format unavailable.", -1)

	if !hMem := DllCall("GetClipboardData", "UInt", Formats["HTML Format"])
		throw Exception("Cannot retrieve clipboard data.", -1)

	if !lptstr := DllCall("GlobalLock", "Ptr", hMem)
		throw Exception("Call to GlobalLock failed.", -1)

	htmlFormat := StrGet(lptstr, "UTF-8")
	fragment := SubStr(htmlFormat, htmlFormat ~= "<!--StartFragment-->")

	DllCall("GlobalUnlock", "Ptr", hMem)

	return !!(fragment ~= "</b>")

Return to “Ask For Help”

Who is online

Users browsing this forum: MannyKSoSo, NackJich and 65 guests