|
|
Comments and Discussions
|
|
 |

|
I have 2 errors
1) Type 'Microsoft.Office.Interop.Word.Application' is not defined
2) Office is not a member of Microsoft
please help
|
|
|
|

|
First off, these sound like the same error. It sounds like you don't have a reference to the Microsoft.Office.Interop.Word assembly included in your references. This part can be confusing if you're not used to working with precompiled objects that aren't already included as a part of the language, but that's an integral part of programming and something you need to learn if you're going to write code.
This code requires a bit of tweaking on it's own for most folks to use it in their environment. That's because different people are working with different versions of the .net assembly and different releases of Office. It will be up to you to figure out which of these you need, but that shouldn't be too hard.
To make this code work in my project, I include references to the .net assemblies to Word and Outlook version 12 (we are using Office 2007 here) as well as the Microsoft.Vbe.Interop assembly, version 12. Previously when developing on XP and using Office 2003 I was referencing different versions.
Best of luck.
modified 15-Apr-13 15:47pm.
|
|
|
|

|
10xx it work
|
|
|
|

|
Thanks. Glad it worked for you.
|
|
|
|

|
Reading back over my post, (it's been a few years since I put this up) I see that I was using the COM object at the time and not the .net dll. Nonetheless, you will have to figure out what dll references you personally need in your environment and adjust accordingly.
|
|
|
|

|
The replace on à was not enough for me using office 14 interop. For whatever reason when using office 14 interop, random characters would trail the closing html tag. I just located the closing tag and removed any characters beyond it with:
sConvertedString = sConvertedString.Replace(Microsoft.VisualBasic.Right(sConvertedString, Len(sConvertedString) - 6 - InStr(sConvertedString, "</html>")), "")
Not sure if this is the same problem seen using office 12 interop. I would only see them at the very end and they were inconsistent, even on the same file.
|
|
|
|
|

|
What? How dare you give me a 4!!!
Just joking, thanks. Glad to have helped.
|
|
|
|

|
I don't know if there is a work around for this, but I am writing a richtextbox editor to generate a simple html file for a website. The problem I incurred using this module is that the html generated has href links to temp xml files stored on my local machine, thus I can't put the html up on a webpage.
Is there a workaround for this? Or do I need to create my own parsing module. Thanks for this, the output is perfect, but, I cannot copy both the html and all the related xml files up to the site without parsing through the html output and copying all the files it references. Thanks again,
|
|
|
|

|
Sorry to be so slow in replying. I meant to look at this problem and see if there was a relatively simple way to address it. However, work has been taking up most of my time and I've been remiss in following up. If I find the time to look into this in the near future I'll post my answer back here. Meanwhile, if you find a good solution, I'd certainly appreciate it if you posted it. Thanks for your compliment and good luck with your code.
|
|
|
|

|
It was very helpfull and exacly what i needed
|
|
|
|

|
Thanks, that's always good to hear.
|
|
|
|

|
Hi im new to vb.net and this the similist method of conversion. Props you too Hanle! =D
but how is this implemented =S as i want the HTML to be pasted into a seperate richtextbox.
Please could someone help =)
I Love Meth.
|
|
|
|

|
Assign the HTML string to the "text" property of whatever text box you wish it to show up in. However, I doubt the results will be exactly what you are looking for. You probably want to just put the results in a regular text box.
|
|
|
|

|
Hi,
I have a quick trick to remove some of the unnecessary html. Note, I am using Word 2010 but I expect this may help with earlier versions also.
I noticed that the HTML contains large blocks of comments i.e. "<!-- sometext -->" . I have therefore used a regular expression to remove the unecessary comments. Here is the code: -
Imports System.Text.RegularExpressions
....
'Also remove multiple  characters that somehow end up in there
sConvertedString = sConvertedString.Replace("Â", "")
' NEW LINE
sConvertedString = Regex.Replace(sConvertedString, "\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>", "")
' END OF NEW LINE
'…and you're done.
Regards,
Paul Nash
|
|
|
|

|
Nice addition. I may wrap up several improvements in a newer version shortly. I'll list credit for contributors of course.
|
|
|
|

|
Hi,
I second many of the comments here, it is a nice solution.
However I cannot get hyperlinks to be transfered from a richtextbox over into the generated HTML (they appear as plain text). Is this possible or am I missing something?
Regards,
Paul Nash
|
|
|
|

|
Not sure, I don't have the same problem. Hyperlinks show up as hyperlinks. May be a settings problem depending on your end product. Where is the final HTML ending up?
|
|
|
|

|
Hi,
Thank you for the great code. It is really helped me.
When I insert images the richtextbox they show alright but do not appear in the receipent's mailbox.
Does anybody know why or what can be done to correct this?
Thank you in advance.
croody
|
|
|
|

|
Sorry, I couldn't say for sure since I'm allowing Word to do the heavy lifting here. Some limitations aren't unexpected. I'll give it some thought and see if anything comes to mind. If you find a good solution, please post it back here.
Thanks,
Hanley
|
|
|
|

|
I've been pretty busy, but just recently had a chance to look at this while I was working on a related problem with my app. As near as I can tell, I'm not able to replicate the problem you're having. I didn't test extensively with different images though.
Are you having this problem with all images or just some particular ones? Are there any other parameters that may be affecting this outcome?
|
|
|
|

|
You will need to check the converted string for <img.
You need to get the file and add it as attachment and replace the src to cid:filename
this is my code:
If html.Contains("<img ") Then
Dim iPos As Integer = html.IndexOf("<img ")
Dim iPosSrc As Integer = html.IndexOf("src=""", iPos)
Dim iPosSrcEnd As Integer = html.IndexOf("""", iPosSrc + "src=""".Length)
Dim strImg As String = html.Substring(iPosSrc, iPosSrcEnd - iPosSrc)
strImg = strImg.Replace("src=""file:///", "")
myMItem.Attachments.Add(strImg, Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 0)
Dim fInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(strImg)
Dim strName As String = fInfo.Name
html = html.Replace(html.Substring(iPosSrc, iPosSrcEnd - iPosSrc), "src=""cid:" & strName)
While html.IndexOf("<img", iPosSrcEnd) > 0
iPos = html.IndexOf("<img ", iPosSrcEnd)
iPosSrc = html.IndexOf("src=""", iPos)
iPosSrcEnd = html.IndexOf("""", iPosSrc + "src=""".Length)
strImg = html.Substring(iPosSrc, iPosSrcEnd - iPosSrc)
strImg = strImg.Replace("src=""file:///", "")
myMItem.Attachments.Add(strImg, Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 0)
fInfo = My.Computer.FileSystem.GetFileInfo(strImg)
strName = fInfo.Name
html = html.Replace(html.Substring(iPosSrc, iPosSrcEnd - iPosSrc), "src=""cid:" & strName)
End While
End If
|
|
|
|

|
While this will work for html that contains the img tag the html generated using RTF that contains images doesn't output the img tags in the html. I think Word might be trying to convert but doesn't know what to do with it. The output after this method was called using RTF from an Outlook MailItem returns:
<p class=MsoNormal style='mso-layout-grid-align:none;text-autospace:none'><span
style='mso-spacerun:yes'>Â Â Â Â Â Â Â Â Â Â Â Â </span><span
style='mso-spacerun:yes'>Â </span><span style='mso-spacerun:yes'>Â </span><o:p></o:p></p>
There should be exactly two images there. The MailItem contains two attachments both Device Independant Bitmaps. Im not sure if these are in the actual RTF stream or the MailItem.
|
|
|
|

