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
 
That "Keyword not found" message can really drive a person nuts ... especially when the keyword does exist!

I am presuming that you are using the cursor-over-keyword and F1 combination, which is what usually yields that #@*$& "not found" message. If that is the case, type the keyword into the VBA Help utility; that will give you answers most times.

Hope this helps.

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
Yes, I was doing the F1 over the keyword.
Thanks for the pointer. I'll give it a try.

JH
 
There is NO mention on how to override Word commands in Help.

Essentially though, you simply write a Sub that IS the Word command.

The default procedure is right in the Word application code, BUT you must remember that any procedure later than "normal" rules.

So, there is, in fact, an internal Sub FileSave() procedure hidden in Word. But if you write one, then it will take priority, and yours will execute.

There is an internal Sub EditCopy() procedure hidden in Word which executes any time you copy something. But, if you write one, then it will take priority, and yours will execute.

Code:
Sub EditCopy()
  Msgbox "na-na-na-na...no copying allowed!"
End Sub

The trick is getting to know what are the Word commands. For example, not too many people know that the Word command (procedure) for the Print button is different than the procedure executed by the Print menu, or by the keyboard shortcut.

Sub FilePrint executes by File > Print, or Ctrl-P
Sub FileDefaultPrint executes by Print button

Two absolutely separate procedures.

faq219-2884

Gerry
My paintings and sculpture
 
Just to tell a little story....

Back when I was more of a s&^t disturber, there was a seriously annoying smart-a$$. I re-wrote his FileDefaultPrint to do something different from the File > Print.

It displayed a message: "Clicking OK will format C: drive. Are you sure?" Except...there was only the OK button.

He went nuts doing virus checking, register hacks etc. etc.

He could not figure out why he could print using the menu, but NOT with the Print button.

BTW: do not do this. It is very very rude.

Eventually I rewrote FileDefaultPrint to display a message: "Normal functionality has been restored." There was another Sub that deleted my written FileDefaultPrint procedure, so indeed everything worked normally again.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry,

I love it! This made my morning. (I'm prone to spreading a little s**t myself from time to time, just to keep folks on their toes.)

Thanks!

----------------------------------------------------------------------------------
"A committee is a life form with six or more legs and no brain." -- L. Long
 
Gerry,
Thanks for your explanations on how to use the word commands as Subs.
I also got a big kick out of your story on being a programming prankster. I can only imagine the look on his face.

You’re giving me ideas…

Thanks again for the feedback,
JH
 
Here is a link to an almost full listing of Word commands,

WARNING!!!! Do not, repeat NOT, open this file unless you already have the VBE open.

ALL Word commands are disabled in this file (override.dot)...including being able to close it. AND the command to open the VBE. So if it is not already open....you can't open it, which means you can't alter the closing command...

You have to comment out the FileClose sub in the VBE to be able to close it.

It is interesting going through the hundreds of procedures. The names of the commands are mostly sane, but some are odd.

Also note that override.dot simply rewrites the commands as messageboxes saying the name of the command. They do NOT show what the real command syntax is. You are on your own to do that.

For example:

Sub ViewVBCode() is the command name to open the VBE.

But the actual instruction is:
ShowVisualBasicEditor = True

So in override.dot there is the sub:
Code:
Sub ViewVBCode()
   Msgbox "ViewVBCode"
End Sub
So if you do Alt-F11, or Tools > Macros > Visual Basic Editor, you get the messagebox (and no VBE). But you need to know what the REAL instruction is, if you want to use it in any practical way. For example:
Code:
Sub Document_Open()
If ShowVisualBasicEditor = True Then
   ShowVisualBasicEditor = False
End Sub

Sub ViewVBCode()
If Environs("username") <> "gerry" Then
   Msgbox "Access to the Visual Basic Editor " & _
       "is denied."
Else
   ShowVisualBasicEditor = True
End If
End Sub
The above closes the VBE (if open) when the document is opened. Then, unless the login name is "gerry", the VBE can not be opened.

Please note that NONE of the override tricks have any serious level of security, and should never be used as such. It would not take much to get around them if you have moderate knowledge of VBA and Word.

The major use of looking at the file (at the link above) is getting a deeper understanding of the Word document/object model. It is a handy companion to wandering through the Object Browser.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top