Modifying Clipboard Binary Data of Formatted Text

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
blue_fields
Posts: 20
Joined: 16 Feb 2023, 11:07

Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 02:47

Hey everyone,

I have some word documents which are templates of different letters that we send out in my office. I have copied them and stored the binary data into a file, which includes the text formatting and images. This is basically the current code:

Code: Select all

; copy letter and store the clipboard buffer data into a file
Send "^c"
Try FileDelete "Company Letter.clip"
FileAppend ClipboardAll(), "Company Letter.clip"

;put file into the clipboard and paste it
ClipData := FileRead("Company Letter.clip", "RAW")  ; In this case, FileRead returns a Buffer.
A_Clipboard := ClipboardAll(ClipData)
Send "^v"
These letters have names and IDs that need to be changed. I would like to include a function in my ahk script that replaces the [name of recipient] in the letter based on a string variable when I paste the template of the letter. However, this is a buffer object, which I guess is binary data? How do I replace text in the buffer object? Is this somehow possible without losing my text formatting and images that are included in the buffer object? Thank you!
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 06:23

Hi, blue_fields

The subject is very complex. ClipboardAll saves all possible detected formats. In the case of Word it saves it in plain format, RTF and Html. The file saved in your pseudocode has those three formats.

The problem is pasting such content. In the same session (without closing the session or restarting Windows), the system saves the clipboard and pastes it in the most appropriate format for the destination program. But if the system is rebooted, you are only left with the contents of the "Company Letter.clip" file and trying to paste it with Ctrl+v will paste gibberish from that file in all three formats.

The following script just saves the .html format and pastes it in that format (by mistake I thought it was in the help for version 1.1, the script is for that version, not 2). This has only solved the problems described above. Possible solution to what you request. If the names and identifiers are recorded in the html format with some kind of tagging (Html ​​language), you would have to use RegExReplace to change the text contained between the two tags.

Code: Select all

#Include WinClipAPI.ahk
#Include WinClip.ahk

FileDelete, TempWord.html

F2::
Send ^c

ClipboardGet_HTML( Data )

 TrayTip
 TrayTip, Clipboard Message, %Clipboard%,, 0x1, 0x10, 0x20
 SetTimer, HideTrayTip, -1500

DataReplace := StrReplace(Data, "<h1", "<p")
DataReplace1 := StrReplace(DataReplace, "</h1", "</p")
FileAppend, %DataReplace1%, Temp.txt

