Link to home
Start Free TrialLog in
Avatar of Infinity Solutions
Infinity SolutionsFlag for United States of America

asked on

Is it possible to assign 2 validation formulas to one field in an outlook form?

Basically we would like the subject field to require a number and but also allow text.  We do not want the email to be able to be sent unless there is a number somewhere in the subject field.

We are currently using the formula isnumeric ([Subject])=True to require the number but cant quite figure out how to allow text as well.  I figure there is a way to use an 'and' statement or even an alphanumeric variable that we may not be aware of.

This is our first go at getting further into outlook so any help is appreciated!
ASKER CERTIFIED SOLUTION
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Infinity Solutions

ASKER

Thank you, I will test this out shortly.  I assume this is input into the VBA project within outlook correct? Under ThisOutlookSession or Module1?

Edit:  Nevermind, I think I got it by putting it under 'thisoutlooksession'! I will do some more testing and report back.  Thank You!
It seems to work well.  I just noticed though that this now runs on every email and not just the form I created.  Is there a different section to put the code for it to run only on my form?
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Let me do some testing ~
do you have an OK button on your form to do the sending?  If so, that is where the code would go.  If you post the code behind your form, I will put this in there (not Cancel = True though) ~

in the design view of the form, right-click on the form and choose View Code
There is no OK Button, just the default send one.  I just used the default message form template when designing my new form.  Nothing really needed changing but the requirement of the number in the subject field.

When hitting view code in the design view of the form it is blank.
User generated image
Previously I was posting the code you gave me in the 'ThisOutlookSession' section inside a VBA Project/Macro.  That had it working but had it working for every email even the emails not using our form.  Not sure if this was the right place to put it.  
User generated image
I need to know a bit more about your form so I can trigger the code to just check for the form ... perhaps you can publish it and attach it?

from the developer ribbon, select Publish and specify a folder you can find :)

thanks
For your form: do users click one button? all buttons? Is there a button they definitely click?

If so, code could be called from that button to assign a property to the mail item that the Application_ItemSend code I gave you could check for.  If the property is found, then the check could be done.  Otherwise, the check will not be done.

To see code for a specific button, go to the design view of the form and double-click on a button.

thanks!
Im not sure exactly how to attach the form exactly, but it is just the message form template when you go to select the form.  It is the default email that comes up when a user hits new email.  I have just altered this and saved it as a separate form/template for our uses. To send the email they would hit the send button.
This is what it looks like when the form is run:
User generated image
and here it is in the design view
User generated image
thanks, John

I need a way to figure out how to distinguish use of the form as opposed to just composing a regular email ...

could you outline the steps you do differently from regular email when you are using the form?

thank you

warm regards,
crystal

~ have an awesome day ~
Sure thing crystal, Thank you for all the awesome help so far.

This form will be used to tag specific emails with a numerical value.  For instance,  Job A(doing the dishes for lack of a better example) will be given a number of 123.  Any email pertaining to this Job A throughout the company will have 123 typed into the subject by the user.  I prefer to use the subject field instead of creating a custom field so that mobile users can see the number as well.  Basically I wanted them to have the option of using the custom form that requires the number, and the ability to use the default mail form(That doesn't require a number) for other personal emails.

If this ends up not being possible that may be ok.  We will just have to inform them that a personal email code number that doesnt pertain to a job will need to be input.  If this happens to be the case, am I placing the code within the VB project in the correct place? The picture I attached a few posts earlier shows where I currently have it.
Hi John,

thank you -- and yes, you did put it in the right place.  However, what we are looking for is some sort of trigger to get OUT of that code when it is just a regular message ... something like this:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
   if not triggerCondition = true then 
      exit sub
   end if

Open in new window

What I am looking for is a way to construct the triggerCondition so the code knows the difference between a regular email and an email you want tested.

warm regards,
crystal

~ have an awesome day ~
Could it be something including the form name/file path?




also I have to 'enable all macros' in the macro settings for outlook to run the original VB code you sent me. Is that correct?
Hi John,

I might be able to cycle through the ribbon tabs and see if the form ribbon is open ... what is its name?

yes, macros will have to run -- you can see if "Notification for all macros" will work as well.

warm regards,
crystal

~ have an awesome day ~
ok thanks :) it said not recommended next to enable all macros so I was just making sure I was doing it correctly/safely.

