none
Outlook 2007 Script to Run VBA Macro RRS feed

  • General discussion

  • 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 CommandButton5_click
     call module7.macroname()
     end sub
     
    and there is error "object module7 required"
     
    and then I did:
     
    sub CommandButton5_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 and it runs the area of the script and runs the macro the script refers to?

    Monday, August 5, 2013 12:24 AM

All replies

  • Any thoughts from others tonight please?
    Tuesday, August 6, 2013 2:19 AM
  • Hi lmseaz.
    It seems no way to call a macro from form script code.
    However You can create a fuction has the same action in your script code.
    See following example:
    Sub CommandButton5_click
        call test
    end sub
    
    sub test()
    
        msgBox “hello world”
    
    end sub
    If you still have errors, please let me know.

    George Hua
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 7, 2013 2:47 PM
    Moderator
  • Thanks...where do I create a function please?
    Wednesday, August 7, 2013 4:23 PM
  • 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
  • Hi Imseaz.

    In your defined contact form, click view code.The script editor will be shown, this is a VBscript editor.

    You could put commandbutton click event and macro here,publish it as your default form.

    Then when you click the commandbutton in the contact form, the fuction will run.

    I provide the below link for you to refer:

    http://msdn.microsoft.com/en-us/library/office/ff866927.aspx

    Hope will help you.


    George Hua
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Thursday, August 8, 2013 3:57 PM
    Moderator
  • When I use the following funcion in the script code, in the sub test, I need it to run the macro from the module that is created, and when I just type in the macro name, that does not work....how do I refer to a macro in a module or a macro in the OutlookSessions area?

    Thanks very much

    Saturday, August 10, 2013 1:12 PM
  • I found the following way to fixt the problem and if others have other thoughts, happy to hear back.  The words runthis is the name of a macro in the OutlookSession and I added the word Application. before it and that did it......I learned that concept as well from Ken Slovak online....and I will see if this never stops working re running codes 

    Sub CommandButton5_click

        call test

    end sub

    sub test()

         Application.runthis()

    end sub

    Saturday, August 10, 2013 2:13 PM
  • To All,

    I did what I showed above, it ran the macros etc....and then I got the same error...and it was not working.....

    I have been told that if another code is running somehow, that gets in the way of this.....is there an easy way to clean this up when it happens?

    Saturday, August 10, 2013 3:45 PM