none
how to cancel closing an item with vbscript RRS feed

  • Question

  • I'm trying to test a field's value in vbscript in a custom Outlook form (Outlook 2007) using the code below:

    function Item_Close(Cancel)	
    	if len(Me.Subject) > 3 then
    	        Me.UserProperties("Locked") = vbNullString
    		Me.Save
    	else
    		msgbox "The CallerName must be longer than 3 characters.", vbInformation, "Invalid CallerName"
    		Cancel = true
    	end if
    end function
    I'm getting the pop-up msgbox ok. But then the item closes anyway.

    How can I prevent the item from closing so the user has a chance to correct the problem I've just told them about?


    Gordon Prince * (901) 761-3393

    Saturday, June 9, 2012 2:04 PM

Answers

  • For Outlook form code the Close function actually looks like this, which you see if you use the Script menu tab to insert the function:
     
    Function Item_Close()
     
    End Function
     
    To cancel the close you need to set the function = False:
     
    Function Item_Close()	
    	if len(Me.Subject) > 3 then
    	        Me.UserProperties("Locked") = vbNullString
    		Me.Save
    	else
    		msgbox "The CallerName must be longer than 3 characters.", vbInformation, "Invalid CallerName"
    		Item_Close = False
    	end if
    End Function
    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Gordon Prince" <=?utf-8?B?R29yZG9uIFByaW5jZQ==?=> wrote in message news:6ca2d32f-e1d1-4949-ae40-ef0e202b5af7...

    Removing the msgbox statement just closed the item.


    Gordon Prince * (901) 761-3393


    Ken Slovak MVP - Outlook
    • Marked as answer by Gordon Prince Wednesday, June 13, 2012 2:24 PM
    Tuesday, June 12, 2012 4:06 PM
    Moderator

All replies

  • could you try adding ByRef for Cancel parameter declaration?
    Saturday, June 9, 2012 3:20 PM
  • Added ByRef, no change.

    Gordon Prince * (901) 761-3393

    Saturday, June 9, 2012 3:27 PM
  • Try adding: 

    Item_Close = False

    Sunday, June 10, 2012 10:50 AM
  • Does it cancel if you don't display the message box?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Gordon Prince" <=?utf-8?B?R29yZG9uIFByaW5jZQ==?=> wrote in message news:16ffc53b-30bd-470a-bad4-4e926f4d255e...

    I'm trying to test a field's value in vbscript in a custom Outlook form (Outlook 2007) using the code below:

    function Item_Close(Cancel)	
    	if len(Me.Subject) > 3 then
    	        Me.UserProperties("Locked") = vbNullString
    		Me.Save
    	else
    		msgbox "The CallerName must be longer than 3 characters.", vbInformation, "Invalid CallerName"
    		Cancel = true
    	end if
    end function
    I'm getting the pop-up msgbox ok. But then the item closes anyway.

    How can I prevent the item from closing so the user has a chance to correct the problem I've just told them about?


    Gordon Prince * (901) 761-3393


    Ken Slovak MVP - Outlook
    Monday, June 11, 2012 3:16 PM
    Moderator
  • Adding Item_Close = False didn't make any difference.


    Gordon Prince * (901) 761-3393

    Tuesday, June 12, 2012 12:09 PM
  • Removing the msgbox statement just closed the item.


    Gordon Prince * (901) 761-3393

    Tuesday, June 12, 2012 12:10 PM
  • For Outlook form code the Close function actually looks like this, which you see if you use the Script menu tab to insert the function:
     
    Function Item_Close()
     
    End Function
     
    To cancel the close you need to set the function = False:
     
    Function Item_Close()	
    	if len(Me.Subject) > 3 then
    	        Me.UserProperties("Locked") = vbNullString
    		Me.Save
    	else
    		msgbox "The CallerName must be longer than 3 characters.", vbInformation, "Invalid CallerName"
    		Item_Close = False
    	end if
    End Function
    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Gordon Prince" <=?utf-8?B?R29yZG9uIFByaW5jZQ==?=> wrote in message news:6ca2d32f-e1d1-4949-ae40-ef0e202b5af7...

    Removing the msgbox statement just closed the item.


    Gordon Prince * (901) 761-3393


    Ken Slovak MVP - Outlook
    • Marked as answer by Gordon Prince Wednesday, June 13, 2012 2:24 PM
    Tuesday, June 12, 2012 4:06 PM
    Moderator