Word97: Add Command Bar causes Prompt to Save Template (.dot) file

V

VBA Coder

Does anyone know why the creation of a PopUp MenuBar would cause the "Save
As" dialog box to appear?

I have a UserForm that loads when a user create a new document based on a
Template file. The UserForm allows the user to enter criteria that will
populate bookmarks and add tables and text to the document. On this
UserForm I created a "Hide" button, that allows the user to Hide the Form so
that they can switch over to the document to make changes. Because Word97
makes all UserForms modal, there was no way to allow the user to work on the
document without closing the UserForm. However, the users need to keep this
form loaded so that they can make additional changes on the UserForm which
will in turn update the document. So the "Hide" button method was created.
When the user clicks the "Hide" button, I created a PopUp menu that has a
button on it that allows them to "Show" the UserForm so that when they've
made all their changes to the document, they can come back to the UserForm.

It is this PopUp Command Bar that is causing Word to think the Template has
changed, therefore, prompting the user to "Save" the template file (.dot)
when they close their Document (.doc). I have made the Command Bar
"Temporary", so that it is not part of the Template. Any ideas why this may
be causing Word to think the Template has changed? I have even tried using
the ActiveDocument.AttachedTemplate.Saved =True, with no success. As long
as the user clicks the btnHide, which loads this CommandBar, the user will
always get the "Save As" dialog box on the Template file when they close
their document. My code is below:

Private Sub btnHide_Click()

' Hide the UserForm
frmMacroMain.Hide

' Show the PopUp Form Button to allow the user to Re-Show the form
ShowPopUpFormButton

End Sub

Public Function ShowPopUpFormButton()
Dim myBar As CommandBar
Dim graphBtn As CommandBarButton
Dim bFound As Boolean, sToolBarName As String

sToolBarName = "ShowMacroForm"

' Check if the PopUp Form already exists. If so, just show it, otherwise,
create it
For Each myBar In CommandBars
If Left$(myBar.Name, Len(sToolBarName)) = sToolBarName Then
bFound = True
Exit For
End If
Next

If bFound Then
CommandBars(sToolBarName).Visible = True
Else
Set myBar = CommandBars.Add(Name:=sToolBarName,
Position:=msoBarFloating, _
Temporary:=True)
myBar.Visible = True
Set graphBtn = myBar.Controls.Add(Type:=msoControlButton)
With graphBtn
.Caption = "&Show Form"
.Style = msoButtonCaption
.DescriptionText = "&Show Macro Form"
' When clicked, run the Macro to Show the Form
.OnAction = "ShowMacroForm"
End With
End If

End Function
 
J

JGM

Hi there,

Just curious,

Where did you try the

ActiveDocument.AttachedTemplate.Saved =True

line in your code? From which module was it being executed?

Cheers!
 
T

Thomas Winter

My understanding is that the Temporary property of CommandBars in Word
doesn't do anything. Some other Office applications make use of this
property but Word does not.

A good solution might be to have your own separate .DOT add-in placed in
Word's STARTUP folder. This .DOT add-in will all ready have the command bar
created. All your code has to do is show and hide the command bar.

You really don't even need to have the .DOT add-in located in the STARTUP
folder, as long as YOUR CODE knows where your .DOT add-in is. Your code can
use the AddIns object within Word to "add" the add-in whenever you need it.

Below is some code that might work for you. It's exact code I used in a
project which worked for me. (It was a VB app, not VBA macro code, which is
why it passed in the Application object.) You might need to modify it, but
you should get the idea.

For a .DOT add-in that is NOT stored in the STARTUP folder, follow the code
as-is (the comments include some good info). I find this works better
because its usually easier to know where you installed your files then
getting a file installed in Word's STARTUP folder. If you do want to go the
route of installing the add-in in the STARTUP folder, then just show and
hide the commandbar at the right time. Don't worry about loading and
unloading the add-in. Word does it for you of course!

I hope I've explained this well enough. Any questions, let me know!

-Tom

Change btnHide_Click() to look like this:

Private Sub btnHide_Click()

' Hide the UserForm
frmMacroMain.Hide

' Make sure the add-in with my custom toolbar is loaded.
ReInstallWordAddIn(Application,
"C:\PathToMyAddInWithToolbar\myaddin.dot")

' Show my custom toolbar.
Application.CommandBars("Name of my custom toolbar").Visible = True

End Sub

In ShowMacroForm() you can do this:

Sub ShowMacroForm()

' Hide my custom toolbar.
Application.CommandBars("Name of my custom toolbar").Visible = False

' Get rid of the add-in entirely. You may NOT want to do this. As long
as you do NOT put the
' add-in in Word's STARTUP folder, the add-in will not be loaded the
next time Word starts,
' though it will still be in the list of add-ins in the Templates and
Add-ins dialog box. Using
' RemoveWordAddIn removes it from the list and thus cleans up any
clutter that might be left behind.
RemoveWordAddIn(Application, "myaddin.dot")

' Do the rest of your stuf......

End Sub

---------

Public Sub ReInstallWordAddIn(oWord As Word.Application, sPathAndName As
String)

' We remove and re-add the add-in, since it might all ready be loaded.
' As I recall, Word will through an error if you try to add an add-in
that's all ready loaded.

On Error GoTo ErrorHandler

RemoveWordAddIn oWord, GetFilePart(sPathAndName)

' This is the key part. The second parameter makes sure the add-in
is "installed" and active.

oWord.AddIns.Add sPathAndName, True

Exit Sub

ErrorHandler:

Exit Sub ' Maybe do something else with the error, if you REALLY need
the add-in

End Sub

Public Sub RemoveWordAddIn(oWord As Word.Application, sName As String)

' sName is just the file name of the add-in, without path.

On Error GoTo ErrorHandler

Dim oWordAddIn As Word.AddIn

Set oWordAddIn = GetWordAddIn(oWord, sName)

If Not oWordAddIn Is Nothing Then

' The 'Delete' method doesn't actually delete the add-in file,
it just
' turns it off. It removes the add-in from the add-ins list.
It's like
' clicking the Remove button in the Templates and Add-Ins
dialog.

oWordAddIn.Delete

End If

Exit Sub

ErrorHandler:

Exit Sub

End Sub

Public Function GetWordAddIn(oWord As Word.Application, sName As String) As
Word.AddIn

' sName is just the file name of the add-in, without path.

' We look for the add-in based on just its file name without the path.

On Error GoTo ErrorHandler

Dim oCurrAddIn As Word.AddIn

For Each oCurrAddIn In oWord.AddIns

If StrComp(oCurrAddIn.Name, sName, vbTextCompare) = 0 Then

Set GetWordAddIn = oCurrAddIn

Exit Function

End If

Next

Set GetWordAddIn = Nothing

Exit Function

ErrorHandler:

Set GetWordAddIn = Nothing

End Function

Public Function GetFilePart(sPath As String) As String

On Error GoTo ErrorHandler

GetFilePart = Right$(sPath, Len(sPath) - InStrRev(sPath, "\", ,
vbBinaryCompare))

Exit Function

ErrorHandler:

' Raise an error here maybe....

End Function
 
V

VBA Coder

I had put it in my AutoNew subroutine just before I show my UserForm, that
way it got run each time the user created a new document based on the
template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top