I’m trying to replicate a VBA function I made into VB.NET.
it basically gets a mail and then it extracts its TABLE tag from its HTMLBody.
My issue is that the functions give a different output.
VBA’s returns a standard HTML format, with all the tags, TABLE included.
VB.NET’s output instead is the one below.
<!-- Converted from text/rtf format -->
<P><FONT SIZE=2>TEXT0,<BR>
<BR>
<BR>
INTRO:<BR>
<BR>
A. TEXT1<BR>
<BR>
B TEXT2<BR>
<BR>
C. TEXT3<BR>
<BR>
D. TEXT4<BR>
<BR>
E. TEXT5 & TEXT6<BR>
<BR>
<BR>
This instead what I would like to have
<body lang=EN-US link=blue vlink=purple style='word-wrap:break-word'><div class=WordSection1><p><span style='font-family:Arial'>TEXT0<o:p></o:p></span></p>
<p><span style='font-family:Arial'><br>INTRO <o:p></o:p></span></p>
<p><span style='font-family:Arial'>A. TEXT1 <o:p></o:p></span></p>
<p><span style='font-family:Arial'>B. TEXT2 <o:p></o:p></span></p>
<p><span style='font-family:Arial'>C. TEXT3 <o:p></o:p></span></p>
<p><span style='font-family:Arial'>D. TEXT4 <o:p></o:p></span></p>
<p style='margin-bottom:12.0pt'><span style='font-family:Arial'>E. TEXT5
I already tried all the BodyFormats, the result is almost the same, no standard HTML.
The references used in both are the same.
Below the code.
Function GetMailTable(Subject As String, daysAgo As Integer, ParamArray Keywords() As Object)
Dim myOlApp As New Outlook.Application
Dim objNamespace As Outlook.NameSpace = myOlApp.GetNamespace("MAPI")
Dim objFolder As Outlook.MAPIFolder = objNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim itm As Outlook.MailItem
Dim Found As Boolean
Dim strFilter As String
Dim Subj As String = Subject
Dim arrList As ArrayList = New ArrayList
daysAgo = myDate.Subtract(previousBusinessDay(myDate, daysAgo)).TotalDays
strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%" & Subj & "%'"
Dim filtereditemssubj As Object = objFolder.Items.Restrict(strFilter)
Dim filteredItems As Object = filtereditemssubj.Restrict("[ReceivedTime]>'" & Format(myDate.AddDays(-daysAgo), "dd/MM/yyyy") + " 00:00" & "'")
filteredItems = filteredItems.Restrict("[ReceivedTime]<'" & Format(myDate.AddDays(-daysAgo).AddDays(1), "dd/MM/yyyy") + " 00:00" & "'")
Dim htmlDoc As New mshtml.HTMLDocument
Dim tables As mshtml.DispHTMLElementCollection
Dim Table As mshtml.HTMLTable
If filteredItems.Count = 0 Then
Found = False
Else
Found = True
For Each itm In filteredItems
htmlDoc.HTMLBody = itm.HTMLBody
tables = htmlDoc.getElementsByTagName("table")
For Each Table In tables
arrList.Add(Table)
Next Table
Next itm
End If
myOlApp = Nothing
Return arrList
End Function
You can try to replicate it including a table sample in a mail body.
Has anyone a suggestion?
Thanks everyone
2
Answers
Community wiki because this probably won’t fully solve the problem (probably going to give the same result), but it’s also too long for a comment. The code below re-writes some things for modern VB.Net. If this is still just an Office add-in you might not be able to use all these language features, but some of the changes (like not using ArrayList or hungarian variable prefixes) have been standard practice for more than 15 years, and it has NEVER been needed or at all helpful with .Net to set items to
Nothing
at the end of a method.Also, the
Keywords
argument was never used. I only left it in case there’s code somewhere calling the method that might break if the parameter was not defined.Of course calling code must also adjust for the new return type. The new result can be used with a
For Each
loop, with the Linq operator methods, or as a list by appending.ToList()
to the function call. Try to avoid the last one as much as possible.Finally, if the
filteredItems
type, which I can’t see, doesn’t formally implement the right interface, you may need to include this method (in a separate class or module):And then the first line of the
Return
statement like would look like this:We know
filteredItems
will have the requiredGetEnumerator()
method, or it wouldn’t have worked with aFor Each
loop in the original code. It’s also possible explicitly typing the initialfilteredItems
variable declaration would work to avoid this, but if you run into this problem at all I don’t think it will help.The HTML indicates that the message is plain text or RTF, not HTML. Most likely your script is accessing a message different from what VBA accessed.
Try to find the right message, select it, and use
Application.ActiveExplorer.Selection
collection instead of the restrictedItems
collection as a test.