How to deploy a Macro RRS feed

  • Question

  • I have used Custom Fields in my DOCX file and update them using OpenXML, but custom fields are not getting updated in document.
    So I have write following macro to update field, it run well on my machine, Now i want to deploy it on each machine at client side, the machine count is 500 I can not go on each machine and paste macro code in each file
    what is easy step to do it ? Or when I open word file, will application ask for installing macro ? like addin ?
    How to do it please help

    Here is my macro

    Private Sub Document_Open()
       Dim aStory As Range
       Dim aField As Field
       For Each aStory In ActiveDocument.StoryRanges
          For Each aField In aStory.Fields
          Next aField
       Next aStory
    End Sub

    Thanks in advance for help
    Tuesday, June 16, 2015 12:57 PM


All replies

  • You could save your docx file in the docm format and just add the macro to it.

    BTW, a more efficient macro would be:

    Sub Document_Open()
    Application.ScreenUpdating = False
    With ActiveDocument
    End With
    Application.ScreenUpdating = True
    End Sub

    Paul Edstein
    [MS MVP - Word]

    Tuesday, June 16, 2015 1:08 PM
  • Hi Prasad

    <<I have used Custom Fields in my DOCX file and update them using OpenXML, but custom fields are not getting updated in document.>>

    Which approaches have you tried with the Open XML?

    And which kinds of fields are you using?

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 16, 2015 6:35 PM
  • Thanks for the reply Paul Edstein, Do I need to put that docm on each machine ? How to deploy it ?
    Thursday, June 18, 2015 5:38 AM
  • I have used DOCPROPERTY in my docx file the type of field is STRING.

    I have set

    <w:updateFields w:val="true" />

    in DOCX using OpenXML but when I open document it prompt me "This document contains links that may refer to other files". Now if user click on NO then it will not update field in document. How to supress the prompt ? Or is there any other way to do it ?

    I can do it using MACRO but I need to deploy it on each machine.

    Please suggest



    Thursday, June 18, 2015 5:51 AM
  • VBA macros are not designed for distributing on multiple PCs. You need to develop an add-in if you want to install the solution on multiple machines. See Walkthrough: Creating Your First Application-Level Add-in for Word to get started quickly. 
    • Marked as answer by L.HlModerator Wednesday, July 1, 2015 9:56 AM
    Thursday, June 18, 2015 6:29 AM
  • Hi Prasad

    No, this message appears by design and cannot be surpressed - it's a security thing.

    If you have Word Automation Services available you could open the document using that, which will force the update (without a message).

    Another possibility is for you to write the initial value to the field result as part of your document assembly.

    Macro discussion: Since this macro should only be used with this particular DOCX what you'd need to do is include the VBA code as part of your document (meaning you'd have to change the document type to docm), so that it travels with the document. But you do need to be aware that this could trigger security alerts or, depending on how Word is configured, the macro might not even be allowed to run.

    Or, if this document is for a corporation it might be possible for you to associate the document(s) with a template, stored in a network location, and have the macro in the template. It would also be possible to have IT distribute the template to the individual machines via the network...

    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, June 18, 2015 5:24 PM
  • Hello Cindy

         Thanks for your nice reply, I have  created DOCM file and paste macro in it and then copy that file on another machine and check with ALT+F11 but still macro is not present in document, it shows on my machine only.

    I have check Macros are enabled on that machine.

    How to travel macro with document ?

    Please help



    Friday, June 19, 2015 5:38 AM
  • If the macro is for use in only that particular document, then that is the only document it needs to be added to. Note that, having added the macro to the document, it must be saved in the doc or docm formats, otherwise it will not be saved with the document; docx format files cannot contain macros.

    If multiple copies of the document are required, it would be best to store the macro in a custom template (dotm or dot format) stored in a location where it will remain accessible for anyone using that document.

    Paul Edstein
    [MS MVP - Word]

    Friday, June 19, 2015 6:29 AM
  • Hi Prasad

    You need to provide more detail on how you create the DOCM file and then "paste the macro in it. Are you trying to do this with Open XML or...?

    Have you actually tested whether the macro works on your development machine when you open the document?

    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, June 20, 2015 6:06 PM