|
I'm definitely getting a different result than scotchy. The HTML created from rich text in my app definitely contains image tags.
When I debug and intercept the HTML string before it is handed over to Outlook, it contains image tags and it stores the images in a temp folder under "local settings", something like this: "C:\Documents and Settings\Username\Local Settings\Temp\msohtmlclip1\01\clip_image002.gif" It shows up in the body of the email just fine, although the body of the email has an image resizing/positioning tool built into it and the actual image data is more complex than simply the gif file I am listing here. I doubt that just attaching this gif file would give you the results you are looking for. If you want the image to show up properly, you need the automated process to handle it for you.
The process of html conversion in the example I am looking at creates the following series of six files that combined seem to contain the image data between them.
clip_colorschememapping.xml 1 KB
clip_image001.wmz 368 KB
clip_image002.gif 18 KB
clip_imaage003.wmz 1 KB
clip_image004.gif 1 KB
clip_themedata.thmx 4 KB
I would suggest looking for a configuration solution to this problem rather than trying to code around it at a low(er) level. What version of Visual Studio, Word and Outlook are you using? I'm currently using VS 2008 and Outlook 2007 although we were using VS and Outlook 2003 when this code was written. I'm also referencing the "Microsoft Word 12.0 Object Library" and running in a Windows XP environment with Office 2007 Installed. Are there any significant deviations to any of these configurations that might be changing the outcome?
|
|
|
|

|
Reading your reply closer, I see the difference. You are looking at the HTML after it has been inserted into Outlook. I am certain Outlook fishes out the img tags. When the initial conversion is done however (by either Word or by the Clipboard), the HTML is more traditional and does contain img tags.
|
|
|
|
|

|
Hard to say. I haven't run into that in particular, but bullets are likely to be problematic because they aren't usually a standard character ascii character. If the only problem you're having is the one you describe, I'd probably do a search and replace where you search for the "i," and bullet characters together and replace with just the bullet. This would help you avoid accidentally removing a valid character string.
You may need to do some additional coding to determine the character value of the bullet, and I would not assume that the "i," characters are exactly what they appear. I would probably parse the output to retrieve the raw integer character values and then use those when doing your "find and replace" operation. Of course, that's just my coding style.
Good luck, and sorry about the late reply. I don't check this email address that often.
HanleyK1
|
|
|
|
|

|
Thanks. Are you sure it's not too much like VBScript?
|
|
|
|

|
Hi
I was trying to convert from HTML to RTF/PlainText and was using the code from OutlookCode.com[^] but it wasn't quite working. After looking at your code, I was able to come up with a function that seems to be working. So here is my code share. Thanks for the help/inspiration.
Private Function GetHTMLBodyAsText(ByVal sourceItem As Object) As String
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Dim sConvertedString As String = ""
On Error Resume Next
' get a Word.Selection from the source item
objDoc = sourceItem.GetInspector.WordEditor
If Not objDoc Is Nothing Then
objSel = objDoc.Windows(1).Selection
objSel.WholeStory()
objSel.Copy()
objSel.PasteAndFormat(WdPasteDataType.wdPasteRTF)
objSel.WholeStory()
objSel.Copy()
sConvertedString = Clipboard.GetData(System.Windows.Forms.DataFormats.Text)
Else
MsgBox("Could not get Word.Document for " & _
sourceItem.Subject)
End If
objDoc = Nothing
objSel = Nothing
Return sConvertedString
End Function
|
|
|
|

|
Thanks, good to know that it helped. Also nice that you built on it and were kind enough to post your code. I guess it's the logical next step to convert HTML back to Rich Text and/or text. I'm still somewhat dismayed that Microsoft hasn't simply included access to this functionality in their libraries, but at least there's a back door.
|
|
|
|

|
More akin to VBS and not really VB.NET
|
|
|
|

|
Sorry, I'm not following you. This is in fact a VB.net project created in visual studio 2008. (Actually, it's just a function removed from a larger project for inclusion in other people's projects, but still VB.net)
If you're having trouble using it in your project, it could be that you are using an earlier version of visual studio or that you haven't got the right references in your project. Tell me what kind of difficulty you're having implementing it and I'll see if I can address it.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
A quick and easy solution to produce excellent HTML from RTF without parsing
| Type | Article |
| Licence | CPOL |
| First Posted | 12 Jan 2010 |
| Views | 47,722 |
| Downloads | 1,802 |
| Bookmarked | 25 times |
|
|