Solved

OnSave Event in VBA?

Posted on 2002-04-24
22
3,705 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now