EDIT: On OSX, copy/paste works perfectly from Firefox, but not from Chrome or Safari
Is it possible to copy table cells with colspan to Excel that reflects the structure?
Would like an app to render tables in HTML such that a user can copy/paste it to Excel reasonably well. In particular, would like some table cells with colspan=# to copy to Excel with correct alignment.
The problem is that Excel seems to ignore colspan in copy/paste. Thus in the example below the column title "Size" appears too far to the left, over the column for "Yellow" when it should appear over the column for "small"
I don't particularly care if the Excel cell is "merged", "centered across selection" or just plain separate cells. They key thing is to have the position be semantically correct.
HTML5 okay as well as any hack
CodePen: http://codepen.io/gradualstudent/pen/MYwNpY
HTML table as rendered in browser:
Table after copy/paste to Excel:
The Clipboard handling differ in different browsers. Whether Excel can get a table from the clipboard in the correct format depends on which data have the different browsers put in the clipboard. Unfortunately the most actual operating systems comes not more with a clipboard viewer per default. So we cannot simply look there what the differences are.
But Excel can deal with HTML tables. And so if HTML source code of a HTML table is in the clipboard, then Excel can paste this properly.
Example with VBA:
Sub HTMLinClipboardTest()
sHTML = "<table>" & _
"<tr><td colspan=""2"" align=""center"">colspan2</td></tr>" & _
"<tr><td rowspan=""2"" style=""vertical-align:middle;"">rowspan2</td><td>default</td></tr>" & _
"<tr><td>default</td></tr>" & _
"<tr><td>default</td><td>default</td></tr>" & _
"</table>"
Dim oDataObject As New DataObject 'needs Microsoft Forms 2.0 Object Library
oDataObject.SetText sHTML
oDataObject.PutInClipboard
ActiveSheet.Range("a1").Select
ActiveSheet.Paste
End Sub
Related
I have been searching the internet for hours to find a solution to my question.
I know how to import mails for a long time, but can't keep the format of the HTML-body like I could do when copy the body from OL by hand into the Rich-text memo field on an access form.
The field (olbody) is bound to a SQL server table field olbody (nvarchar(max)) and could store html mail body text when I insert the content of the clipboard.
I even tried to read the .HTMLbody property in the clipboard
clp.SetText .HTMLBody
clp.PutInClipboard
and then I pasted manually the content of the clipboard with Ctrl+V in the field, but again, it does no show up like the HTML mail. But I can see all HTML commands, which should format the mail.
Without formating the mail in the access field, the content is almost unreadable because no tab, lineskip etc. is in the body.
Any help welcome.
Thanks
The proper way to display HTML is using a web browser. This means: use the web browser control!
You can load the HTML in the webbrowser control on load, and when your mail field changes.
Example code:
Private Sub Form_Current()
Dim wb As Object
Set wb = MyWebbrowserControl.Object
With wb
.Navigate2 "about:blank"
Do Until .ReadyState = 4 '=READYSTATE_COMPLETE
'This is a somewhat inefficient way to wait, but loading a blank page should only take a couple of milliseconds
DoEvents
Loop
.Document.Open
.Document.Write MemoField.Value
.Document.Close
End With
End Sub
Replace Memofield with your mail field, and MyWebbroserControl with your webbrowser control, and you should be ready to go.
Note that you probably should use the Access web browser, and not the ActiveX web browser. This code will work with both, but the Access web browser control uses a more modern version of IE, and doesn't come with a non-removable border.
I've run into something that I find a bit strange... There is a website with a <table> of audio file links, author, etc.
The link looks like:
This & That
I found it strange that the & character is not written as & in the source file. If I use the Chrome Dev Tools (or Firefox) and change the content to This & That, the string & is visible, instead of just the & character. Why is that?
The table is using TableSorter, which I thought could be doing this under the hood... but even if I open it with Javascript disabled, the raw & character is still displayed, so that doesn't seem to the be culprit.
When are HTML entities left unparsed?
They aren't. The HTML is parsed as it's being read. This is completely independent of JavaScript.
I found it strange that the & character is not written as & in the source file.
Indeed, it should be, but it works the way it is because browsers handle invalid markup gracefully, and in fact that particular aspect is now encoded in the specification: An ampersand that isn't ambiguous is taken as a literal ampersand.
If I use the Chrome Dev Tools (or Firefox) and change the content to This & That, the string & is visible, instead of just the & character. Why is that?
When you view the DOM through Chrome's devtools and similar, you're seeing the live DOM represented in valid form, not the actual source file, so naturally the browser shows you that & as & (the entity it should have been in the source file).
This is independent to Javascript.
& and & has the same HTML number as : &, which is also the ASCII Dec provision of those symbols. You can take a look at here : http://www.ascii.cl/htmlcodes.htm
So I guess there is a problem with your code, they should be seen as the same symbol (&) in your screen.
I'm writing a piece of code to populate a spreadsheet, then email it out to a bunch of people with the spreadsheet as an attachment, along with an explanatory PDF and an HTML covering letter (featuring the company logo) in the email body.
Now, I've sent emails with Excel VBA code before, and I've formatted their layout with HTML in the .HTMLBody. But the other bits are causing me some bother.
The attachments I think I can figure out; I assume I can just use multiple .Attachments.Add with the various files, haven't tried that.
The main question is the company logo in the email body. I'm fairly well up on HTML, but I can't figure out how to actually embed a picture when I don't have a src for it.
Googling has produced a few approaches, but they've been cid-based and have had comments about only working if the reader uses Outlook.
Any suggestions? Is there a nice simple solution?
try the below code,
for attaching the image to your mail(add this under create mail object),
.Attachments.Add ThisWorkbook.Path & "\image.png"
add this into your mail body (assuming you are using table)
"<td Colspan=2><img src='cid:image.png' height=205 width=1015></td>"
I am trying to SaveAs all incoming emails as .html files but it creates a subfolder with each email.
Here is my code:
Public Sub ShowMessage(Item As Outlook.MailItem)
Dim path As String
path = "C:\Users\me\Desktop\"
Item.SaveAs path & Item.SenderName & ".htm", olHTML
End Sub
It gives me something like:
"joe.htm" and a folder call "joe_files" with "colorschememapping.xml" and "filelist.xml" as well as "themedata.thmx"
Is there a way to save as without these files?
In Word I solved the problem by saving as Filtered HTML but it does not seem to be possible with Outlook.
Here is my code in MSWord:
ActiveDocument.SaveAs FileName:=Path, FileFormat:=wdFormatFilteredHTML
Or could it be possible to use the MailItem.BodyFormat to edit the item's body and then save it as html? That way we won't have all the automatic encoding of Outlook when it saves as HTML. (https://msdn.microsoft.com/en-us/library/office/ff869979.aspx)
Never mind... I solved it.
By re-reading my question I just realized that the answer was right there.
item.SaveAs path & Item.SenderName & ".htm", olTxt
Somehow the support page of Microsoft does not mention that Filtered HTML works on their code but it actually does.
https://msdn.microsoft.com/en-us/library/office/ff868727.aspx?
Try to use olMHTML (10) format instead.
You can also simply read the HTMLBody property and save it as file - the problem might be Unicode characters - HTMLBody property is UTF-16, and an HTML file has to be 8 byte, so you need to convert it appropriately based on the value of the PR_INTERNET_CPID (which might not necessarily match the code page in the HTML body).
If using Redemption is an option (I am its author), it supports the olHTMLEmbeddedImages format it creates an HTML file with the image data inside the <img> tags. Outlook (which uses Word to render HTML message) does not like that, but both IE and Firefox should be fine. It embeds both the images already attached to the message as well as referenced images (which are downloaded):
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Item = Session.GetMessageFromID(Application.ActiveExplorer.Selection(1).EntryID)
Item.SaveAs "c:\temp\HTMLWithImages.html", 1033 ' olHTMLEmbeddedImages
I'm using Aspose.Cells to build an Excel document programmatically. This works great. One of the cells, though, is a block of raw HTML. I'm wondering if it is possible to tell Excel (in any fashion, including the GUI - you don't need to know the Aspose API) to parse and display a cell as HTML. Right now, it just shows up as the raw HTML in text format, tags and all.
I know Excel is capable of having HTML pasted into it, but it looks like it just parses it on its own and then Excel-ifies it for you, and it doesn't store the HTML, so it's not actually parsing it and displaying it as HTML. Plus, I can't figure out how to replicate this paste functionality anyway.
Thanks.
Unfortunately the answer is no.
Excel has two HTML options:
Open a HTML file, which will sort of render the HTML, sort of, but won't contain any actual HTML in cells
Store HTML in cells, but as unformatted text.
You could, maybe possibly, come up with a macro that lets you enter HTML into a cell, then saves that HTML as a document, opens it up in another instance of Excel, then grabs that formatted HTML and places it in the original document; that way you would have two columns, one with the HTML, and one with the output. It would be very unsightly though. Don't do it :0)
Pasting html data in excel will result in the html being properly displayed in excel. The one issue with this is that carriage returns and tabs will be pasted to the next cell.
Dim objData As New DataObject
objData.SetText(sHTML)
Clipboard.SetDataObject(objData)
objRange.PasteSpecial()
Will fill a cell with properly formated text
This code worked for me on one cell (inspired by #Rick's answer, but with few changes because Clipboard.SetDataObject(objData) caused error and also objRange.PasteSpecial() didn't work):
Private Sub Worksheet_Change2(ByVal Target As Range, ByVal sht As Worksheet)
Dim objData As DataObject 'Set a reference to MS Forms 2.0'
Dim sHTML As String
Dim sSelAdd As String
Application.EnableEvents = False
objData = New DataObject
sHTML = Target.Text
objData.SetText sHTML
objData.PutInClipboard
sht.PasteSpecial Format:="Unicode Text"
Application.EnableEvents = True
End Sub
Sub test()
Dim rng As Range
Set rng = ActiveSheet.Range("F15") 'cell to change'
Worksheet_Change2 rng, ActiveSheet
End Sub
see this post for some more details
I guess it shouldn't be too difficult to tweak it a bit that it would work for entire worksheet and not only one specific cell, you should probably add some if condition to wrap this code in order to prevent errors, see this post for some more info
I found an interesting YouTube video which shows how to create a simple HTML Interpreter (VBA) in Microsoft Excel using the web browser control. Enter your HTML and CSS code into a text box, and the form will convert the HTML into a web preview.
HTML Interpreter in Microsoft Excel 2010/2007 - Write directly to Web Browser