Focus on the userform

Status
Not open for further replies.
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
I have created a VBA Macro which uses the ItemSend Even to modify the Subject of an email. Form within ItemSend I call a userform (ExampleForm.Show vbModal)with 3 buttons. The userform works perfectly well and when the user clicks a button the Subject is replaced and the mail is fired. However when the user clicks on the Send button and the userform comes up, the user is able to click outside the form on the mail and make changes. How can I focus only on the userform without allowing the user to click elsewhere?
 
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
I did it and it does not work.
I call the ExampleForm.Show vbModal from the ItemSend, when I click on the email's send button the form appears but while the form is opened I can click (outside the form) on the email and make changes.
This is what I try to avoid. I do not want the user to be able to click outside the form.
 
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
The code is as follows:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
strSubject = Item.Subject
If Len(Trim(strSubject)) = 0 Then
prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
If (Left(strSubject, 3) <> "P1-" And Left(strSubject, 3) <> "P2-" And Left(strSubject, 2) <> "P3-") Then
EmailPrioForm.Show vbModal
End If
End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
As mentioned, vbModal does not work. You need to replace that word by the figure 1.
 
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
A I mentioned, I replaced EmailPrioForm.Show vbModal

with EmailPrioForm.Show 1

and it does not work. They have exactly the same behavior.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
How should we know if you show a different code?
Anyway, figured it, instead of calling the form directly by its name, use a variable:
dim fm as new EmailPrioForm
fm.show 1
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Is the code running in Outlook, or in another host like Excel? If Outlook, show the code you're really using.
 
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
Excuse me, but what makes you believe that I'm not showing the code I'm really using...?

Outlook 2007:

ThisOutlookSession:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim strSubject As String
Dim myForm As New EmailPrioForm
strSubject = Item.Subject
If Len(Trim(strSubject)) = 0 Then
prompt$ = "Subject is Empty. Are you sure you want to send the Mail?"
If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check for Subject") = vbNo Then
Cancel = True
End If
End If
If (Left(strSubject, 3) <> "P1-" And Left(strSubject, 3) <> "P2-" And Left(strSubject, 2) <> "P3-" And Cancel <> True) Then
myForm.Show 1
End If
End Sub


EmailPrioForm:
Private Sub CommandButton1_Click()
Dim Item As MailItem
Set Item = Outlook.Application.ActiveInspector.CurrentItem
Item.Subject = "P1-" + Item.Subject
Unload Me
End Sub

' Subject Identifier: Action
Private Sub CommandButton2_Click()
Dim Item As MailItem
Set Item = Outlook.Application.ActiveInspector.CurrentItem
Item.Subject = "P2-" + Item.Subject
Unload Me
End Sub

' Subject Identifier: Decision
Private Sub CommandButton3_Click()
Dim Item As MailItem
Set Item = Outlook.Application.ActiveInspector.CurrentItem
Item.Subject = "P3-" + Item.Subject
Unload Me
End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Because that's often the case, and here the code worked. Now I've copied all your code into my IDE, and sometimes the window is modal, sometimes it is not. I'm sorry, I don't have more ideas.
 
Outlook version
Outlook 2007
Email Account
Exchange Server 2007
Hi,
I actually found the trick to work and it looks preety simple. Just had to initialise the form with a focus to one of its buttons and did the trick:

Private Sub UserForm_Initialize()
Me.CommandButton1.SetFocus
End Sub
 
Status
Not open for further replies.
Top