SaveAs html without creating the subfolders with .xml files - html

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

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.

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

Can Excel interpret a cell as 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

HTML encoding issues - "Â" character showing up instead of " "

I've got a legacy app just starting to misbehave, for whatever reason I'm not sure. It generates a bunch of HTML that gets turned into PDF reports by ActivePDF.
The process works like this:
Pull an HTML template from a DB with tokens in it to be replaced (e.g. "~CompanyName~", "~CustomerName~", etc.)
Replace the tokens with real data
Tidy the HTML with a simple regex function that property formats HTML tag attribute values (ensures quotation marks, etc, since ActivePDF's rendering engine hates anything but single quotes around attribute values)
Send off the HTML to a web service that creates the PDF.
Somewhere in that mess, the non-breaking spaces from the HTML template (the s) are encoding as ISO-8859-1 so that they show up incorrectly as an "Â" character when viewing the document in a browser (FireFox). ActivePDF pukes on these non-UTF8 characters.
My question: since I don't know where the problem stems from and don't have time to investigate it, is there an easy way to re-encode or find-and-replace the bad characters? I've tried sending it through this little function I threw together, but it turns it all into gobbledegook doesn't change anything.
Private Shared Function ConvertToUTF8(ByVal html As String) As String
Dim isoEncoding As Encoding = Encoding.GetEncoding("iso-8859-1")
Dim source As Byte() = isoEncoding.GetBytes(html)
Return Encoding.UTF8.GetString(Encoding.Convert(isoEncoding, Encoding.UTF8, source))
End Function
Any ideas?
EDIT:
I'm getting by with this for now, though it hardly seems like a good solution:
Private Shared Function ReplaceNonASCIIChars(ByVal html As String) As String
Return Regex.Replace(html, "[^\u0000-\u007F]", " ")
End Function
Somewhere in that mess, the non-breaking spaces from the HTML template (the s) are encoding as ISO-8859-1 so that they show up incorrectly as an "Â" character
That'd be encoding to UTF-8 then, not ISO-8859-1. The non-breaking space character is byte 0xA0 in ISO-8859-1; when encoded to UTF-8 it'd be 0xC2,0xA0, which, if you (incorrectly) view it as ISO-8859-1 comes out as " ". That includes a trailing nbsp which you might not be noticing; if that byte isn't there, then something else has mauled your document and we need to see further up to find out what.
What's the regexp, how does the templating work? There would seem to be a proper HTML parser involved somewhere if your strings are (correctly) being turned into U+00A0 NON-BREAKING SPACE characters. If so, you could just process your template natively in the DOM, and ask it to serialise using the ASCII encoding to keep non-ASCII characters as character references. That would also stop you having to do regex post-processing on the HTML itself, which is always a highly dodgy business.
Well anyway, for now you can add one of the following to your document's <head> and see if that makes it look right in the browser:
for HTML4: <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
for HTML5: <meta charset="utf-8">
If you've done that, then any remaining problem is ActivePDF's fault.
If any one had the same problem as me and the charset was already correct, simply do this:
Copy all the code inside the .html file.
Open notepad (or any basic text editor) and paste the code.
Go "File -> Save As"
Enter you file name "example.html" (Select "Save as type: All Files (.)")
Select Encoding as UTF-8
Hit Save and you can now delete your old .html file and the encoding should be fixed
Problem:
Even I was facing the problem where we were sending '£' with some string in POST request to CRM System, but when we were doing the GET call from CRM , it was returning '£' with some string content. So what we have analysed is that '£' was getting converted to '£'.
Analysis:
The glitch which we have found after doing research is that in POST call we have set HttpWebRequest ContentType as "text/xml" while in GET Call it was "text/xml; charset:utf-8".
Solution:
So as the part of solution we have included the charset:utf-8 in POST request and it works.
In my case this (a with caret) occurred in code I generated from visual studio using my own tool for generating code. It was easy to solve:
Select single spaces ( ) in the document. You should be able to see lots of single spaces that are looking different from the other single spaces, they are not selected. Select these other single spaces - they are the ones responsible for the unwanted characters in the browser. Go to Find and Replace with single space ( ). Done.
PS: It's easier to see all similar characters when you place the cursor on one or if you select it in VS2017+; I hope other IDEs may have similar features
In my case I was getting latin cross sign instead of nbsp, even that a page was correctly encoded into the UTF-8. Nothing of above helped in resolving the issue and I tried all.
In the end changing font for IE (with browser specific css) helped, I was using Helvetica-Nue as a body font changing to the Arial resolved the issue .
I was having the same sort of problem. Apparently it's simply because PHP doesn't recognise utf-8.
I was tearing my hair out at first when a '£' sign kept showing up as '£', despite it appearing ok in DreamWeaver. Eventually I remembered I had been having problems with links relative to the index file, when the pages, if viewed directly would work with slideshows, but not when used with an include (but that's beside the point. Anyway I wondered if this might be a similar problem, so instead of putting into the page that I was having problems with, I simply put it into the index.php file - problem fixed throughout.
The reason for this is PHP doesn't recognise utf-8.
Here you can check it for all Special Characters in HTML
http://www.degraeve.com/reference/specialcharacters.php
Well I got this Issue too in my few websites and all i need to do is customize the content fetler for HTML entites. before that more i delete them more i got, so just change you html fiter or parsing function for the page and it worked. Its mainly due to HTML editors in most of CMSs. the way they store parse the data caused this issue (In My case). May this would Help in your case too

Outlook HTML Mail - changing linked items to embedded

I'm attempting to send HTML formatted emails using C# 3 via Outlook.MailItem
Outlook.MailItem objMail = (Outlook.MailItem)olkApp.CreateItem(Outlook.OlItemType.olMailItem);
objMail.To = to;
objMail.Subject = subject;
objMail.HTMLBody = htmlBody;
The email is generated externally by saving from an RTF control (TX Text Control), which yields HTML with links to images stored in a <<FileName>>_files subdirectory. Example:
<img border="0" src="file:///C:/Documents%20and%20Settings/ItsMe/Local%20Settings/Temp/2/zbt4dmvs_Images/zbt4dmvs_1.png" width="94" height="94" alt="[image]">
Sending the email this way generates a mail with broken links.
Using Outlook 2007 as the email client with Word as the email editor, switching to RTF (Options tab, Format tab group) preserves the layout and inlines the images.
Programmatically doing this via:
var oldFormat = objMail.BodyFormat;
objMail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
objMail.BodyFormat = oldFormat;
loses the formatting and mangles the images (the image becomes a [image] link marker on screen which is clickable but no longer shows the image). This isn't a surprise given that the documentation for MailItem.BodyFormat Property says "All text formatting will be lost when the BodyFormat property is switched from RTF to HTML and vice-versa".
Sadly there doesnt seem to be an easy way to change the Type of each Attachment in the MailItem.Attachements to OlAttachmentType.olByValue, as it's a read-only property that's set when you create the Attachment.
An approach that comes to mind is to walk the HTML, replacing the <img> tags with markers and programatically walking the MailItem text, inserting an Outlook.Attachment of Type OlAttachmentType.olByValue.
Another option is to convert the <img> links to use src="cid:uniqueIdN" and add the images as attachments with the referenced identities.
So, to the question... Is there a way to get the linked images converted to embedded images, ideally without getting into third party tools like Redemption? Converting to RTF happens to yield the outcome, but doing it that way is by no means a pre-requisite, and obviously may lose fidelity - I Just Want It to Just Work :D Neither of my existing ideas sound Clean to me.
Since you are using .net > 2.0, you may want to look into the System.Net.Mail namespace for the creation of mail messages. I have found that its quite customizable and was very easy to use for a task similar to yours. The only problems that I had was making sure I was using the right encoding, and I had to use HTML tables for layouts (css would not work right). Here are some links to show you how this works...
Basic
With multiple views (Plain Text and HTML)
If that's not an option, then I would recommend going the Content ID route and embedding the images as attachments. Your other option is to host the images publicly on a website, and change the image links in the html to the public images.
Something that you should be cognizant about is that HTML emails can easily look like spam and can be treated as such by email servers and clients. Even ones that are just for in-house usage (its happened to me) can end up in Outlook's Junk Mail folder..
DOH!, actually sending the email in Outlook 2007 forces the images to become embedded.
The Sent Item size of 8K is a lot smaller than the draft size of 60K (RTF) I was seeing vs the draft size of 1K (HTML that hadn't been converted to RTF and back again).
So it was Doing What I Mean all the time. Grr.
I'll leave the Q and the A up here in case it helps someone of a similarly confused state of mind.
BTW some useful links I found on my journey:
Sending emails example
General Q&A site with other examples of varying quality