Loop, read, Temp.txt, CP65001
{
   If A_Index >6
   FileAppend, %A_LoopReadLine%`n, TempWord.html, CP65001
}

FileDelete, Temp.txt
Return

F3::
html:= FileOpen("TempWord.html", "r").read()
WinClip.SetHTML(html)
WinClip.Paste()
Return

HideTrayTip:
TrayTip
Return

ClipboardGet_HTML( byref Data ) ;www.autohotkey.com/forum/viewtopic.php?p=392624#392624
 {
 If CBID := DllCall( "RegisterClipboardFormat", Str,"HTML Format", UInt )
  If DllCall( "IsClipboardFormatAvailable", UInt,CBID ) <> 0
   If DllCall( "OpenClipboard", UInt,0 ) <> 0
    If hData := DllCall( "GetClipboardData", UInt,CBID, UInt )
       DataL := DllCall( "GlobalSize", UInt,hData, UInt )
     , pData := DllCall( "GlobalLock", UInt,hData, UInt )
     , VarSetCapacity( data, dataL * ( A_IsUnicode ? 2 : 1 ) ), StrGet := "StrGet"
     , A_IsUnicode ? Data := %StrGet%( pData, dataL, 0 )
                   : DllCall( "lstrcpyn", Str,Data, UInt,pData, UInt,DataL )
     , DllCall( "GlobalUnlock", UInt,hData )
 DllCall( "CloseClipboard" )
Return dataL ? dataL : 0
}
I have not found the original site to download the included libraries, but they can be downloaded from here: https://github.com/lintalist/lintalist/tree/master/include
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 06:26

I read over your post a few times trying to understand not what, but why you are approaching the task the way you are. From what you wrote, it seems like you are traveling east from New York to get to Los Angeles and going around the world to get there.
blue_fields wrote: I would like to include a function in my ahk script that replaces the [name of recipient] in the letter based on a string variable when I paste the template of the letter.
You have Word documents that are templates and you are copying the template and putting it into a separate binary file. And then you what, read the binary data in AHK and past it back into a blank Word document? Why? That's an extra step - just load the template document itself.

Where is the data that will be in your string variable coming from? Are you typing it in for every letter? If so, why not just type it directly into the document instead of an AHK input box or whatever? Either way, you are still typing it.

If the data is coming from a table (Excel or Access or CSV file), you should probably spend some time learning Word's extensive Mail Merge capabilities. Once your template document is set up properly with data fields where you want them, you link the template with your data source and simply select the records you want to print. If you have multiple records and you want to save individual copies of the source document with the data fields filled in, look up "splitting mail merge files into separate documents".

Don't get me wrong - I automate repetitive things with AHK all the time, but not when that automation would add more steps than performing the same task with the application's built-in capabilities. There's an old adage, "If the only tool you know how to use is a hammer, every job looks like a nail." Based solely on what you posted, I'd say it was time to expand your toolbox.

Russ
blue_fields
Posts: 20
Joined: 16 Feb 2023, 11:07

Re: Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 10:37

@wetware05 thank you! Your solution may work, I'm gonna do some tests today or tomorrow. However, so far I am able to restart my computer and the "CompanyLetter.Clip" file does successfully paste properly when I load it into the clip board, perhaps because it's copied from a fixed word doc on my system?

@RussF My apologies, in trying to keep my post simple I think I just made it more confusing in some ways. I am aware of mail merge in word and it would solve what I'm technically trying to do, but the issue is that it's too slow. I will explain what I'm trying to do properly this time:

Basically, I work in a student records department that has a lot of outdated manual processes. For example, if faculty wants to change a grade of a student, or a student wants to change their program, in both these cases they are required to fill out a form, get it signed, and then email it to my department. So just to keep it simple with these two examples, I may be responding with either 1. "Grade Change Processed Reply Letter Template" or 2. "Program Change Processed Reply Letter Template" depending on what is requested of me via email. I might do 50 of these in a day, which means I have to open up the word document of the 1. or 2. template letter, copy its contents, paste it into my email reply box, and then manually enter the student's name in the name field of the template, same with ID, course name, etc.

Now in reality I have ~20 different processes like this. So it's pretty slow to individually open up a separate word doc for each, manually copy and paste it into the email. It would be much faster to simply have a letter template saved as a clipboard object, and I simply have a hotkey or custom keyboard shortcut to paste the template I need instantly into the email response. No opening up word, no highlighting, etc.

So I can already do this, but I want to take it a step further. When I get an email for a grade change for example, I need to type the student id, course name, term, etc. into our system, so my idea was to have a GUI with some edit fields, in which I would type in that info. When I get to emailing back with the reply, the AHK script would look at what info is in my GUI edit fields, and then sub in the appropriate info for the current template I am pasting (student ID, name, course, etc). Hopefully it's clearer now what I'm trying to achieve.
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 17:29

blue_fields wrote:
05 May 2023, 10:37
@wetware05 thank you! Your solution may work, I'm gonna do some tests today or tomorrow. However, so far I am able to restart my computer and the "CompanyLetter.Clip" file does successfully paste properly when I load it into the clip board, perhaps because it's copied from a fixed word doc on my system?

@RussF My apologies, in trying to keep my post simple I think I just made it more confusing in some ways. I am aware of mail merge in word and it would solve what I'm technically trying to do, but the issue is that it's too slow. I will explain what I'm trying to do properly this time:

Basically, I work in a student records department that has a lot of outdated manual processes. For example, if faculty wants to change a grade of a student, or a student wants to change their program, in both these cases they are required to fill out a form, get it signed, and then email it to my department. So just to keep it simple with these two examples, I may be responding with either 1. "Grade Change Processed Reply Letter Template" or 2. "Program Change Processed Reply Letter Template" depending on what is requested of me via email. I might do 50 of these in a day, which means I have to open up the word document of the 1. or 2. template letter, copy its contents, paste it into my email reply box, and then manually enter the student's name in the name field of the template, same with ID, course name, etc.

Now in reality I have ~20 different processes like this. So it's pretty slow to individually open up a separate word doc for each, manually copy and paste it into the email. It would be much faster to simply have a letter template saved as a clipboard object, and I simply have a hotkey or custom keyboard shortcut to paste the template I need instantly into the email response. No opening up word, no highlighting, etc.

So I can already do this, but I want to take it a step further. When I get an email for a grade change for example, I need to type the student id, course name, term, etc. into our system, so my idea was to have a GUI with some edit fields, in which I would type in that info. When I get to emailing back with the reply, the AHK script would look at what info is in my GUI edit fields, and then sub in the appropriate info for the current template I am pasting (student ID, name, course, etc). Hopefully it's clearer now what I'm trying to achieve.

What you want is very doable and well suited for AHK but I would forget the binary clipboard approach and look into COM.

Here is an example of creating an Outlook email and then pasting a Word document into the email.

Code: Select all

; Copy Whole Story from Word
WordTemplateFile := A_Desktop '\Test\TestDoc.docx'
wdApp := ComObject("Word.Application")
wdDoc := wdApp.Documents.Open(WordTemplateFile)
wdApp.Selection.WholeStory
wdApp.Selection.Copy
wdApp.Selection.End := true
wdDoc.Close()
wdApp.Quit()

; Create Email and Paste Clipboard to Email Body
olApp := ComObjActive("Outlook.Application")
olMailItem := olApp.CreateItem(0) ; 0 = Mail Item
wdDoc := olMailItem.GetInspector.WordEditor
wdDoc.Range(0,0).Paste
olMailItem.Display
Outlook must be open. This opens and closes Word each time for demonstration but if Word was just left open it would be much faster for a batch process.

Once the email is created, then you can change text items as you might need with some Find and Replace. Like Find <<<NAME>>> and replace with the actual name. The editor used in Outlook is actually a plugin of Word.

If you don't really need all the fancy formatting that Word can allow, I would not even bother with Word templates. I mean if you are doing a bunch of pictures and colored text of all different sizes and really graphically pleasing formatting, then Word templates are great but if you are just needing text then Word is overkill.

Also, AHK can pull information from Excel very easily. If you already had the information in Excel, AHK could easily pull the information and create 50 emails and send them with the push of one button.

But if you got to type it in anyways, you could create a Gui that enters the data in your system and also sends the email at the same time, after you enter each. Or enter it in your system and then have AHK pull the information from your system by some means.

There are lots of options but this sounds like a great chance to lessen your workload dramatically depending on your scripting skills.

Rereading your post, since the original request forms are emailed to you, AHK could probably pull a lot of the information automatically from the emails, even if it is scanned images of the forms.

AHK watches for emails, pull data from appropriate emails, puts the data in an Excel spreadsheet, user reviews the data for problems, click a button, email sent. Pulling the data from a scanned form would be the hard part, sending the emails is relatively easy. AHK could also enter the data from the Excel in your system but that can be tricky too depending on your system.

It just occurred to me that you may not use Outlook but some online email application. Office is kind of the standard for business but not so much for education. That makes things quite a bit more complicated and the dream of full automation fleeting. :(

Ah, well, at least this shows you how to put a Word document on the clipboard without saving it as a binary.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 18:15

I agree with @Russ and @FanaticGuru that what you proposed in your 1st post is basically too convoluted. If the ultimate goal is to quickly create emails based on a template but with some level of customization, using COM with Outlook an a bit of HTML are your friends. This post provides a glimpse of the capabilities:
viewtopic.php?f=76&t=108026&p=481257&hilit=Proof+of+identity%2C+such+as#p481257

I am using something like that on a daily basis, pulling data from Excel as FG suggested.
14.3 & 1.3.7
blue_fields
Posts: 20
Joined: 16 Feb 2023, 11:07

Re: Modifying Clipboard Binary Data of Formatted Text

05 May 2023, 19:59

@FanaticGuru
@flyingDman
Thanks, I think you all make really good points. I can definitely try the COM object method, I already used the COM objects with ahk for Adobe Acrobat, so it probably won't be too hard, as long as the com object documentation for word is up to date and accessible. Thanks again for everyone's help!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 45 guests