Outlook VBA - can view email Source Code? - vba

In MS Outlook, I want to loop through each selected email, and for each email, view the source code and check if the source file (as in txt format) contains a certain string "XX". Since using view source code can display the email content into html format and I would like to trace some format which with issues in the text.
Currently, I am doing manually by opening the mail, right click > View source > Ctrl+F to find the string I am looking for.
Is there a view to use VBA to do the action of "view source" in the email?
Dim individualItem As Object
For Each individualItem In Application.ActiveExplorer.Selection
'View Source Code of the email
'Find "XX" in the email body content
If Instr(individualItem.body, "XX") = 1 Then
Msgbox ("Find string!")
End if
Next Message
Thanks.

You are almost right, I see two issues:
1.) There are two distinct properties Body (only text) and HTMLBody.
2.) Instr returns the position of the search token. It can be any positive number if the token is found.
So try:
If Instr(individualItem.HTMLBody, "XX") <> 0 Then

By "source" do you mean the MIME headers of the message? They are stored in the PR_TRANSPORT_MESSAGE_HEADERS property - take a look at the message with OutlookSpy (I am its author - click IMessage button). You can reads that property using MailItem.PropertyAccessor.GetProperty. The DASL name of the PR_TRANSPORT_MESSAGE_HEADERS property "http://schemas.microsoft.com/mapi/proptag/0x007D001F".
Note that full MIME source of a message is never stored by Outlook - MIME is not native Outlook format.

Related

Send calendar item properties from Acces form to Outlook agenda

My script in Access-vba works (I can send from an Access form a calendar item to Outlook). But now I want to send some poperties (such as subject and location of the appointment) to the agenda in Outlook and then I get an error (see image attached). For subject I want to use some text from my current form (Me.Monitor_account) but that does not work. See the image attached. If I use: .Subject = "test" then it works. But I dont see the difference between the two.
.Subject = (Me![MonitorAccount]) 'worked!

VB 2010 - Button that opens DEFAULT email program and populates entries from txtboxes

I've created a form in VB that requests some entries and through those entries a body for an email is created. Now I have to make a button that opens the default email client of the computer and populates the Body with the text that is generated through the form (I don't want it to send the email since the user is supposed to add the From and the Subject). I've managed to make it happen through this simple line :
Dim Text As String = Textbox1.Text
Process.Start("mailto:email#email.com?subject=Hello&body=" & Text)
However, when the email client opens the format of the body is messed up. There are supposed to be paragraphs and spaces but there aren't any. Can you help me fix this ?
In order for MailTo to work properly, you need to URL Encode your message. The following should work:
Dim Text As String = Textbox1.Text
Process.Start("mailto:email#email.com?subject=Hello&body=" & System.Web.HttpUtility.UrlEncode(Text))

outlook vba code to display pictures in email

By default, my MS Outlook 2013 is set NOT to download images in received HTML e-mail messages. I would like to keep this setting.
There are some senders whose emails are handled by my Outlook VBA code...and filed into specific folders (rather than the INBOX). I do not use the in-built RULES.
These are known senders...and I would like to have the pictures in the emails from these SELECT KNOWN senders downloaded and displayed. I could do this manually for each email... by right clicking etc... but that is a pain... when there are many such emails.
I am unable to figure out the few lines of code (one line ?) required to download / enable display of images / pictures in the email. Something like... MailItem.Display (which does not work... it only displays the mail in an independent window)... or MailItem.DisplayImages (that is not a known method!).
I would include this one line (or lines) in the routine which handles emails from some known senders....so that their emails always have images / pictures downloaded and displayed.
Thanks.
You would need to set the PidTagBlockStatus property - see http://msdn.microsoft.com/en-us/library/ee219242(v=exchg.80).aspx.
Note that while you can read/write that property using MailItem.PropertyAccessor.SetProperty, you will not be able to calculate its value correctly - Outlook Object Model rounds off the value of the message delivery time, and you would need the raw Extended MAPI value (accessible in C++ or Delphi only) as the FileTime structure.
If using Redemption (I am its author) is an option, it exposes the RDOMail.DownloadPictures property. Something like the following should do the job (VB script):
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Item = Session.GetRDOObjectFromOutlookObject(YourOutlookItem)
Item.DownloadPictures = true
Item.Save
The Outlook object model doesn't provide any property or method for that.

Accessing User Defined Fields in Outlook

