none
Run Word Macro - Office App Javascript API RRS feed

  • Question

  • hi friends,

    I am trying to run a macro from my button.

    I have looked at the documentation online but no where does it show me how to run a macro.




    Does any one have a sample code to show me how I can attach a simple macro to a button.
    So when i click it the macro runs

    example macro stored in file "macros.docm"

    or i can put the macro  in normal template

    Sub Hello()

    MsgBox "Hello World"

    End Sub

    I need to add this to the java script file - but how exactly - the onclick function?


    Cheers Dan :)

    Monday, August 8, 2016 8:52 PM

Answers

  • Hi Dan,

    >> I am trying to run a macro from my button.

    Do you mean you want to run macro from Office javascript API? If so, as far as I know, it is impossible. Office AddIn API did not provide such function.

    If this is a new feature you want to include in the future, I suggest you submit a feedback in the link below:

    http://officespdev.uservoice.com

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Dan_CS Saturday, August 13, 2016 3:19 AM
    Tuesday, August 9, 2016 6:42 AM

All replies

  • Hi Dan,

    >> I am trying to run a macro from my button.

    Do you mean you want to run macro from Office javascript API? If so, as far as I know, it is impossible. Office AddIn API did not provide such function.

    If this is a new feature you want to include in the future, I suggest you submit a feedback in the link below:

    http://officespdev.uservoice.com

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by Dan_CS Saturday, August 13, 2016 3:19 AM
    Tuesday, August 9, 2016 6:42 AM
  • Hello Edward,

    I am trying to run a vba macro.

    I found this

    http://www.codeproject.com/Questions/168103/Run-method-for-excel-macro-in-javascript

    I would like to run a word vba macro when i press my button


    Cheers Dan :)

    Tuesday, August 9, 2016 11:44 AM
  • Hi Dan,

    The function in the link is not built-in function in Office.js. It uses an ActiveXObject to create Excel object, and I would not suggest you to do this. This function might be blocked by IE settings. The link below might be useful to you.

    # Javascript Excel OpenFile

    http://stackoverflow.com/questions/15246837/javascript-excel-openfile

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, August 10, 2016 5:14 AM
  • Hello Edward,

    this is a good link.

    I did follow the instructions but no file opened, or excel activated

    OK i will keep looking for info on this and testing


    Cheers Dan :)

    Wednesday, August 10, 2016 1:43 PM
  • Hi Dan,

    I made a test with this. In a simple html page, it works. But, if I test in Office add-ins, it will get error “Automation server can't create object”. I assume you could not use this in Office add-ins. For this function, I would suggest you submit a feedback.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, August 11, 2016 8:00 AM
  • Hi Edward,

    in my home.html this is the code

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <title>Boilerplate text app</title> <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.1.4.min.js"></script> <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script> <script src="home.js" type="text/javascript"></script> </head> <body> <div> <h1>Welcome</h1> </div> <div> <p>This sample shows how to add boilerplate text to a document by using the Word JavaScript API.</p> <br /> <h3>Try it out</h3> <button id="Dan"> Dan's Button </button> <br><br> <button id="emerson">Add quote from Ralph Waldo Emerson</button> <button id="checkhov">Add quote from Anton Chekhov</button> <button id="proverb">Add Chinese proverb</button> </div>

    <form name="form1"> <input type=button onClick="test()" value="Open File"> <br><br> </form> <script type="text/javascript"> function test() { var Excel = new ActiveXObject("Excel.Application"); Excel.Visible = true; Excel.Workbooks.Open("C:\\Users\\Dan\\Desktop\\a.xlsm") //Excel.Workbooks.Open("C:\Users\Dan\Desktop\a.xlsm") } </script> <h3><div id="supportedVersion"/></h3> </body> </html>


    I opened it in EDGE , and clicked open file button it did not work, neither in firefox.

    is this the correct code?


    Cheers Dan :)

    Friday, August 12, 2016 12:49 PM
  • Hi Dan,

    As my above reply, it did not work in Office Add-ins. For a simple web page, you need to open it by IE.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Saturday, August 13, 2016 2:40 AM
  • Hello Edward,

    This is very very strange, it did not work in Firefox, or EDGE,

    I opened it in Internet Explorer IE as you suggested now, and lo and behold it finally worked.

    Ok so i guess i will have to wait a couple of years untill it finally works in office 2016

    I still need to work out how to run the macro, but i guess thats for another day

    :)


    Cheers Dan :)

    Saturday, August 13, 2016 3:19 AM