PDA

View Full Version : Macro to Copy Another Macro to Normal.dot



Anne Troy
06-26-2005, 08:08 PM
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):


Sub PasteSpec()

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

End Sub


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!!

:)

xCav8r
06-26-2005, 08:37 PM
Does it need to go to a particular module? And do you want it to run after it's created?

Anne Troy
06-26-2005, 08:53 PM
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!!

xCav8r
06-26-2005, 09:50 PM
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.

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

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

xCav8r
06-26-2005, 10:03 PM
For future reference on this subject, this article might be handy...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnofftalk/html/office07042002.asp

MOS MASTER
06-26-2005, 10:50 PM
Hi Ann, :yes

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:
Application.OrganizerCopy Source:= _
"C:\Documents and Settings\Joost Verdaasdonk\Application Data\Microsoft\Templates\Normal.dot" _
, Destination:="Document1", Name:="NewMacros", Object:= _
wdOrganizerObjectProjectItems


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. :whistle:

MOS MASTER
06-27-2005, 11:28 AM
Hi Ann, :yes

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:
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


Try attachment. Shortcut key is CTRL+9

Enjoy! :whistle:

xCav8r
06-27-2005, 04:48 PM
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.

Anne Troy
06-27-2005, 09:41 PM
LOL!!

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

MOS MASTER
06-27-2005, 10:44 PM
Good point. I hadn't thought about that xCaveat.
No Problem you can easily check for that in the code! :yes

MOS MASTER
06-27-2005, 10:46 PM
LOL!!

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

No problemo you're welcome.

Yupz will add it to the kb soon..:whistle: