Can Excel interpret a cell as HTML? - html

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

Related

Unable to copy the cell color from excel to outlook

I am unable to copy the entire cell format (ex. cell color) when using vba code to send emails to client using excel
I want to copy the cell color to outlook. The formula only let me copy the cell value only.
In case of HTML you need to get the required formatting from Excel objects and then set up HTML attributes (or styles) on your own.
You can also use the Word object model for copying and pasting the data with formatting. The WordEditor property returns the Microsoft Word Document Object Model of the message being displayed. The returned Word Document object provides access to most of the Word object model. See Chapter 17: Working with Item Bodies for more information.
Also be aware, the HTMLBody property in the following piece of code returns a well formed HTML document:
Signature = OutMail.HTMLBody
But later in the code you add some HTML markup before the document begins which is not correct. Even if Outlook handles such cases correctly, there is a possible mistake if the behavior is changed at some point. Keep the HTML markup well-formed inserting the content between the opening and closing <body> tags.

Need help understanding how imaegs in a loaded HTML page in IE are stored/accessible (Excel VBA)

I am scraping data from a proprietary system that serves inventory information via HTML. It includes images of parts that I'd like to scrape, but I cannot use the src link directly (the system is designed to block requests that aren't part of a returned web search). Since I am already scraping the HTML/page text, I'm hoping that someone can tell me how images are stored in IE once a page is loaded, and if there is any way to access that using the MS HTML reference commands/library
I've tried pinging the server directly with various versions of the src URL, with no luck.
Here are some relevant lines of my VBA:
Dim IE As InternetExplorer
Set IE = objShell.Windows(x)
Dim html As Object
Set html = IE.document
Dim ResultClasses as Object
Set resultClasses = html.getElementsByClassName("PM Parts List")
My goal is to add the appropriate IE/HTML call to get the images that were loaded when the page was originally loaded in IE. I cannot call the page to load it from the start within Excel VBA, due to the way items are selected (the load of building out a whole interface within Excel and using sendkeys would be way more hassle than getting the pictures to add to the procurement form for use when validating order reciept

How to import HTML Outlook mails into MS Access using VBA

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.

SaveAs html without creating the subfolders with .xml files

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

mail-merge HTML from a database into MS Word

project: Using VB.NET to build a winforms database interface and work-automation app.
I am using this editor for the users to enter their text in the database interface environment that will both load/save/show them what they are working on in the form and also mail-merge into a Word document waiting for the content. I can do the first step and it works well, but how do I get MS Word to recognize HTML as formatting instead of just merging in tags and text all as text?
The tool has two relevant properties: one to get just the text (no markup, i.e. no HTML) and one to get the full markup with HTML. Both of these are in text format (which I use for easy storage in the Database).
ideas/directions I can think of:
1) use the clipboard. I can copy/paste the content straight from the editor window to Word and it works great! But loading from a database is significantly different, even when using the clipboard programatically. (maybe I don't understand how to use the clipboard tools)
2) maybe there is a library or class/function in Word that can understand the HTML as "mergable" content?
thanks!
:-Dan
You may use our (SautinSoft) .Net library to transform each of your HTML data to Word document.
Next you may merge all produced Word documents into single Word document. The component also have function to merge Word documents.
This is link download the component: http://www.sautinsoft.com/products/html-to-rtf/download.php
This is a sample code to transform HTML to Word document in memory:
Dim h As New SautinSoft.HtmlToRtf
Dim rtfString As String = ""
rtfString = h.ConvertString(htmlString)
This is a sample code to merge two documents in memory:
Dim h As New SautinSoft.HtmlToRtf
Dim rtfSingle As String = ""
rtfSingle = h.MergeRtfString(rtf1, rtf2)
I ended up using the clipboard to set the text. Here is a code sample that I needed to answer this question.
Clipboard.SetText(Me._Object.Property, TextDataFormat.Rtf)
I just didn't know how to tell the computer that the content was HTML or RTF etc. It turned out to be simple.
:-Dan