Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute Sub when Save is Selected 2

Status
Not open for further replies.

Jhtexas

Technical User
Jan 4, 2005
59
US
I want to be able to execute a Sub whenever the user selects the “Save Icon” or tries to do a “File-Save” command. The end result is to keep a user from saving the active document that has an extension other than .doc.
Is there a way to tie this in?

Thanks,
Jim
 
Below is the code I have so far. Currently I’m having two issues:
One is that the Register Event Handler is not Executed automatically when Word opens.
The other is Word provides a dialog box that tells you that you may loose the format since it is a txt file and then it provides another dialog box for a SaveAs option. I’m not sure how to dismiss these dialog boxes so the user can’t see them. I tried ActiveDocument.Close but that just crashed Word.
Any help is appreciated.
Thanks,
Jim
Module:
Private Sub Document_Open()
Call Register_Event_Handler
End Sub

Module:
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub

CLASS MODULE:
Public WithEvents App As Word.Application
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If (UCase(Right(ActiveDocument.Name, 3)) = "TXT") Then
ActiveDocument.Saved = True
MsgBox ("Sorry, You Can't Save this Document Type")
End Sub
 
What about this ?
Application.DisplayAlerts = False

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV,
I added it to the code but the two windows still appear after the MsgBox. So it diplays the MsgBox and then the Format warning dialog and then the MsgBox again and then the SaveAs dialog box. If you have any other ideas I'll give it a try.

Thanks for your help,
Jim
 
I also tried it witht this format and had no success:
Application.DisplayAlerts = wdAlertsNone
 
The code below seems to work but since I'm a novice I wouldn't mind any suggestions.

Thanks,
JH

Module:
Private Sub AutoOpen()
Call Register_Event_Handler
End Sub

Module:
Dim X As New Class1
Public Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub

CLASS MODULE:
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If (UCase(Right(ActiveDocument.Name, 3)) = "PRN") Then
ActiveDocument.Saved = True
Cancel = True
MsgBox ("Sorry, You are not allowed to Save Document")
End If
End Sub
 
Can you clarify what it is you want?

If the active document was opened as .TXT - user can not save it, even as itself?

If the active document was opened as .TXT - user can not save it as .doc - or can they?

If the active document was opened as .DOC - user can not save it as TXT?

You may want to try intercepting the FileSaveAs command itself.
Code:
Sub FileSaveAs()
If UCase(Right(ActiveDocument.Name, 3)) = "TXT" Then
    MsgBox "No."
End If
End Sub

faq219-2884

Gerry
My paintings and sculpture
 
Gerry,
I don't want them to be able to save the file in anyway when they open any text file with a .PRN extension. I'm not trying to confuse the issue but I did change the file extension from TXT to PRN. The PRN files are text files.

Thanks,
Jim
 
Gerry,
I’ve incorporated your code and added the FileSave and added the code to not prompt when closing the file and I believe this should keep them form saving over the original file.
Your example was much easier than the route I was going.
Thanks for you help,
Jim
Code:
Sub FileSaveAs()
If UCase(Right(ActiveDocument.Name, 3)) = "PRN" Then
    MsgBox "Sorry, You are not allowed to Save this Document"
End If
End Sub

Sub FileSave()
If UCase(Right(ActiveDocument.Name, 3)) = "PRN" Then
    MsgBox "Sorry, You are not allowed to Save this Document"
End If
End Sub

Sub AutoClose()
If (UCase(Right(ActiveDocument.Name, 3)) = "PRN") Then
    ActiveDocument.Saved = True
End If
End Sub
 
I hope you realize that this is far from beinmg any sort of security.

1. They can open Word with macros disabled.

2. They can copy the contents elsewhere, and save it.

What exactly is your intention here?

faq219-2884

Gerry
My paintings and sculpture
 
My intention is to protect the original file from being altered in anyway. I’m not trying to protect the data from being copied or printed.

Thanks,
JH
 
If the user is not going to be changing anything (which is what "protect the original file from being altered in anyway." means to me), then simply protect the document.

Tools > Protect Document. Select Forms, give it a password (and PLEASE write it down somewhere.....). Voila. This protects the document from ANY edits. The document can not be altered in any way.

Not only that, but because it is not a macro, but a property of the document file, opening Word without macros will STILL have the document protected. No edits will be allowed.

This is not a critcism, but it helps if you clearly state what your intention is, what you are trying to do, when you post.

Your original post asked about "The end result is to keep a user from saving the active document that has an extension other than .doc."

Your last post is not this. Your last post states "My intention is to protect the original file from being altered in anyway. "

These are NOT the same end result. If you had stated that your intention was to prevent any changes, then I would have suggested protecting for forms immediately.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry,
The file is a native text file that is created and written to by an inspection program. The operator opens the text file and examines and prints the document. It is never a Word document and I don’t believe Word can store any protection information in a Text file.

Thanks,
JH
 
I don’t believe Word can store any protection information in a Text file
But windows does ...
 
I don't have any control over the program that writes the text files, so I can't add write protection from that side. That's why I took the approach of having Word open the text files to protect the original files.

Thanks for your help,
Jim
 
I see your problem. And true, Word can not store protect information on a text file. And true, as PHV mentions, Windows can. So, if you want to be sneaky......

You do not state what you have in Word that is going to open the text file. Obviously you must have a document (even a blank one) open. I am further assuming you are running the open code from normal.dot. Although you could be doing it from a global template.

In any case, you could, if you want to be sneaky, run code that will set Windows file attributes on the text file. You could make it Read-Only, then open it.

Not sure it would be worth the bother, but you could do it. Like this:
Code:
Dim fso As Scripting.FileSystemObject
Dim fil As Scripting.File
Set fso = CreateObject("Scripting.FileSystemObject")
Set fil = fso.GetFile("c:\myfiles\TestTxt.txt")
If fil.Attributes = 0 Then
   fil.Attributes = fil.Attributes + 1
End If
Set fil = Nothing
Set fso = Nothing
Documents.Open FileName:="c:\myfiles\testtxt.txt"
The text file will open in Word as Read-Only. You need a reference to Scripting Run-time to use FileSystemObject, but that is not a big deal.

NOTE: the above assumes there are NO other file attributes (Attributes = 0). Correct procedure would be to check first. Also, this is not temporary. This actually sets the file attribute to read-only.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry, as I like the oneliners ...
SetAttr "c:\myfiles\TestTxt.txt", vbReadOnly

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Curse you...of course....[blush]...now I feel REALLY stupid.

Jhtexas, just ignore me, lack of sleep has made me totally pointless.

<slinks away>

faq219-2884

Gerry
My paintings and sculpture
 
My only excuse is that I was teaching FSO to my Word VBA students this morning...and I had FSO on my brain. Ummm, a poor excuse I know, as yes, I like one-liners as well.

faq219-2884

Gerry
My paintings and sculpture
 
I’ve tried the suggestions and they are also very good alternatives. Making the file read only definitely reduces any chances of the file being altered.
One of the good code suggestions above used the FileSaveAs and FileSave sub routines and captured the File Save As and File Save events. When I try to find those in Help I get an error Keyword not found. How would I ever know to use those if they’re not in the help file? And how do they capture the corresponding events?

Thanks for all the help,
JH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top