Outlook form question

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Outlook form question

Post by fergusfog » Wed Jun 08, 2011 1:17 am

Can somebody help me with what should be a simple bit of code (I bet they all say that!)?The current code I have is below. The problem is, when somebody opens the original form, it fires the routine again and creates another form in the destination folder. I would like it to not run any code in this case; only when the item is created.Thanks in advance!Fergus.Function Item_Write()On Error Resume NextSet olns = Item.Application.GetNamespace("MAPI")Set myFolder1 = olns.GetDefaultFolder(18)Set myFolder2 = myFolder1.Folders("Folder1")Set myFolder3 = myFolder2.Folders("Folder2")Set myFolder4 = myFolder3.Folders("Folder3")Set myItem = myFolder4.Items.Add("IPM.Post.StudentAbsence")myItem.Subject = Item.SubjectmyItem.SaveSet CC = Item.UserProperties("Cc")Set myApp = CreateObject("Outlook.Application")Set myItem = myApp.CreateItem(0)With myItem .To = CC .Subject = Item.Subject .Body = Item.BodyEnd WithmyItem.SendEnd Function

User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Outlook form question

Post by fergusfog » Wed Jun 08, 2011 1:17 am

Can somebody help me with what should be a simple bit of code (I bet they all say that!)?The current code I have is below. The problem is, when somebody opens the original form, it fires the routine again and creates another form in the destination folder. I would like it to not run any code in this case; only when the item is created.Thanks in advance!Fergus.Function Item_Write()On Error Resume NextSet olns = Item.Application.GetNamespace("MAPI")Set myFolder1 = olns.GetDefaultFolder(18)Set myFolder2 = myFolder1.Folders("Folder1")Set myFolder3 = myFolder2.Folders("Folder2")Set myFolder4 = myFolder3.Folders("Folder3")Set myItem = myFolder4.Items.Add("IPM.Post.StudentAbsence")myItem.Subject = Item.SubjectmyItem.SaveSet CC = Item.UserProperties("Cc")Set myApp = CreateObject("Outlook.Application")Set myItem = myApp.CreateItem(0)With myItem .To = CC .Subject = Item.Subject .Body = Item.BodyEnd WithmyItem.SendEnd Function

User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Outlook form question

Post by fergusfog » Wed Jun 08, 2011 2:49 am

Hi jvierra, and thanks for your reply. Sorry, I should have given more info... This is the only code within a custom Outlook form. It is intended to take only the subject line from the post and create another post in a public folder, as well as send a message to anyone specified as CC. I'm looking for a way to run the code only when the item is first created (i.e. not when an existing post item is opened and edited). This is code I have cobbled together myself with help along the way. Does this make sense? Ferg.P.S. I can't get to the MS Office forums for some reason.

User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Outlook form question

Post by fergusfog » Mon Jun 13, 2011 4:21 am

Cheers jvierra, The code you posted is a bit confusing for me, but I understand about the event. So I guess you're saying there is no way to get the code to know if you are opening or creating the item? That seems pretty fundamental! Otherwise, what event could I use without re-writing all the code in VBA or something? F.

User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Outlook form question

Post by fergusfog » Mon Jun 13, 2011 4:33 am

What I posted is ALL of the code in the form. I didn't make my 1st para clear; what I meant is that there seems to be no "on_Open" event or similar; that would make it easier for someone like me!
fergusfog2011-06-13 11:34:09

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Outlook form question

Post by jvierra » Mon Jun 13, 2011 5:13 am

A little checking shows that it would be possible to do this with VBA but it can only be done as you want by detecting a field in the custom form. The VBScript used in forms events is very limited on purpose. It i only intended to be used for things like local validation and, for security reasons I believe, is very much restricted.


User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Outlook form question

Post by jvierra » Mon Jun 13, 2011 5:22 am

Another thing that may beconfusign you. The code you posted is for a custom form template and not for a form. If you wre to use a VBA form that was launched directly from a macro you would have pretty much complete control overy what is happening.

MOst users do not try to program Outlook. This is a pretty advanced ecentdriven environment that frustrates even experienced programmers. If you are not technical with OUtlook then yuo might consider hiring a consultant to do this for you.

Ther eis an Outlook SDK that has many examples that would be helpful for learning. There are alos a ouple of good books available for learning how to program Outlook.

I haven't scripted Outlook for a couple of years since it is now much easier to customize Outlook with Visual Studio.

http://www.amazon.com/Microsoft-Outlook ... 1555582869

Most of the listed books will get you started with Outlook development and help to clear the confusionaround the Outlook model. Sue Mosher has been programming Outlook with VBScript and VBA for many years.


User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Outlook form question

Post by jvierra » Mon Jun 13, 2011 5:29 am


So, if I set a value on the item when it's saved ("alreadyPresent" for example), I can check for this before the code does anything..? Something like:


That might work although I still don't understand. I would jave used a VBA global function to do all of teh work and just called it from teh script.

Remeber that not all variables are accesible from script and some are available read-only I believe.

The VBScript is generally just used for validation. VBA can do much more and can easily inspect more areas of OUtlook.

In a template you are 'sandboxed' which is why it will only use vbscript.

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Outlook form question

Post by jvierra » Mon Jun 13, 2011 5:51 am

In many ways VBA is easier because the VBA editor has full 'Intellisense' and teh vbscrip edito does not. WIth this it is easier to discove rthe object properties. The editor also has a dropdown that lets you insert teh event code template when needed.

How you would approach this is all dependent on your design and model. If you just want someone to crete an item in a folder then the VBA approach is more powerful although you would likely need to design the complete form. Just think of it as a VB form. All events and properties are availbale in the designer just like in VB6. It is also possible to launch a new form from a temlpate but you will still not have direct access to teh forms events. In that case you would want to manage the form with an inspector and get notified in teh inspector when teh form is being saved. With this method you would know that the form is new or saved because you will have managed the launch.

All of this may be even too difficult for many Outlook programmers who are not really technical programmers. It is all bound up in the nuances of teh Outlook object model. This is one reason why most programmers use the add-in approach with Visual Studio.

If you need to do this more often than once than I recommend getting the books. Post back with any technical questions.

By the way. Your explanation doesn't really explain the processing rules or, put another way, the workflow.
jvierra2011-06-13 12:51:56

Locked