Nilesmaxim
03-14-2016, 06:12 AM
Hello again. Once again, I'm in need of assistance or advice, if there's any available.
I'm trying to find a way to export a memo field using rich text formatting into an excel cell. This is as far as I got:
Public Sub export(ByVal Target As Range, ByVal sht As Worksheet)
Dim objdata As DataObject
Dim sHTML As String
Dim sSelAdd As String
Dim s As String
Dim c As Range
xlApp.EnableEvents = False
Set objdata = New DataObject
sHTML = Target.Text
sHTML = Replace(sHTML, "<div>", "")
sHTML = Replace(sHTML, "</div>", "")
sHTML = "<html>" + sHTML + "</html>"
objdata.SetText sHTML
objdata.PutInClipboard
sht.Select
Target.Select
sht.PasteSpecial Format:="Unicode Text"
xlApp.EnableEvents = True
End Sub
NOTE: to use this code, I had to set a reference to Microsoft Forms 2.0 Object Library. If the reference isn't available in your reference list, browse to c:\windows\SysWOW64\FM20.dll, and register it. (Other references needed to export: Microsoft Office 14.0 Object Library; Microsoft Excel 14.0 Object Library.)
As you can see, I've had to extract the <div></div> tags (otherwise, the resulting string would display these tags as part of the text), and encapsulate the entire field with <html></html> tags, then put the entire thing into a clipboard, and then paste the clipboard contents into the selected target. I've also had to use the active sheet's PasteSpecial method rather than the cell's PasteSpecial method, because the Cell's version of the method does not accommodate the "Format" parameter, which is why I had to use the select method for both the sheet and the range (a no-no, I know, but it's the only way I know to target a specific cell if I am forced to use the sheet's "PasteSpecial" method.)
So far, this code somewhat successfully exports the contents of an rtf memo field into an excel cell, even preserving most of the formating, except for one problem. The memo field also contains carriage return-line feed characters. This is problematic because when the data gets pasted back, Excel splits the data using the CRLF character as a sort of a cell-delimiter, so the data gets split across several cells. I suppose I could extract the <CR> character, but then all the data would appear as one very long line, making the result appear rather cryptic.
Is there a way I can preserve the contents and the formatting of rtf memo field (including CRLF's) during export to an excel cell, AND contain the entire contents into multiple lines of the same cell, not having it split across multiple cells?:banghead:
Thanks in advance for any advice you may have to offer.
I'm trying to find a way to export a memo field using rich text formatting into an excel cell. This is as far as I got:
Public Sub export(ByVal Target As Range, ByVal sht As Worksheet)
Dim objdata As DataObject
Dim sHTML As String
Dim sSelAdd As String
Dim s As String
Dim c As Range
xlApp.EnableEvents = False
Set objdata = New DataObject
sHTML = Target.Text
sHTML = Replace(sHTML, "<div>", "")
sHTML = Replace(sHTML, "</div>", "")
sHTML = "<html>" + sHTML + "</html>"
objdata.SetText sHTML
objdata.PutInClipboard
sht.Select
Target.Select
sht.PasteSpecial Format:="Unicode Text"
xlApp.EnableEvents = True
End Sub
NOTE: to use this code, I had to set a reference to Microsoft Forms 2.0 Object Library. If the reference isn't available in your reference list, browse to c:\windows\SysWOW64\FM20.dll, and register it. (Other references needed to export: Microsoft Office 14.0 Object Library; Microsoft Excel 14.0 Object Library.)
As you can see, I've had to extract the <div></div> tags (otherwise, the resulting string would display these tags as part of the text), and encapsulate the entire field with <html></html> tags, then put the entire thing into a clipboard, and then paste the clipboard contents into the selected target. I've also had to use the active sheet's PasteSpecial method rather than the cell's PasteSpecial method, because the Cell's version of the method does not accommodate the "Format" parameter, which is why I had to use the select method for both the sheet and the range (a no-no, I know, but it's the only way I know to target a specific cell if I am forced to use the sheet's "PasteSpecial" method.)
So far, this code somewhat successfully exports the contents of an rtf memo field into an excel cell, even preserving most of the formating, except for one problem. The memo field also contains carriage return-line feed characters. This is problematic because when the data gets pasted back, Excel splits the data using the CRLF character as a sort of a cell-delimiter, so the data gets split across several cells. I suppose I could extract the <CR> character, but then all the data would appear as one very long line, making the result appear rather cryptic.
Is there a way I can preserve the contents and the formatting of rtf memo field (including CRLF's) during export to an excel cell, AND contain the entire contents into multiple lines of the same cell, not having it split across multiple cells?:banghead:
Thanks in advance for any advice you may have to offer.