Consulting

Results 1 to 11 of 11

Thread: Macro to Copy Another Macro to Normal.dot

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Macro to Copy Another Macro to Normal.dot

    Let's leave out all the reasons why we shouldn't write to someone's normal.dot file and assume we NEED to write to someone's normal.dot file.

    We have a macro. Here it is (or very similar):

    [vba]
    Sub PasteSpec()

    Selection.PasteSpecial Link:=False, DataType:=wdPasteText

    End Sub
    [/vba]

    We just want a macro that will copy the macro above into the normal.dot of the current user's PC. The macro can run on open or, even better yet, run from a shortcut key, but it only need to run ONCE. It should probably throw up a message box that says "Paste Special Macro Copied to Your Global Template".

    Please--no lectures on method. Thanks!!

    ~Anne Troy

  2. #2
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Does it need to go to a particular module? And do you want it to run after it's created?

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    It should just go in a module in normal.dot. And, dang, I suppose it's gotta build a toolbar or shortcut to run the macro code it copies into it. Completely computer-illiterate users should be able to:

    a) copy the macro in
    b) run the macro with a shortcut key or toolbar button they don't have to assign

    Thanks!!
    ~Anne Troy

  4. #4
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    If I've understood correctly what you want, this should do the lion's share of the work that you're looking for, but it'll prolly require a bit of tweaking.

    [VBA]Sub AddProcedure()
    ' Requires reference to Microsoft Visual Basic for Applications Extensibility 5.x
    Dim VBCodeMod As CodeModule
    Dim LineNum As Long
    Dim MyMenuItem As Object

    Set VBCodeMod = Application.Templates("Normal.dot").VBProject.VBComponents("ThisDocument"). CodeModule
    With VBCodeMod
    LineNum = 3
    .InsertLines LineNum, _
    "Sub PasteSpec()" & Chr(13) & _
    " Selection.PasteSpecial Link:=False, DataType:=wdPasteText"" " & Chr(13) & _
    "End Sub"
    End With

    CustomizationContext = NormalTemplate

    Set MyMenuItem = Application.CommandBars("Edit").Controls.Add(Type:=msoControlButton, _
    Before:=8)
    With MyMenuItem
    .Caption = "Paste Super Special"
    .OnAction = "PasteSpec"
    End With

    MsgBox "Paste Special Macro Copied to Your Global Template"

    End Sub[/VBA]

    P.S. I adopted this from some code I found at http://www.cpearson.com/excel/vbe.htm

  5. #5
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    For future reference on this subject, this article might be handy...

    http://msdn.microsoft.com/library/de...ce07042002.asp

  6. #6
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Ann,

    Just to tease you anyway.

    You should use a simple template with the required code and shortcut in the users startup.

    But if you still like to proceed use the "OrganizerCopy" method to copy the module from your project to the target document like: [VBA]
    Application.OrganizerCopy Source:= _
    "C:\Documents and Settings\Joost Verdaasdonk\Application Data\Microsoft\Templates\Normal.dot" _
    , Destination:="Document1", Name:="NewMacros", Object:= _
    wdOrganizerObjectProjectItems
    [/VBA]

    The code by marco is great indeed but there's a cave-at.
    Trusted access to VBE projects have to be checked in security tab of target machine to be able to run that sub. (So you have to check for that)

    OrganizerCopy method will always work...But again just use a template!!/add-in.

    Later.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Ann,

    Didn't have much time this morning so I'll give you the full answer now.

    Use this code to do the copy action and clear the keybinding for the run once option: [VBA]
    Option Explicit

    Sub CopyToNormal()

    'Copy the module
    Application.OrganizerCopy Source:=ThisDocument.FullName, _
    Destination:=NormalTemplate.FullName, _
    Name:="basModuleToCopy", _
    Object:=wdOrganizerObjectProjectItems

    'Save Normal
    If NormalTemplate.Saved = False Then NormalTemplate.Save

    'Run Once so clear Keybinding
    CustomizationContext = ThisDocument
    FindKey(BuildKeyCode(Arg1:=wdKeyControl, Arg2:=wdKey9)).Clear
    ThisDocument.Save

    'Prompt
    MsgBox "Paste Special Macro Copied to Your Global Template", _
    vbInformation, "Succes"

    End Sub
    [/VBA]

    Try attachment. Shortcut key is CTRL+9

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Quote Originally Posted by MOS MASTER
    The code by marco is great indeed but there's a cave-at.
    Trusted access to VBE projects have to be checked in security tab of target machine to be able to run that sub. (So you have to check for that)
    Good point. I hadn't thought about that xCaveat.

  9. #9
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    LOL!!

    Sorry, guys. I was away today. TERRIFIC. Think you could add it to the KB?
    ~Anne Troy

  10. #10
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by xCav8r
    Good point. I hadn't thought about that xCaveat.
    No Problem you can easily check for that in the code!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Dreamboat
    LOL!!

    Sorry, guys. I was away today. TERRIFIC. Think you could add it to the KB?
    Hi Ann,

    No problemo you're welcome.

    Yupz will add it to the kb soon..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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