Kaushal Modi
Kaushal Modi

Reputation: 1370

How can I insert HTML formatted text in Outlook?

I have this syntax highlighted code snippet generated in emacs and would like to paste it in an Outlook mail (of course, with the html rendered, without the html code).

<pre>
<span style="color: #a020f0; background-color: gtk_selection_bg_color;"> function</span><span style="background-color: gtk_selection_bg_color;"> </span><span style="color: #008b8b; background-color: gtk_selection_bg_color;">uvm_object</span><span style="background-color: gtk_selection_bg_color;"> uvm_object::</span><span style="color: #0000ff; background-color: gtk_selection_bg_color;">clone</span><span style="color: #707183; background-color: gtk_selection_bg_color;">()</span><span style="background-color: gtk_selection_bg_color;">;
   uvm_object tmp;
   tmp = </span><span style="color: #228b22; background-color: gtk_selection_bg_color;">this</span><span style="background-color: gtk_selection_bg_color;">.</span><span style="color: #0000ff; background-color: gtk_selection_bg_color;">create</span><span style="color: #707183; background-color: gtk_selection_bg_color;">(</span><span style="color: #0000ff; background-color: gtk_selection_bg_color;">get_name</span><span style="color: #7388d6; background-color: gtk_selection_bg_color;">()</span><span style="color: #707183; background-color: gtk_selection_bg_color;">)</span><span style="background-color: gtk_selection_bg_color;">;
   </span><span style="color: #a020f0; background-color: gtk_selection_bg_color;">if</span><span style="color: #707183; background-color: gtk_selection_bg_color;">(</span><span style="background-color: gtk_selection_bg_color;">tmp == </span><span style="color: #228b22; background-color: gtk_selection_bg_color;">null</span><span style="color: #707183; background-color: gtk_selection_bg_color;">)</span><span style="background-color: gtk_selection_bg_color;">
     </span><span style="color: #0000ff; background-color: gtk_selection_bg_color;">uvm_report_warning</span><span style="color: #707183; background-color: gtk_selection_bg_color;">(</span><span style="color: #8b2252; background-color: gtk_selection_bg_color;">"CRFLD"</span><span style="background-color: gtk_selection_bg_color;">, </span><span style="color: #a020f0; background-color: gtk_selection_bg_color;">$sformatf</span><span style="color: #7388d6; background-color: gtk_selection_bg_color;">(</span><span style="color: #8b2252; background-color: gtk_selection_bg_color;">"The create method failed for %s,  object cannot be cloned"</span><span style="background-color: gtk_selection_bg_color;">, </span><span style="color: #0000ff; background-color: gtk_selection_bg_color;">get_name</span><span style="color: #909183; background-color: gtk_selection_bg_color;">()</span><span style="color: #7388d6; background-color: gtk_selection_bg_color;">)</span><span style="background-color: gtk_selection_bg_color;">, UVM_NONE</span><span style="color: #707183; background-color: gtk_selection_bg_color;">)</span><span style="background-color: gtk_selection_bg_color;">;
   </span><span style="color: #a020f0; background-color: gtk_selection_bg_color;">else</span><span style="background-color: gtk_selection_bg_color;">
     tmp.</span><span style="color: #0000ff; background-color: gtk_selection_bg_color;">copy</span><span style="color: #707183; background-color: gtk_selection_bg_color;">(</span><span style="color: #228b22; background-color: gtk_selection_bg_color;">this</span><span style="color: #707183; background-color: gtk_selection_bg_color;">)</span><span style="background-color: gtk_selection_bg_color;">;
   </span><span style="color: #a020f0; background-color: gtk_selection_bg_color;">return</span><span style="color: #707183; background-color: gtk_selection_bg_color;">(</span><span style="background-color: gtk_selection_bg_color;">tmp</span><span style="color: #707183; background-color: gtk_selection_bg_color;">)</span><span style="background-color: gtk_selection_bg_color;">;
</span><span style="color: #a020f0; background-color: gtk_selection_bg_color;"> endfunction</span><span style="background-color: gtk_selection_bg_color;">
</span></pre>

It rendered in HTML as below.

enter image description here

As Outlook mail supports html formatting I am curious if there's a way to use the html code directly to created a well-formatted email body text.

My currently approach below works but is slow:

I am hoping someone posts a solution involving some sort of windows background script (just as autohotkey works as a background service) that renders an html code in the clipboard on-the-fly and pastes that.

Upvotes: 4

Views: 8222

Answers (2)

Zach Dahl
Zach Dahl

Reputation: 637

update

It appears that copying to 'text/html' and then 'text/plain' allows copying to both Outlook correctly and plain text locations. YMMV but it is worth a try if you want to be able to paste everywhere.

https://jsfiddle.net/bzc97301/

original

I discovered a simple workaround for this issue using javascript. You copy to the text/html clipboard and leave your text/plain clipboard empty. Pasting into Outlook then works without a hitch.

Minimal code to reproduce:

function handleCopy(evt) {
  evt.clipboardData.setData('text/html', '<table><thead><tr><th>some</th></tr></thead><tbody><tr><td>markup</td></tr></tbody></table>')
  evt.preventDefault(); // prevent writing to text/plain
}

document.addEventListener('copy', handleCopy)
document.execCommand('copy')
document.removeEventListener('copy', handleCopy)

Functional example with text entry (you can copy paste into the textarea and then hit the button to copy to text/html clipboard. Do note that pasting most places will not do anything: the input must accept text/html content - thankfully for us Outlook does):

https://jsfiddle.net/schtauffen/nequ7Lzs/13/

Upvotes: 4

pjammin
pjammin

Reputation: 1

I have a simple python script to convert plain text HTML in my copy buffer to HTML formatted text which I can then paste into Microsoft Office programs. It requires a package "pywin32" available here.

import htmlclipboard
import win32clipboard

win32clipboard.OpenClipboard()
clipboardStr = win32clipboard.GetClipboardData()
htmlclipboard.PutHtml(clipboardStr)

It doesn't meet your requirement to run as a background service, but in my case, I've pinned the script to my quicklaunch bar which makes it easy to run when I need it. Also, I use this elisp procedure to copy my current selection in emacs to the kill ring as HTML to preserve the formatting:

(defun copy-region-as-html (START END)
  (interactive "r")
  (let ((snippet (buffer-substring START END))
        (buf (get-buffer-create "*htmlized_to_clipboard*"))
        (htmlized-file-name (expand-file-name "~/htmlized.html")))
    (set-buffer buf)
    (delete-region (point-min) (point-max))
    (insert snippet)
    (htmlize-buffer)
    (set-buffer "*html*")
    (clipboard-kill-region (point-min) (point-max))
    (kill-buffer "*html*")
    (kill-buffer "*htmlized_to_clipboard*")
    (message "HTML copied to clipboard")))

In my case, if I want to email code preserving the formatting I see in emacs:

  1. Select the region in the emacs buffer
  2. M-x copy-region-as-html
  3. Run the python script from the quicklaunch bar
  4. Paste into outlook

Upvotes: 0

Related Questions