Document Save Event
-
Mittwoch, 1. August 2012 09:47
Just like the way we have DocumentChange event
Example:
oWord.DocumentChange += new MSWord.ApplicationEvents4_DocumentChangeEventHandler(oWord_DocumentChange);
I also want to Capture an event that is fired when the document is saved ( It could be normal save or Save As) So which event will it be? There is an event named DocumentBeforeSave. But I want an event that is DocumentAfterSave which doesn't exist.
Adeel
Alle Antworten
-
Mittwoch, 1. August 2012 11:18
Yeah it would have saved me a ton of hassle if Word a DocumentAfterSave event.
There are workarounds, but they're not hassle free. Probably the easiest is to handle the DocumentBeforeSave event, cancel the save (assuming you're doing this from within an AddIn), and instead save it with your own command, after which you can enact more code.
You need to couch this with a global (or class level) 'ignore' bool, so when you enact a save it won't get trapped, endlessly saving....something like:
bool allowSave = false;
BeforeSaveEvent(Document doc, ref bool cancel)
{
if (!allowSave)
{
// Before save code
allowSave = true;
doc.Save();
// After save code
allowSave = false;
cancel = true; // Stop Word doing its normal save: we have already saved the document
}
}
- Bearbeitet JosephFox Mittwoch, 1. August 2012 11:20
- Als Antwort vorgeschlagen Quist ZhangMicrosoft Contingent Staff, Moderator Donnerstag, 2. August 2012 09:17
- Als Antwort markiert maverick786us Freitag, 3. August 2012 04:01
- Tag als Antwort aufgehoben maverick786us Freitag, 3. August 2012 11:45
-
Freitag, 3. August 2012 11:48
Can you suggest me some trick in this case? Because if I use this code DocumentBeforeSaveAs...
void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); }
Now when a user want to save this document as .ODT the above code should enable the button the moment it encounters extension to be .ODT. But the problem is the above mentioned code is executed BEFORE the save as dialog box where the user choose .ODT format for saving the file. So in this case it will never enable the button the moment .ODT document that was saved is loaded by default.Adeel
-
Freitag, 3. August 2012 12:13
I think you want:
bool allowSave = false; void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!allowSave) { allowSave = true; Doc.Save(); File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); allowSave = false; Cancel = true; // Stop Word doing its normal save: we have already saved the document } }- Bearbeitet JosephFox Freitag, 3. August 2012 12:14 forgot to update variable names to your situation
-
Freitag, 3. August 2012 12:38
I think you want:
bool allowSave = false; void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!allowSave) { allowSave = true; Doc.Save(); File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); allowSave = false; Cancel = true; // Stop Word doing its normal save: we have already saved the document } }
Thanks. This is perfect the only thing that is missing is ...
void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!bAllowSave) { bAllowSave = true; Doc.Save(); File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); bAllowSave = false; Cancel = true; } }this Doc.Save doesn't invoke the SaveAs dialog where the user will specify the .ODT format. So by default it saves in .docx format instead of .odt format. Is there a way to invoke save as dialog?Adeel
-
Freitag, 3. August 2012 12:59
Well, as you may guess the SaveAsUI ref bool sets that. So I guess you need to propagate that value to the invoked save.bool allowSave = false; bool showSaveAsUI = false; void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!allowSave) { allowSave = true; showSaveAsUI = SaveAsUI; Doc.Save(); File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); allowSave = false; Cancel = true; } SaveAsUI = showSaveAsUI; }
- Bearbeitet JosephFox Freitag, 3. August 2012 12:59
-
Freitag, 3. August 2012 13:20
I am afraid this thing doesn't seem to show SaveAs Dialog
private bool bAllowSave = false; private bool showSaveAsUI = false; void oWord_DocumentBeforeSave(MSWord.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!bAllowSave) { bAllowSave = true; showSaveAsUI = SaveAsUI; Doc.Save(); File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); bAllowSave = false; Cancel = true; } SaveAsUI = showSaveAsUI; }
I hope something isn't missed outAdeel
-
Freitag, 3. August 2012 13:57
I don't know why that didn't work. Here's a solution I've just tested:
private bool bAllowSave = false; void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel) { if (!bAllowSave) { bAllowSave = true; if (SaveAsUI) { // Display Save As dialog Word.Dialog d = this.Application.Dialogs[Word.WdWordDialog.wdDialogFileSaveAs]; object timeOut = 0; d.Show(ref timeOut); } else { // Save without dialog Doc.Save(); } File = oWord.ActiveDocument.Path.ToString() + @"\" + oWord.ActiveDocument.Name.ToString(); Extension = Path.GetExtension(File); ribbon.InvalidateControl("ODTConvertor"); bAllowSave = false; Cancel = true; return; } }You may be able to set the default save format to ODT...but I haven't attempted it before. If you're interested, start looking here and here.- Als Antwort markiert maverick786us Montag, 6. August 2012 04:58
-
Montag, 6. August 2012 07:20Finally it worked. It was the Word Dialog solved the thing. Thank you for all the help that you've recently provided in this forum section
Adeel
- Bearbeitet maverick786us Montag, 6. August 2012 08:09

