none
Outlook 2007 Script Codes RRS feed

  • General discussion

  • I was talking to microsoft suport and they told me to post this as someone could answer right away.

    I found these two different ways to have a commandbutton in a contact form run a code from the script that runs a macro from a module in the VBA area. And the words macroname is the name of the macro and the module7 is the module number the macro is in

    sub CommandButton6_click
    call module7.macroname()
    end sub

    and there is error "object module7 required"

    and then I did:

    sub CommandButton6_click
    macroname
    end sub

    and the errror is "type mismatch macroname"

    Any way to fix this so I add a command button to a contact form script and it runs the the macro the script refers to as I need this very heavily!!

    Wednesday, August 7, 2013 2:06 AM

All replies

  • A form code script can't access code in the Outlook VBA project under normal circumstances.

    You can use an unsupported method if your code is in the ThisOutlookSession class module and call your method as Application.macroname() only if you are using Outlook 2007 or earlier.

    That unsupported way of calling code in the Outlook VBA project broke in Outlook 2010 and 2013. It won't be back.

    An alternative would be to simply translate your macro code from VBA code to VBScript code and include it in your form's code.


    Ken Slovak MVP - Outlook

    Wednesday, August 7, 2013 1:29 PM
    Moderator
  • Are there standard things that you just change the code from the Module to put in the Script including the ability to click on a commandbutton in the contact?

    Thanks

    Wednesday, August 7, 2013 4:26 PM
  • Visual Basic for Applications Features Not in VBScript: http://support.microsoft.com/kb/190273/en-us

    VBScript Features not in Visual Basic for Applications:

    http://www.csidata.com/custserv/onlinehelp/VBSdocs/vbs236.htm

    Also, any Outlook constants won't be recognized unless they are explicitly declared in the VBS code or the enumerated values are used instead of the object model names.


    Ken Slovak MVP - Outlook

    Wednesday, August 7, 2013 7:40 PM
    Moderator
  • This is what I put in TheOutlookSession area.....and the second line is a macro name from a Module so when I run the code from TheOutlookSession area it runs the Macro of the Module, which opens up a combobox from a userform and gives me list of things from the combobox to decide to put in or run from the contact.


    Public Sub runthis()
    macroname
    End Sub


    Then I added a commandbutton5 to the Contact form with a caption name that shows what I want to do, and here is what I put in the Script area of the Contact form, and published it as my default form.


    Sub CommandButton5_Click
    Set myOlApp = CreateObject("Outlook.Application")
    myOlApp.runthis
    End Sub


    And when I click on the CommandButton5 in any Contact, it runs the macro that was put in TheOutlookSessionArea

    And then, even though it was working perfectly, and I did not change anything, it just stopped working....so what do I fix please?

    Thursday, August 8, 2013 2:34 AM
  • Never use CreateObject() or New() to create an Outlook Application session when you're running in form code or Outlook VBA code. Outlook only support one session running at a time. The handle you get from CreateObject() or New() is not trusted and may or may not run code depending on the code settings.

    Why not just do what I told you to do? Use Application.macroname()  if you must use an unsupported method that no longer works and was broken in Outlook 2010. If you ever update to a later version of Outlook you will automatically break your code with no fixes possible.

    What I'd really recommend is doing is the main thing I said to do, put the code and business logic directly in the form code and have done with unsupported methods that no one will want to support for you.


    Ken Slovak MVP - Outlook

    Thursday, August 8, 2013 2:44 PM
    Moderator
  • Thanks again.   Will gave the Applicationl.macroname() a try and it also works perfectly...

    Also, I am posting another question re using macros and appreciate your help...

    Thursday, August 8, 2013 9:11 PM
  • The update is the I changed the code in the script of the contact to:

    Sub CommandButton5_Click
    Application.runthis
    End Sub  

    It runs the macro from the OutlookSession titled as "runthis" but then often times it opens up the textbox from the module and userform, but the entryid does not show up....and then I click on it a second time, and it does show up....

    And if I run the macro re the module/userform from the Quick Access Toolbar, it always runs....so why is there a little stop running from the script one time, and then it works right after that?

    Monday, August 19, 2013 2:31 AM
  • I don't know why it wouldn't work the same each time, but you are using an unsupported methodology that doesn't work at all with Outlook 2010 and later, so I'm not sure that anyone would able to support you on this.

    Ken Slovak MVP - Outlook

    Monday, August 19, 2013 2:58 PM
    Moderator