#1
|
|||
|
|||
Disabling Word's Save and SaveAs commands
Is it possible to run my macro via word's Save command. So basically when the user clicks on Save/SaveAs, my macro will be called.
How do I go about doing this? |
#2
|
|||
|
|||
Yes. Just have your macro procedure have the name.
Code:
Sub FileSave() MsgBox "Blah blah blah" ActiveDocument.Save End Sub |
#3
|
|||
|
|||
You probably want it for both FileSave and FileSaveAs.
Note, with some commands this gets tricky. Intercepting events like Save and Print |
#4
|
|||
|
|||
Indeed. I do not know what your macro is supposed to do, but make sure you think things out fully before you start overwriting standard commands.
|
#5
|
|||
|
|||
Hi, Thanks for your response.
What I want to do is: 1. Fill in 3 fields (content controls) on the document i.e. Name, Date (DateTimePicker) and Subject. 2. When the user Selects/Clicks on Save/Save As, the dialog box needs to display and i) All the content in the 3 fields needs to be combined and displayed in the "File Name" field of the dlgbox ii) The dlgbox needs to open from a specified path. Below is my code. But for some reason, one minute it works and the next it doesn't. Please have a look and let me know what I'm doing wrong. Code:
Sub FileSave() ' ' FileSave Macro ' Saves the active document or template ' Dim strName As String, strDate As String, strSubject As String Dim strDocName As String Dim dlgSaveAs As FileDialog Dim ccCtrl As ContentControl On Error GoTo errhandler: Set dlgSaveAs = Application.FileDialog(msoFileDialogSaveAs) With ActiveDocument strName = .ContentControls("1017512746").Range.Text strDate = .ContentControls("2985804456").Range.Text strSubject = .ContentControls("821630576").Range.Text End With strDocName = strName & "_" & strDate & "_" & strSubject With dlgSaveAs If .Show = -1 Then .InitialFileName = "C:\" & strDocName .Title = strDocName ActiveDocument.SaveAs2 (strDocName) Else MsgBox ("Document not saved!") End If End With errhandler: 'MsgBox ("Error saving the document!") End Sub |
#6
|
|||
|
|||
Why
MsgBox ("Document not saved") rather than MsgBox "Document not saved" ? You are certainly going to get an error if one or more of your content controls does not exist. I do not know what you mean by "the dlgbox needs no open from a specific path." Have the path for saving be a specific path? The string concatenation looks fine. Other than that, I would need a bit more documentation. I do not know what the -1 value for .show indicates or why you are testing for it. You could always have your dialog box get the values from the content controls and if they are empty require input (and then save the input to those controls). |
#7
|
|||
|
|||
Hi,
Thanks for all the feedback. My macro intercepts word's commands FileSave and FileSaveAs perfectly for word 2007 and 2010. It also works perfectly for 2013 FileSave but NOT for FileSaveAs. I don't understand why this should be the case because the FileSaveAs command in 2013 is the same as 2010. Could I be doing something wrong? Are there any extra commands I should be putting in? |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
When I try to save an existing word doc, save as pops up and will not save... | samanthab | Word | 3 | 01-19-2013 06:27 AM |
Saveas error handling | jillapass | Word VBA | 7 | 03-30-2012 03:24 PM |
Keyboard Commands | korric | Word | 3 | 02-09-2012 12:52 AM |
saveAs ChDir _ | tinfanide | Excel Programming | 4 | 11-05-2011 01:40 AM |
Advance Commands | Richard1 | Word | 0 | 02-10-2011 01:35 PM |