Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Transporting m,acros (2002 SP3)

    I have created a template that contains 3 macros

    <UL><LI>A New macro that updates fillin macros in the header and then locks them
    <LI>A macro that runs from a MACROBUTTON that does a show/hide toggle on some text in a table
    <LI>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

  2. #2
    Gold Lounger
    Join Date
    Feb 2004
    Location
    Cape Town, RSA
    Posts
    3,444
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Transporting m,acros (2002 SP3)

    <P ID="edit" class=small>(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!!!)</P>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!
    Regards,
    Rudi

  3. #3
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Transporting m,acros (2002 SP3)

    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!

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Transporting m,acros (2002 SP3)

    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("ThisDocumen t").CodeModule
    .AddFromString strText
    End With

    With ThisDocument.VBProject.VBComponents(strModule).Cod eModule
    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

  5. #5
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Transporting m,acros (2002 SP3)

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

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Transporting m,acros (2002 SP3)

    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.

  7. #7
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Transporting m,acros (2002 SP3)

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

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Transporting m,acros (2002 SP3)

    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.)

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Transporting m,acros (2002 SP3)

    This whole thread is confusing to me. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    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.

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Transporting m,acros (2002 SP3)

    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.

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Transporting m,acros (2002 SP3)

    Heck, Monsieur Scher beat me to it.

    The Organizer object is the proper way to move modules.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •