none
VBA Word Macro Stored in Another Document - Run on Active Document RRS feed

  • Question

  • hi friends,

    I am trying to run a macro on my active document.

    The macro I am trying to run is stored in macros.docm

    I have opened a word document and would now like to run the macro FontSize stored in macros.docm on the current active document.

    It is from a vbscript
    
    Dim Word 
    Dim WordDoc 
    
    Dim oDoc = "ActiveDocument"
    
    Set Word = CreateObject("Word.Application") 
     Word.Visible = False
     Set WordDoc = Word.Documents.open("C:\Users\Dan\Desktop\macros.docm") 
     
    'Run the macro called FontSize on the Active Document
    oDoc.Activate
    Word.Run "FontSize" 
     
    

    I have also tried windows.activate - but that didn't work

    The VBscript runs on the original document - I don't know how to activate the current document and run the macro on the active document.

    any ideas are welcome thank you


    Cheers Dan :)



    • Edited by Dan_CS Friday, October 21, 2016 5:04 PM
    Friday, October 21, 2016 4:58 PM

Answers

  • Save the macro enabled document as a macro enabled template. Temporarily load the template as an add-in. Developer > Document Template > Add - select the template from its current location. The macros it contains will be available to all documents until Word is closed. The entry will be left behind, but unchecked next time Word is started. You can remove the entry if you wish.

    You may be able to do this from the script e.g. in VBA you could add the template as an add-in

    AddIns.Add FileName:="C:\Path\Filename.dotm", Install:=True

    Do stuff

    then uninstall it

    AddIns("C:\Path\Filename.dotm").Delete


    Graham Mayor - Word MVP
    www.gmayor.com


    • Edited by Graham MayorMVP Monday, October 24, 2016 4:15 AM
    • Marked as answer by Dan_CS Monday, October 24, 2016 12:21 PM
    Monday, October 24, 2016 4:10 AM

All replies

  • Hi Dan,

    There is a much easier approach.

    Put your macro FontSize into a module in Normal.dotm.

    Then, in Word, customize the QAT and add a button to run the macro FontSize, and it will be available any time you have a document open.

    Here is a link in case you need it:

    https://support.office.com/en-us/article/Customize-the-Quick-Access-Toolbar-6c616455-653c-451c-93f7-96082e98de4d?ui=en-US&rs=en-US&ad=US&fromAR=1

    Also, you may need to re-write the code of "FontSize" - it may depend on what you recorded and how you edited it.



    Friday, October 21, 2016 5:19 PM
  • Hello Bernie,

    nice to see you.

    Unfortunately its a bit complicated the situation. And its a long story - but the gist is, if i double click on the script above - its a vbs file stored on my desktop. It runs the macro nicely.

    But only on the document specified below :(

    Word.Documents.open("C:\Users\Dan\Desktop\macros.docm

    now there is something missing - i just need to somehow activate the active document, and i tried a number of other things unsuccesfully, I tried activating a sample document with   -  Documents("Test.docx").activate as well  in the vb script 

    I have plenty of macros in my normal template.dotm that work nicely.

    do let me know if you have any other ideas


    Cheers Dan :)

    Friday, October 21, 2016 5:32 PM
  • OK, I did some searching found that you cannot call a procedure in another document, unless that doc is saved as a template in the word STARTUP folder and is added to your global templates. That is why it only runs on the specific file that holds the macro. So you really should just bite the bullet and add your procedure to your Normal.dotm file. Or add the fontsize code to your vbscript - try it this way to get to the active document:

        


        Dim wrdApp As Word.Application
        Dim wrdDoc As Word.Document

        Set wrdApp = GetObject(, "Word.Application")
        Set wrdDoc = wrdApp.ActiveDocument

       'code to set the font size of the selection in wrdDoc

    Friday, October 21, 2016 8:51 PM
  • I wouldn't recommend the Normal.dotm file approach.  I've had endless problems with it getting corrupted or missing for no apparent reason.  Create an addin (.dotm) and add it to the STARTUP folder.  It is much easier to manage and update.
    Saturday, October 22, 2016 4:44 PM
  • Thank you Bernie,

    let me play abut with the VBS /VBA

    I did put it in the normal template which I have to access from the normal document.

    But it doesnt work when i click the vbs file on my desktop

    I' ll do some investigating on the VBS

    :)


    Cheers Dan :)

    Saturday, October 22, 2016 4:54 PM
  • That's true - my normal.dotm just evaporated into thin air!

    Well the macros got vaporised somewhere :)


    Cheers Dan :)

    Saturday, October 22, 2016 4:55 PM
  • The script creates a new Word application. That Word application knows nothing about the active document in the existing Word application. The two applications are not connected in the manner you imagine.

    If Word is already open then you need to get that running Word application. If it isn't already open then there is no active document, so you need to open both documents in the application you have created.


    Graham Mayor - Word MVP
    www.gmayor.com

    Sunday, October 23, 2016 4:24 AM
  • Hi Graham,

    you are spot on I did some investigating and found that one was not aware of the other.

    I wish microsoft would make it easier to create a bridge for issues such as this.

    It makes common sense that some times users may need to use a macro stored in another document on a different document without having to load up a dotm as is per the norm.

    Sometimes you may not want to store a dotm on others computers etc or what ever the issue is.

    I would like to take my macro document with me and do some magic, so its about portability too.

    Ok so I am still trying to find a way to

    Have 2 Documents open

    Document A  -  to apply to

    Document B - where macro is stored

    So hence trying to use VBS to bridge that gap

    I'll keep investigating


    Cheers Dan :)


    • Edited by Dan_CS Sunday, October 23, 2016 12:14 PM
    Sunday, October 23, 2016 12:14 PM
  • Save the macro enabled document as a macro enabled template. Temporarily load the template as an add-in. Developer > Document Template > Add - select the template from its current location. The macros it contains will be available to all documents until Word is closed. The entry will be left behind, but unchecked next time Word is started. You can remove the entry if you wish.

    You may be able to do this from the script e.g. in VBA you could add the template as an add-in

    AddIns.Add FileName:="C:\Path\Filename.dotm", Install:=True

    Do stuff

    then uninstall it

    AddIns("C:\Path\Filename.dotm").Delete


    Graham Mayor - Word MVP
    www.gmayor.com


    • Edited by Graham MayorMVP Monday, October 24, 2016 4:15 AM
    • Marked as answer by Dan_CS Monday, October 24, 2016 12:21 PM
    Monday, October 24, 2016 4:10 AM
  • Hi Graham,

    I think temporarily installing the addin then deleting it from computer would be the best.

    Since I've looked everyhwere and there doesn't seem to be anything else, or what my skills permit.

    You know how folks can be suspicious when you add anything to their computer,

    suddenly it was that program you installed for the malfunction of their mouse :)

    I did learn about command switches where you can run macros - but again it only runs on the same file.


    Thanks for all the replies


    Cheers Dan :)

    Monday, October 24, 2016 12:21 PM