PDA

View Full Version : Solved: Copying VBA into Normal Template



samuelwright
12-08-2005, 07:59 AM
Hey all

Whenever I want to copy a VBA project into the Normal.dot Template, I am allowed to copy the Userforms and Modules across, but not the Microsoft Word Objects ie. the code that is in ThisDocument.

ThisDocument contains Document_Open and Document_Close subs that do not work if I put them in a normal module (unless of course I manual run them-kinda defeats the point of having them run on Document_Open!!).

Is there a way of doing this? Thanks!

The ThisDocument Code is written below:


Sub Document_open()

Dim oCtl As Office.CommandBarControl
Dim oBar As Office.CommandBar

On Error Resume Next
'Loop through all controls to clear all "Subject Naming Utility" buttons
For Each oCtl In oBar.Controls
If oCtl.Caption = "Subject Naming Utility" Then
oCtl.Delete

End If
Next

Set oCtl = Nothing

Dim cb As CommandBarControl

With Application.CommandBars.ActiveMenuBar 'with the main menu bar
Set cb = .Controls.Add(msoControlPopup, Temporary:=True)
With cb 'with the new menu
.Caption = "Subject Name Utility" 'set the caption
.OnAction = "FOI"
End With
End With

End Sub

Sub Document_Close()

Dim cb As CommandBarControl
On Error Resume Next
For Each cb In Application.CommandBars("Menu Bar").Controls
If cb.Caption = "Subject Naming Utility" Then cb.Delete
Next

End Sub

matthewspatrick
12-08-2005, 10:16 AM
If you put a Document_Open sub in Normal.dot, then that sub will fire whenever:

1) You open a doc (not create, but open) based on the Normal template or
2) You open Normal.dot as a document

To get a sub to fire once when the template opens (in the case of Normal.dot, when Word starts up), put an AutoExec sub in a regular module.

TonyJollans
12-08-2005, 11:23 AM
You can't copy the thisdocument module. You have to copy the contents.

samuelwright
12-09-2005, 02:00 AM
Hey Guys

I put an AutoExec and AutoExit sub in the regular module, cut the code I had in the thisdocument module and pasted it into those subs...the result?

Well, it works brilliantly, thank you so much!!! The code is below:


Sub AutoExec()

Dim oCtl As Office.CommandBarControl
Dim oBar As Office.CommandBar
On Error Resume Next
'Loop through all controls to clear all "Subject Naming Utility" buttons
For Each oCtl In oBar.Controls
If oCtl.Caption = "Subject Naming Utility" Then
oCtl.Delete

End If
Next

Set oCtl = Nothing

Dim cb As CommandBarControl

With Application.CommandBars.ActiveMenuBar 'with the main menu bar
Set cb = .Controls.Add(msoControlPopup, Temporary:=True) 'add a menu
With cb 'with the new menu
.Caption = "Save Form Utility" 'set the caption
.OnAction = "FOI"
End With
End With

End Sub

Sub AutoExit()

Dim cb As CommandBarControl
On Error Resume Next
For Each cb In Application.CommandBars("Menu Bar").Controls
If cb.Caption = "Save Form Utility" Then cb.Delete
Next

End Sub



I wrote this on Word 2003: would someone mind testing it on Word 2000 please, because I do not have access to it and the code is intended for someone on Word 2000...

:cloud9:

TonyJollans
12-09-2005, 05:03 AM
I don't entirely see what you're doing. You seem to remove buttons which you don't seem to create - or you would if you set oBar to anything. Apart from that ...

There was a problem with Temporary:=True - certainly in Word 97 and, I think, in Word 2000 so you should certainly check that it works as expected

There is a problem in Word 2003 with Normal templates growing in size when regular changes are made to commandbars - see http://support.microsoft.com/default.aspx?scid=kb;en-us;873017 - so best check that out as well.

samuelwright
12-09-2005, 08:18 AM
Aha! Yes, sorry I forgot to mention that the button entitled subject naming utility was a hangover from a Outlook macro that puts a button on the toolbar if Word is selected as the default message editor!

Thank you for your tip about the Temporary = True line...I will check this out with a friend on 2000.