Outlook add-in mailitem.display not working Visual Basic - visual-studio

I'm in the process of converting some outlook VBA macros to an Add-in. I am having difficulty with my macros that create emails based on a template. I decided to code a simple button to create and display a new email with the subject test.
Everything is working up to displaying the email which doesn't happen.
Private Sub ButtonGenEmail_Click(sender As Object, e As EventArgs) Handles ButtonGenEmail.Click
Me.Close()
Dim objApp As Outlook.Application
Dim objMail As Outlook.MailItem
objApp = Globals.ThisAddIn.Application
objMail = objApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
objMail.Subject = "test"
objMail.Save()
objMail.Display(False)
End Sub
At one point I added msgbox "Done" after objMail.Display(False) and the message never appears.
Thanks in advance

This should obviously work. I'm not so skilled in Outlook but I guess the problem here is that you run it from a form (I see the Me.Close)
Isn't then Outlook blocking to display the mail because the form is still open (I guess modal = ShowDialog())?
I think you will have to either change the form to be not modal (use Show() instead of ShowDialog()) or handle the event after the form is closed, something like
All code below written from top of my mind, so I may miss something
A method from where you initialize the form
Dim frm as new YourForm()
frm.ShowDialog()
if frm.MyState = TheyClickOnButton Then
' Run your mailItem code here
End If
in the form code
Public Enum State
Unknown = 0
TheyClickedOnButton
End Enum
Public Property MyState as State
Private Sub ButtonGenEmail_Click(sender As Object, e As EventArgs) Handles ButtonGenEmail.Click
MyState = TheyClickedOnButton
Me.Close()
End Sub

Related

Can i add custom icon to "outlook" item?

I am trying to add a custom icon to outlook items (inbox items list) using officejs addon for outlook.
If this is not possible with officejs then how can I achieve this using Exchange service or with any other tool or library?
You can change the icon, however, you have a choice of icons. You would need to use C# or VB.NET (eg VSTO Outlook-addin) or VBA.
I could not find a list of icon values you can use but here is an image of an old list - in case the link gets lost.
Source of the image and also partly answering your question.
To change the icon you need to use the MailItem.PropertyAccessor
Couple of Examples Const (these are hex values but you can use a Long as well)
Const OL_PHONE = &H15D
Const OL_GROUP = &H202
Const OL_NOTE = &H1BD
Const PR_ICON_INDEX As String = "http://schemas.microsoft.com/mapi/proptag/0x10800003"
Using the below helper methods
'use the Get to see the value of an icon
'prior to this code you would need to get a reference to an Outlook mailitem
Dim res As New Object
GetExtendedPropertyValue(oMailItem, PR_ICON_INDEX, res)
'check the value of res or call Hex(res) to see its hex value
'here you can set the icon eg OL_GROUP etc
SetExtendedPropertyValue(oMailItem, PR_ICON_INDEX, OL_PHONE)
Couple of helper methods I have made
Private Function GetExtendedPropertyValue(ByVal aMailItem As Outlook.MailItem, ByVal aProperty As String, ByRef res As Object) As Boolean
Dim oPropAcc As Outlook.PropertyAccessor = Nothing
Try
oPropAcc = DirectCast(aMailItem.PropertyAccessor, Outlook.PropertyAccessor)
res = oPropAcc.GetProperty(aProperty)
Return True
Catch ex As System.Exception
'Put your own logging here
Finally
If Not oPropAcc Is Nothing Then
Marshal.ReleaseComObject(oPropAcc)
oPropAcc = Nothing
End If
End Try
Return False
End Function
Private Function SetExtendedPropertyValue(ByVal aMailItem As Outlook.MailItem, ByVal aProperty As String, ByVal value As Integer) As Boolean
Dim oPropAcc As Outlook.PropertyAccessor = Nothing
Try
oPropAcc = DirectCast(aMailItem.PropertyAccessor, Outlook.PropertyAccessor)
oPropAcc.SetProperty(aProperty, value)
Return True
Catch ex As System.Exception
'Put your own logging here
Finally
If Not oPropAcc Is Nothing Then
Marshal.ReleaseComObject(oPropAcc)
oPropAcc = Nothing
End If
End Try
Return False
End Function
These three examples look like this
The PR_ICON_INDEX PidTagIconIndex Canonical Property can be found here and note that they do say
This property, if it exists, is a hint to the client. The client may ignore the value of this property.
However this does not seem to be the case.
Of course the icon change will not be permanent. If the user forwards the emails or replies then it would be changed.
EDIT
By the way here is a nice way to find out the possible icons. Make a temp folder and copy a rubbish email into this folder 2048 times. Then run this code
Public Sub PrIconIndex()
USE THIS MACRO WITH CARE!
Dim objItems As Items
Dim mail As MailItem
Dim i As Integer
Set objItems = Application.ActiveExplorer.CurrentFolder.Items
For i = 1 To 2048
Set mail = objItems(i)
Call mail.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", i)
mail.Body = CStr(i)
mail.Save
Next
End Sub
The above come from this forum link
Currently the feature to add a custom icon to outlook items is not there. We track Outlook add-in feature requests on our user-voice page. Please add your request there. Feature requests on user-voice are considered, when we go through our planning process.

cannot reference main form code

I have 3 user controls and a main form and I can reference the user controls and use methods from the main form just fine:
Dim ReviewPanel As New ReviewPanel
Controls.Add(ReviewPanel)
ReviewPanel.BringToFront()
ReviewPanel.Go(-8, 0)
But referencing from the user control to the main form doesn't work properly:
Private Sub SelectionPanelClick(sender As Object, e As EventArgs) Handles MyBase.Click, picCover.Click, lblTitle.Click
Dim _Main As New Form1
_Main.DisplayReview()
End Sub
The code still runs fine; I can add messageboxes in the main form method and they'll still show up. However, nothing visually happens like a label won't update with the username.
I've tried to solve it and find workarounds but nothing is working.
Use the ParentForm() property and cast it to Form1:
' ... in your UserControl ...
Dim frm As Form = Me.ParentForm
If Not IsNothing(frm) AndAlso TypeOf frm Is Form1 Then
Dim f1 As Form1 = DirectCast(frm, Form1)
f1.DisplayReview()
End If

Form1.button2.text.. is not accessible in this context because its private

What I would like to do is after clicking the button14 which is login it would change all the button in all forms text to "sample" I already change the class where the button2 is from private to public but I still get an error
Public Sub button14_Click(sender As Object, e As EventArgs) Handles button14.Click
If textBox2.Text = "sample" And textBox3.Text = "****" Then
Form1.Show()
Me.Hide()
button2.Text = ("sample")
Form1.button2.Text = ("sample")
Else
MsgBox("Sorry, Username or password not found", MsgBoxStyle.OkOnly, "Invalid")
textBox2.Text = " "
textBox3.Text = " "
End If
End Sub
this is form 1
Public Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
Login.Show()
Me.Hide()
End Sub
Do people still write VB code? Anyway, the error message is quite clear. You need to make the button public (not just the class).
But, for security reasons, instead of making the button public...I would recommend you to expose a method to change the text of the button, after all, that's all you're doing. You can even mark this method with the Friend access modifier...I think it is the same as internal in C#?
My guess this is winform project.
You can change the access modifier of your button in the form designer. Select your button press F4 for the property grid, find Modifiers and change it to whatever.

visual basic multiple toolstripmenuitems to perform the same on click

I am new here. So apologize in advance if not wording question properly.
I am developing an application (VS2013, Visual Basic) that using multiple menu items in the MenuStrip. When item is clicked the identical function is called - the Tab is created and appropriate form is loaded.
i.e.
Private Sub XXXMNU0039_Click(sender As Object, e As EventArgs) Handles XXXMNU0039.Click
Dim f1 As New frm_B05_01_SalesQuotes
Dim imgnm As String = "XXXMNU0039"
Call XXXTabPages("Sales Quotes", f1, imgnm)
End Sub
Private Sub XXXMNU0040_Click(sender As Object, e As EventArgs) Handles XXXMNU0040.Click
Dim f1 As New frm_B05_03_SalesQuotesReports
Dim imgnm As String = "XXXMNU_Reports"
Call XXXTabPages("Sales Quotes Reports", f1, imgnm)
End Sub
....
I am wondering is there is a way to create a "global" default "on click event" for all menu items that will accomplish the same thing by default. I have all the relevant information for each menu item stored in a table and hope to avoid creating "on click" for each item.
Thanks in advance.
You could have the same handler for the click event of your ToolStripMenuItems.
Just add, after the first Handles XXXMNU0039.Click the event to handle for another ToolStripMenuItem and so on
Oviously, then problem is how to differentiate the various ToolStripMenuItem that calls the same event handler. But in the event arguments there is the Sender object that represent the current ToolStripMenuItem that has called the event.
Just DirectCast to a ToolStripMenuItem and read its name to pass the correct parameter to the XXXTablPages method
Private Sub menuItemHandler_Click(sender As Object, e As EventArgs) _
Handles XXXMNU0039.Click, XXXMNU0040.Click
Dim f1 As New frm_B05_01_SalesQuotes
Dim itm = DirectCast(sender, ToolStripMenuItem)
Dim imgnm As String = item.Name
Call XXXTabPages("Sales Quotes", f1, imgnm)
End Sub

VB6 MDI child form : picturebox invokes Form_Load event

I use several instances (myForm1, myForm2,etc...) of the same MDIChild form (frmChart) to display different MSCharts:
frmMain:
Private Sub Open()
dim myForm1 as frmChart
myForm1.Show
dim myForm2 as frmChart
myForm2.Show
End sub
The problem happens when I try to save the MSChart of one opened instance, because I call a frmChart.SaveChart() function which resizes a picturebox and then the Form_Load() event is invoked, so a new extra frmChart is opened.
frmChart:
Public Sub SaveChart()
picGrapgh.Height = chChart.Height
picGrapgh.Width = chChart.Width
picGraph.Autoredraw = True
picGraph.Picture = picGraph.Image
SavePicture picGraph.picture, FileName
End Sub
When I call that sub, it invokes the Form_Load() of the frmChart. This only happens when I use form instances (myForm1). Once I use any property of the PictureBox control of the frmChart it launches the Form_Load event. How could I prevent it?.
Thank you very much in advance.
Regards,
Ruben
There are 2 problems:
dim myForm1 as frmChart
This just declares that myForm1 will be of the Type frmChart if/when one is created (instanced). To create an actual instance of frmChart:
dim myForm1 as New frmChart
Since myFormN is now an instance of frmChart, you can call those procedures directly on/thru the instance variable:
myForm1.SaveChart

Resources