Problem re-assigning template

B

Brian

I am building a macro that will prevent users from changing original docs. I
only write simple macros as I haven't done many.

The problem is that when the document is saved - forced on user- I want the
new document's template re-assigned to normal.dot otherwise every time they
open their document it will run the macro again. Re-assigning Normal.dot to
their document works, however it also re-assigns the normal.dot to the
original; which I assumed would be closed. The code is as follows:

Sub AutoOpen()
' Automatically runs when Word starts
'With ActiveDocument.ActiveWindow
MsgBox "Please save this document, with your document name before
continuing", vbOKOnly
If vbOK = 1 Then
With Dialogs(wdDialogFileSaveAs)
.Show
End With
End If
With Dialogs(wdDialogFileSaveAs)
If ActiveDocument.Saved = False Then
ActiveDocument.Close

Else
If ActiveDocument.Saved = True Then
ActiveDocument.AttachedTemplate = NormalTemplate
End If
End If
If SendKey = ESC Then
End
End If
End With
End Sub

Any advice would be gratefully recieved. There are many of these docs
containing the basic format and layout for requirements, design etc for
applications.

Thanks

Brian

PS, Their are some clever clogs who un-check the read-only, hense forcing
the save
 
D

Doug Robbins - Word MVP

The macro should be named AutoNew. Then it will only run when a new
document is created from the template.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Brian

Unfortunately, the users open an existing documents - they know it as the
template- which contains all front matter, headings, TOC etc. they then
simply fill in the blanks using the SOPs and Guidance documents.

I was wondering, do I have capture the click on save? Problem is I have
checked the documentation, and Google, and haven't managed to find out how.
 
J

Jean-Guy Marcil

Brian was telling us:
Brian nous racontait que :
I am building a macro that will prevent users from changing original
docs. I only write simple macros as I haven't done many.

The problem is that when the document is saved - forced on user- I
want the new document's template re-assigned to normal.dot otherwise
every time they open their document it will run the macro again.
Re-assigning Normal.dot to their document works, however it also
re-assigns the normal.dot to the original; which I assumed would be
closed. The code is as follows:

Sub AutoOpen()
' Automatically runs when Word starts
'With ActiveDocument.ActiveWindow
MsgBox "Please save this document, with your document name before
continuing", vbOKOnly
If vbOK = 1 Then
With Dialogs(wdDialogFileSaveAs)
.Show
End With
End If
With Dialogs(wdDialogFileSaveAs)
If ActiveDocument.Saved = False Then
ActiveDocument.Close

Else
If ActiveDocument.Saved = True Then
ActiveDocument.AttachedTemplate = NormalTemplate
End If
End If
If SendKey = ESC Then
End
End If
End With
End Sub

Any advice would be gratefully recieved. There are many of these docs
containing the basic format and layout for requirements, design etc
for applications.

Thanks

Brian

PS, Their are some clever clogs who un-check the read-only, hense
forcing the save

If I understand correctly, you have a document (*.doc) that is used as a
template, i.e. other documents are created from this original document, but
it must not be modified.
This is the error right there. This document should really be saved as real
template (*.dot) and you would not have all these problems. Then, no code at
all would ne necessary.

Now, if it is not possible, then try something like this below. My code is
different than yours for many reasons:

I know the document has already been saved, so we have to force a save as. I
want to make sure the user does not do a "Save As" on top of the original
document, which would defeat the purpose.

I do not know what you were trying with:
If SendKey = ESC Then
but it does not work on my machine.

Since you are just saving as a document into another document and that the
code is in an AutoOpen procedure, I assume that the code is part of that
original document. In that case, the code will be saved wit the document and
the user will have to go through this code every time they open a document
that was "saved as" from that original document. This is why I included the
first If block.

I use the Display method of the dialog box so that I can get information
from the dialog box without its OK button actually performing the actions.

'_______________________________________
Dim dlgSave As Dialog
Dim lngDlg As Long
Dim strSaveFullName As String
Const strOriginalDoc As String = "Name_of_Original_document.doc"

If ActiveDocument.Name <> strOriginalDoc Then
Exit Sub
End If

'With ActiveDocument.ActiveWindow
MsgBox "Please save this document, with your document name before " _
& "continuing ", vbOKOnly

Set dlgSave = Dialogs(wdDialogFileSaveAs)

With dlgSave
Do
lngDlg = .Display
'full path name
strSaveFullName = WordBasic.FileNameInfo(.Name, 1)
If lngDlg = -1 And strSaveFullName = ActiveDocument.FullName Then
lngDlg = 0
End If
If lngDlg <> -1 Then
If MsgBox("You must save this document with a new name before "
_
& "continuing on.", vbOKCancel) = vbCancel Then
ActiveDocument.Close wdDoNotSaveChanges
Exit Sub
End If
Else
With ActiveDocument
.AttachedTemplate = NormalTemplate
.SaveAs strSaveFullName
End With
End If
Loop While lngDlg <> -1
End With
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

Bear

Brian:

What you're trying to do is already built into Word as a fundamental
concept. You should be createing new documents by opening a template, not by
copying a document.

"Original documents" = Templates

Consider saving your "template" document as an actual template (File > Save
As). You can then remove all your code and the read-only attribute. Word
won't let them overwrite the template, because it won't let them open it for
editing.

Put the template in the templates folder (as indicated in Tools > Options >
File Locations for User Templates. Then it will appear when they click File >
New, which is how they should be creating a new document. Even if they double
click the template, Word will still only create a new document based on the
template, but will not open the template for editing.

You can create an additional single level of subfolders in the Templates
folder, and the folder names will appear as tabs in the New dialog box. Your
company name or the categories of "original" documents would all make fine
folder names.

Bear
 

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