• Transporting m,acros (2002 SP3)

    Author
    Topic
    #410937

    I have created a template that contains 3 macros

    • A New macro that updates fillin macros in the header and then locks them
    • A macro that runs from a MACROBUTTON that does a show/hide toggle on some text in a table
    • an Open macro that also runs the show/hide macro with a parameter that ensures that the text is visible when the document is opened
      [/list]Now, these macros seem to stay in the template whilst the document has a pointer to the template.

      This is fine for the New macro, but if the document is to be sent to someone who does not have the template, it would be useful if the Open and toggle macros were available in the document itself.

      So, my question is: Is there a way to have these macros exist in the document once it is created from the template?

      TIA

    Viewing 7 reply threads
    Author
    Replies
    • #887372

      (Edited by RudiS on 12-Oct-04 13:06. Sorry Paul, I tested it more thouroughly, and noticed that it does not copy the actual macro…It only sets a reference to the macro….PLEASE IGNORE POST!!!)

      Hi Paul,

      I’m not too sure where to move the macros AFTER you have created them, but you can follow these steps to have new macros you record in a template copied automatically to documents that are opened from the template! :

      1. On the template, record a new macro
      2. In the macro dialog, give the macro a name
      3. In the STORE MACRO IN dropdown, choose DOCUMENTS BASED ON Template.dot
      4. Record as usual, and make any changes in the VBA code.

      Any document that is now generated from the template will have the macros available for that documents too!

      • #887379

        Just noticed your extra message.

        I was going to say that, even if it did work, it wouldn’t be any good for the Open macro as that one cannot be recorded.

        Can it be done in VBA in the New macro?

        P.S. I’m glad to see that not all my questions are dumb ones that I should easily have got the answers for if only I had bothered to look for them!

      • #887380

        Just noticed your extra message.

        I was going to say that, even if it did work, it wouldn’t be any good for the Open macro as that one cannot be recorded.

        Can it be done in VBA in the New macro?

        P.S. I’m glad to see that not all my questions are dumb ones that I should easily have got the answers for if only I had bothered to look for them!

    • #887373

      (Edited by RudiS on 12-Oct-04 13:06. Sorry Paul, I tested it more thouroughly, and noticed that it does not copy the actual macro…It only sets a reference to the macro….PLEASE IGNORE POST!!!)

      Hi Paul,

      I’m not too sure where to move the macros AFTER you have created them, but you can follow these steps to have new macros you record in a template copied automatically to documents that are opened from the template! :

      1. On the template, record a new macro
      2. In the macro dialog, give the macro a name
      3. In the STORE MACRO IN dropdown, choose DOCUMENTS BASED ON Template.dot
      4. Record as usual, and make any changes in the VBA code.

      Any document that is now generated from the template will have the macros available for that documents too!

    • #887424

      The following is a bit clunky, but I hope it does what you want.
      – Replace the string constants at the beginning of the code by the appropriate names.
      – Set a reference (through Tools | References… in the Visual Basic Editor) to the Microsoft Visual Basic for Applications Extensibility 5.3 object library; this library enables you to use code to manipulate code.
      – Select Tools | Macro | Security (in Word itself), activate the Trusted Sources tab and tick the “Trust Access to Visual Basic Project” check box. Otherwise, manipulating code in code is not allowed.

      Private Sub Document_New()
      ‘ Modify constants as needed
      Const strModule = “basUtils”, strMacro = “Test”
      Dim lngStart As Long, lngLength As Long, strText As String

      With ThisDocument.VBProject.VBComponents(“ThisDocument”).CodeModule
      lngStart = .ProcStartLine(“Document_Open”, vbext_pk_Proc)
      lngLength = .ProcCountLines(“Document_Open”, vbext_pk_Proc)
      strText = .Lines(lngStart, lngLength)
      End With
      With ActiveDocument.VBProject.VBComponents(“ThisDocument”).CodeModule
      .AddFromString strText
      End With

      With ThisDocument.VBProject.VBComponents(strModule).CodeModule
      lngStart = .ProcStartLine(strMacro, vbext_pk_Proc)
      lngLength = .ProcCountLines(strMacro, vbext_pk_Proc)
      strText = .Lines(lngStart, lngLength)
      End With
      With ActiveDocument.VBProject.VBComponents.Add(vbext_ct_StdModule)
      .Name = strModule
      .CodeModule.AddFromString strText
      End With

      ‘ Other code for the Document_New procedure goes here.

      End Sub

      • #887464

        Am I right in assuming that anyone who wishes to use this template would have to make all those settings on their machine too?

      • #887465

        Am I right in assuming that anyone who wishes to use this template would have to make all those settings on their machine too?

        • #887484

          They don’t have to set the reference, for that is stored with the template. They would need to allow access to the VB project. That is the price you pay for wanting to use code to copy code.

          Alternatively, you could make sure that the text is visible, then break the link with the template (Tools | Templates and Add-Ins…, enter Normal in the Document Template box, click OK), then save the document. You can now distribute the document (without macros) to others.

          • #887496

            But the whole point of this is that they should have access to the show/hide macro.

            • #887510

              In that case, you will have to use the code to copy the macro into the document, and instruct the recipients that they have to allow access to the VB project, if they haven’t already done so. (Note: where I work, this is set by a group policy, I can’t change the setting myself.)

            • #887511

              In that case, you will have to use the code to copy the macro into the document, and instruct the recipients that they have to allow access to the VB project, if they haven’t already done so. (Note: where I work, this is set by a group policy, I can’t change the setting myself.)

          • #887497

            But the whole point of this is that they should have access to the show/hide macro.

        • #887485

          They don’t have to set the reference, for that is stored with the template. They would need to allow access to the VB project. That is the price you pay for wanting to use code to copy code.

          Alternatively, you could make sure that the text is visible, then break the link with the template (Tools | Templates and Add-Ins…, enter Normal in the Document Template box, click OK), then save the document. You can now distribute the document (without macros) to others.

    • #887425

      The following is a bit clunky, but I hope it does what you want.
      – Replace the string constants at the beginning of the code by the appropriate names.
      – Set a reference (through Tools | References… in the Visual Basic Editor) to the Microsoft Visual Basic for Applications Extensibility 5.3 object library; this library enables you to use code to manipulate code.
      – Select Tools | Macro | Security (in Word itself), activate the Trusted Sources tab and tick the “Trust Access to Visual Basic Project” check box. Otherwise, manipulating code in code is not allowed.

      Private Sub Document_New()
      ‘ Modify constants as needed
      Const strModule = “basUtils”, strMacro = “Test”
      Dim lngStart As Long, lngLength As Long, strText As String

      With ThisDocument.VBProject.VBComponents(“ThisDocument”).CodeModule
      lngStart = .ProcStartLine(“Document_Open”, vbext_pk_Proc)
      lngLength = .ProcCountLines(“Document_Open”, vbext_pk_Proc)
      strText = .Lines(lngStart, lngLength)
      End With
      With ActiveDocument.VBProject.VBComponents(“ThisDocument”).CodeModule
      .AddFromString strText
      End With

      With ThisDocument.VBProject.VBComponents(strModule).CodeModule
      lngStart = .ProcStartLine(strMacro, vbext_pk_Proc)
      lngLength = .ProcCountLines(strMacro, vbext_pk_Proc)
      strText = .Lines(lngStart, lngLength)
      End With
      With ActiveDocument.VBProject.VBComponents.Add(vbext_ct_StdModule)
      .Name = strModule
      .CodeModule.AddFromString strText
      End With

      ‘ Other code for the Document_New procedure goes here.

      End Sub

    • #887710

      This whole thread is confusing to me. grin

      You can embed macros directly in a document, just as you can in a template. The user typically will get a warning that the document contains macros, and the option to Enable them. Unless security is set to High, in which case your macros will be silently disabled, or low, in which case they will be silently enabled. (Over that you have no control.)

      Added: You can use the Organizer dialog to copy the macros you want to embed before sending the document off.

      • #887724

        Heck, Monsieur Scher beat me to it.

        The Organizer object is the proper way to move modules.

      • #887725

        Heck, Monsieur Scher beat me to it.

        The Organizer object is the proper way to move modules.

    • #887711

      This whole thread is confusing to me. grin

      You can embed macros directly in a document, just as you can in a template. The user typically will get a warning that the document contains macros, and the option to Enable them. Unless security is set to High, in which case your macros will be silently disabled, or low, in which case they will be silently enabled. (Over that you have no control.)

      Added: You can use the Organizer dialog to copy the macros you want to embed before sending the document off.

    • #887720

      Macros belong in template files unless they apply only to a specific document.

      Copying macros to documents very bad for code control.
      Suppose you want to change a macro, you then have to find all documents that have copies of the macros, much easier to update macros in a template.

      When one has application specific macros, the macros need to be isolated in a separate template for each app. Then the template is sent once, each time the template is modified, along with the documents, which are sent anyway.

    • #887721

      Macros belong in template files unless they apply only to a specific document.

      Copying macros to documents very bad for code control.
      Suppose you want to change a macro, you then have to find all documents that have copies of the macros, much easier to update macros in a template.

      When one has application specific macros, the macros need to be isolated in a separate template for each app. Then the template is sent once, each time the template is modified, along with the documents, which are sent anyway.

    Viewing 7 reply threads
    Reply To: Transporting m,acros (2002 SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: