Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Macro to copy a macro

    Hi

    Word2000

    I'm having trouble writing a macro to copy a macro from the attached template to the active document. Can't work out what the objects are, and how to check whether the macro (in the document) already exists.

    Can someone help?
    Dale

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to copy a macro

    Hi Dale,

    Because there's no really straightforward way to do this, the first question has to be: why would you want to copy a macro from the attached template to the active document? - if the template is attached, then isn't the macro already available to the document?

    You can use the Organizer (and control it programatically) to transfer macro project items from one file to another, but these are at the module, not the procedure level.
    You can also use the MS VBA Extensibility Library to programatically work with code in the VB Editor, but here too there aren't any methods available to work directly with procedures - it can be done but only through very strenuous methods - see some of Chris Greaves' posts in the VB/VBA forum for more on that topic.

    Gary

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Macro to copy a macro

    Thanks, Gary

    ---
    "Why" is because the documents will be downloadable from an intranet, and experience shows that if the person who downloads them can't see the server the template is on, then all sorts of funny things happen.

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to copy a macro

    Here's some code to use the VBE extensibility library to delete a module (you need to set a reference to it):
    <pre>For i = NormalTemplate.VBProject.VBComponents.Count To 1 Step -1
    sName = NormalTemplate.VBProject.VBComponents(i).Name
    Select Case LCase$(sName)

    Case "tmpdde"
    blnChanged = True
    Application.OrganizerDelete Source:=NormalTemplate.FullName, _
    Name:=sName, Object:=wdOrganizerObjectProjectItems
    End Select
    Next
    </pre>


    Here's a snip of code I used some while back to copy code:
    <pre> Application.OrganizerCopy Source:=Templates(iAddinIndex).FullName, _
    Destination:=NormalTemplate.FullName, _
    Name:="GetSaveFields", _
    Object:=wdOrganizerObjectProjectItems
    </pre>


    However, we gave up doing this, because we were copying code into Normal.dot, and it rang alarm bells for our virus checker.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to copy a macro

    Hi again,

    Creating a new template-based document from an intranet seems to be a popular problem; wish I had a magic bullet for that one. One workaround is, rather than having a hyperlink to the template (which seems to want to open in Word a copy of the template, rather than a new document based on the template), have the hyperlink open an intermediary Word document; the intermediary Word document contains a hyperlink to the template. Although this method requires the user to go through an additional "page" to get to their new document, it does permit you to create a new document, based on and attached to its template, via an intranet. (But perhaps that's not the specific problem you're dealing with anyway <g>)

    As far as copying macros via code, I'd use the Organizer method as it's straightforward and doesn't require setting a reference to VB Extensibility.

    The code Geoff posted makes a nice starting point. If memory serves, you can also end up with code pretty close to what you need, by recording a macro of your using the Organizer to copy a macro.

    There will still be the problem, after the document is reattached to its template, of the macro remaining in the document, and triggering a macro warning whenever the document is opened.

    Gary

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Wellington, Wellington, New Zealand
    Posts
    378
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Macro to copy a macro

    Thanks to you both.

    I'll give it a go and let you know the outcome.

    Dale

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Macro to copy a macro

    I have addressed this problem of template access via the intranet by hyperlinking to a shortcut and the shortcut points to the template. It is clunky and a little problematic (the shortcut won't do relative links and you still need to choose to download the file) but it achieves the aim. I am living in hope that someone will point the way with a Java or VBScript solution but so far I ain't seen it.

    I don't want to play with autonew macros as I still want the base templates to be available in the usual way as well.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Macro to copy a macro

    Hi Andrew,

    I thought I'd hit the jackpot with the following:

    <pre><script LANGUAGE="vbscript">

    Sub cmdNewDoc_OnClick
    Dim objWord

    Set objWord = CreateObject("Word.Application.9")
    With objWord
    .Visible = True
    .Documents.Add "D:WordDataTemplatesWebTestTemplate.dot"
    End With
    Set objWord = Nothing
    End Sub
    </script>
    </pre>


    but first time you run it, you get a warning about an unsafe ActiveX control and advising you not to run it - not too user-friendly! [It does work though on subsequent tries.]

    Based on my (very rudimentary at this point) understanding of client-side scripting, CreateObject ain't welcome <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15>.

    Gary

Posting Permissions

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