Solved

OnSave Event in VBA?

Posted on 2002-04-24
22
3,771 Views
Last Modified: 2007-11-27
There are only new, open and close events in a WORD document with VBA. Where are OnSave, Onalign events? How do I make a procedure for these?
Thanks

0
Comment
Question by:luckie
  • 9
  • 6
  • 4
  • +1
22 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 6967816
Hi Luckie,

Check the object model inspector

-open Word
-goto the VB Editor with ALT+F11
-then use F2 to birng it up
-now look for all events | properties you need info about

for save it's the ActiveDocument.Save you need to call the method, there's no event triggered except for close

about the align which align, from a picture or a page or something?

maybe if you could explain what you're trying to do we can come with an more precise suggestion

HTH:O)Bruintje
0
 

Author Comment

by:luckie
ID: 6968075
I'm trying to for example, open up a messagebox when the user clicks on the Save/Save As... button.
So what you mean is to code under the close event for this situation? (by putting ActiveDocument.Save under Document.Close)? Hope I get across the concept right..
Thanks
Jacky
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6968101
Hi Jacky,

-what you can do is a bit of a workaround
-make your own save procedure

it works like this
--> first build your own save script

public sub MySave
  'do something here
end sub

-->now place this code into your template (could be normal.dot or whatever)
-->then close the VB Editor
-->then in Word
-->choose tools | customize
-->choose the first tab
-->in the list look for macros
-->click and in the right pane there will be MySave
-->drag this one to the file menu
-->place it under the Save or something
-->give it a decent menu name like Save Special or something

-->you can also add the macro to the exisitng Save menu if that's more convenient

HTH:O)Bruintje
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:luckie
ID: 6968320
Hi Brunintje,
    So Does this mean there would be no other ways to manufacture a save event in WORD VBA? :)
Thanks
Jacky
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6968343
Hi Jacky ,
this is the closest you can get one i guess
Brian
0
 
LVL 8

Expert Comment

by:starl
ID: 6970309
what version of word do you have? starting in 2000, there's a "DocumentBeforeSave" available.
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6970329
ok, it's an application level event so you've got to catch it for it all open documents, if you decide to use it then you should check for the current document

thought it was meant to go from the current document and to perform a special action, but it's an alternative nonetheless

btw what's the purpose, you've to perform any special action on the save event?

:O)Bruintje
0
 

Author Comment

by:luckie
ID: 6970415
WORD 97, but I'm planning to use WORD 2000.
0
 
LVL 8

Expert Comment

by:starl
ID: 6971142
planning???? so, can the code wait till you get to 2000?
0
 

Author Comment

by:luckie
ID: 6971713
cos I use WORD97 at home, but I need to use WORD 2000 in the workplace. :)
Thanks
Jacky
0
 
LVL 6

Expert Comment

by:blakeh1
ID: 6972242
You can have macros you create triggered when a certain action is run, if you Name a macro FileSaveAs this will get triggered when someone hits File, Save As, the problem with this is that you now have to code in your own save as macro (this will NOT get triggered if someone uses File, Save, or hits the Save button on the toolbar and the document has never been saved). For File, Save you will have to repeat the same thing. You will need to create error handling in the case they hit cancel in the save as dialog.

Sub FileSaveAs()
    Dim dlg As Dialog
    Dim sFileName As String
    MsgBox "Trapping and replacing the FileSaveAs command"
   
    Set dlg = Application.Dialogs(wdDialogFileSaveAs)
    dlg.Display
    dlg.Execute
   
End Sub

Sub FileSave()
    MsgBox "Trapping and replacing the FileSaveAs command"
    ActiveDocument.Save
   
End Sub
0
 
LVL 6

Expert Comment

by:blakeh1
ID: 6972248
Of course those macros will have to be created in the normal.dot file, and anyone you expect to be using this will either need to have to share a single normal.dot, or will need a copy to replace there exisiting normal.dot.
0
 

Author Comment

by:luckie
ID: 6972954
Hi blakeh1,
    How do you 'connect' the SaveAs action to the macro specifically? Still, when there is no onSave event?

 
0
 

Author Comment

by:luckie
ID: 6972955
Or 'connect' the saveas Macro to the saveas menu
file->saveas...
or something like that?
0
 
LVL 8

Expert Comment

by:starl
ID: 6973220
hmmm - you could rebuild the menu...is this for a certain document template? all uses?
0
 
LVL 6

Accepted Solution

by:
blakeh1 earned 50 total points
ID: 6977359
By creating a macro in your Normal.dot that is called FileSaveAs, word will trigger that macro whenever someone uses File, Save As. Likewise creating a macro called FileSave will be triggered when someone uses file, save  (or clicks on the toolbar button). By naming macros the same as the built in Word commands these intercept them (which means you will have to write your own routine something like the sample above. To see the list of word commands go to Tools, Macros, and the dropdown for Macros In, choose Word Commands.
0
 

Author Comment

by:luckie
ID: 6977383
give me a day or two... because too busy... come back ASAP
0
 
LVL 8

Expert Comment

by:starl
ID: 6980671
blakeh - is this a 97 option??? I didn't see the "Word Commands" listing... (my 2000 is at home)
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6982911
Well there are some relevant suggestions in this thread why not wrap it up?

-changing the menu is a common theme
-adding a custom save item or replacing the current save seems to be the open question

did you find anything usefull in these proposed solutions or are you still looking to replace the event, which isn't an option and the suggestion from blakeh1 comes closest to this

:O)Bruintje
0
 

Author Comment

by:luckie
ID: 6983105
OK... let's finish it off. But Can I split the points between bruintje and blakeh1? Both of you gave me a lot of useful information... :)
0
 

Author Comment

by:luckie
ID: 6983114
I think the points should to awarded to ... :)
0
 
LVL 44

Expert Comment

by:bruintje
ID: 6983576
forget about point splits, since the stupid minimum rule it's not smart to split points only if they are more then 100 it would be doable, i like it this way i got the PAQ anyway and two ways to solve this problem :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question