So, I have a custom email form/message like below and I want to access the "Doc Title:" field value to insert it into the body of the email.
I currently have this code;
Function Item_Send()
Item.Body = Item.Body + UserProperties.Find("TextBox1").Text
End Function
And I've tried multiple variations of this, such as Item.UserProperties.Find(...).Value, Find(...).Value by itself, UserProperties.Find("TextBox1", false).Text, etc.
Research;
CodeProject
MSDN Find Method Documentation
Microsoft Support - How to create an email message form
Microsoft Support - FAQ about custom outlook forms
Microsfot Support - Working with User Defined Fields
I just can't seem to find a solution.
The posted code returns Object requred: 'UserProperties.Find(...)'
If I add in false to the parameters I get; Object doesn't support this property of method: 'UserProperties.Find'
Find by itself gives me Type mismatch: 'Find'
And that's all the error messages I can get to come up. Any help would be greatly appreciated. (I'm using the Script Editor button to write the above code, not the Visual Basic button).
You never check that UserProperties.Find returns null. Change the problematic line to
set prop = Item.UserProperties.Find("TextBox1")
if Not (prop Is Nothing) Then
Item.Body = Item.Body + prop.Value
End If
Also make sure the property name is really "TextBox1", that sounds like a control name. Have a look at the item with OutlookSpy (I am its author): click Item button, select the UserProperties property, click browse, go to the IEnumVariant tab, double click on the property.
You can also click the IMessage button to see the raw MAPI properties.

Validating Attachment in Richtext field

I am using below code to validate the Attachment in Richtext field.
If I will not used Call source.Refresh(True)
then validation is not working, but this code is also refreshing document everytime querysave is called in buttons.
So is there any option or any other idea so that I should not user this Refresh part or entire code to validate .
If anybody have more efficient code then please share this.
If Source.Document.YesNo20(0)="Yes" Then
Call source.Refresh(True)
Dim rtitem As NotesRichTextItem
Set rtitem = source.Document.GetFirstItem( "Atchmnt20" )
NotesEmbeddedObjectArray = rtitem.EmbeddedObjects
If Isempty ( NotesEmbeddedObjectArray ) Then
Messagebox "Please enter an attachment in 20a. As you selected option Yes"
continue=False
Exit Sub
End If
End If
There's a way in LotusScript to check attachments presence even for new (not saved) documents.
Create a hidden computed field, for instance AttachmentNames with formula:
#If(#AttachmentNames!=""; "1"; "");
In LotusScript do the following:
'in new documents Form field may be empty
If doc.Form(0) = "" then
doc.Form = "YourFormAlias"
End If
'computing doc contents with the form
call doc.ComputeWithForm(false, false)
If doc.AttachmentNames(0) = "" then
MsgBox "Please attach a file",,"Attention"
Continue = False 'if you are running this code in QuerySave
Exit Sub
End If
Validating rich text fields in Lotus Notes is a bit of a dark art, but can you not just do this? (where doc is the back-end):
If(doc.HasEmbedded) Then Continue = True
There are other things you can do. Check this Lotus Developer Domain post, which covers attachments, text, embedded objects, all sorts:
http://www-10.lotus.com/ldd/nd6forum.nsf/0/8b3df10667d355768525719a00549058
Can you validate RT field with formula?
I created a hidden field below my rich text field with this Input Validation formula:
REM {Validate just when saving};
#If(!#IsDocBeingSaved; #Return(#Success); "");
REM {Should contain some file};
_filenames := #AttachmentNames;
#If(
#Elements(_filenames)=0;
#Return(#Failure("You should attach at least one file"));
#Success);
Assuming that you want to avoid the Refresh because it takes too long, here is what you may want to look at and if feasible, try to change:
Maybe you can use the "Entering" event of the RichText field in conjunction with a global variable (in the form) to skip the Refresh in your code, if the RichText field wasn't touched at all.
Are there keyword fields with "Refresh choices on document refresh" option enabled that may be safe to disable? Or even place a button that would bring up a dialog and populate the field with the selected keyword(s) - refreshing the choices won't be neccessary then, as you can always present up-to-date choices through #DbColumn/#DbLookup or NotesUIWorkspace.PickListStrings.
Is there any code (LotusScript or Formula) in "Queryrecalc" and/or "Postrecalc" form events that may be possible to optimize? For example by using a global variable (in the form) as a flag whether to execute the code in Queryrecalc/Postrecalc - set it to false just before calling Refresh in your code, then set it back to true (because this Refresh only serves to update the RichText field to the backend document).