As for the name of the form i just used subjectjobform.oft when saving as a template.    And have moved the filepath around from the root of C: to the actual Templates folder messing around with things.  The actual form name in my personal forms library is a little different.
I think what is needed is the name of the ribbon tab that it creates since that is something that shows.

thanks, John

warm regards,
crystal

~ have an awesome day ~
Hi John,

I was hoping to do some more research on this today but it got too late ... I will look again tomorrow ~ or maybe someone else will jump in too

warm regards,
crystal

~ have an awesome day ~
No problem at all crystal.  It had gotten late for us too. We appreciate any help!


But back on topic, I think I can change/make the ribbon buttons and stuff anything you wish.
Hi John,

the best solution would be for your template to add a custom property that can be checked.  Otherwise, I do not see a way to know if the template is being used.  Here is a reference link:

https://www.add-in-express.com/creating-addins-blog/2011/08/19/how-to-add-a-custom-property-to-the-userproperties-collection-of-an-e-mail-item-in-outlook/

Application_ItemSend could then check and see if the mail item has this property.  If so, the check can be done.

warm regards,
crystal

~ have an awesome day ~
Thank you, Ill take a look.  I will go ahead and mark your initial code as a solution as i think it will work ok as is!

One more quick question if you don't mind.  Is there some code to also pre fill the subject line with 'Job Number:' ?
I know you can do it in the properties of the field on a custom form, but im now trying to get it all to work without needing a form at all, and to just use the default new email template.

I can resort to the form and making a custom ribbon button/macro if needed.

Thank You Crystal!
Hi John,

thanks and you're welcome ~

I don't know how your template works, but here is code to fill outlook stuff from VBA:

sub Procedurename() '------------ customize
'151118 strive4peace
   on error goto Proc_Err

   Dim outApp As Object
   Dim outMsg As Object
   
   Set outApp = CreateObject("Outlook.Application")
   
   with outMsg 
      '.Importance = olImportanceHigh
       .To = sEmail
       '.CC = "cc email address"
       '.BCC = "cc email address"
        .Subject = "Your subject line"
        .Body = "Your message"
       ' .Attachments.Add sPathFile
       ' If you want to edit the message then uncomment Display and comment Send
       '.Display
        .Send  
   end with         
   
Proc_Exit:
   On Error Resume Next
   'release object variables
   Set outMsg = Nothing
   Set outApp = Nothing
   Exit Sub
  
Proc_Err:
   MsgBox Err.Description, , _
        "ERROR " & Err.Number _
        & "   Procedurename" 'CUSTOMIZE

   Resume Proc_Exit
   Resume
End Sub

Open in new window


obviously, if you are running this from outlook, you won't need outApp

warm regards,
crystal

~ have an awesome day ~
Sorry template is probably not the best way to say it.  I'm just using the default method to compose emails in outlook.  No custom form or anything other than the code you gave me.

As for the new code.
Im getting an error 91 Procedurename when running the project.

Sorry im not much of a programmer haha. I'm guessing something has to be defined in the () after it or somewhere else at least?
Hi John,

not sure where you put the code ... it should probably go into a general (standard) module.  Also, if you are putting it IN outlook, then don't use outapp so the top lines are:

   Dim outMsg As Object
   with outMsg 

Open in new window

and delete:
Set outApp = Nothing

Procedurename is just a placeholder for whatever you want to call it ...

*** How to Create a Standard (General) Module ***

Press Alt-F11 to go to the VBE (Visual Basic Editor)

From the menu in a the Microsoft Visual Basic window:
Insert --> Module

once the code is in the module sheet, from the menu, do -->
Debug, Compile

if there are no syntax/reference errors, nothing will appear to happen -- in this case, nothing happening is good <g>

Make sure to give the module a good name when you save it.  You can have several procedures (Subs and Functions) in a module, which gives you a way to categorize them ... ie: basic procedures that would be useful in any database; procedures that are specific to a particular database; procedures for converting data; etc

~~~~~ also be sure to use Option Explicit at the very top of each module so variables that are not declared or are misspelled will be picked up
Option Explicit ' require variable declaration

Open in new window


warm regards,
crystal

~ have an awesome day ~
I was orginally placing the code above the original(for requiring a number) in 'ThisOutlookSession'.

I have moved it into its own module as shown below:
User generated image
and it pops this error:
User generated image
oops! I left out a line when needs to be after DIM and before WITH
Set outMsg = outApp.CreateItem(0) '0=olMailItem

Open in new window


also, you should rename the procedure ;) ~