DocumentBeforeSave Event

P

Peter Karlström

Hi

I'm developing a Word COM-Addin and did place some document property
controls in the DocumentBeforeSave-event, but I have been forced to remove
them because of the AutoSave feature in Word.

Is it possible to see if it is Words Autosave feature or the user that have
triggered
this event?

Thanks in advance
 
J

Jialiang Ge [MSFT]

Hello Peter,

From your post, my understanding on this issue is: you want to know how to
distinguish the user's save from Word AutoSave in DocuemntBeforeSave event
handler. If I'm off base, please feel free to let me know.

As we see, DocumentBeforeSave event fires on background auto save. The
reason why this happens is because the application is not able to
differentiate between saving a document by the AutoRecovery option or
saving the document using the File->Save menu option. This is by design in
the application. According to our internal logs for the issue, there
doesn't seem to be a way to detect AutoSave. But here are several
workarounds you may have a try to see if they fit your current situation:

Workaround #1. Overriding FileSave (Recommended)
If what you need is to trap the case when the user clicks Save or SaveAs,
you may consider using the FileSave and FileSaveAs override in a document
template instead, a way simpler and cleaner than setting up event handler.
The following articles discuss the usage of these two macros:
http://msdn2.microsoft.com/en-us/library/Aa264128(office.10).aspx
http://support.microsoft.com/default.aspx?scid=kb;en-us;211468
For example:
To intercept each call and set a parameter or global variable.

Sub FileSave()
DoMySave "FileSave"
End Sub

Sub FileSaveAs()
DoMySave "FileSaveAs"
End Sub

But the problem of this workaround is that it may require us to deploy the
add-in with the template macros.

Workaround #2. Disabling AutoSave
We can turn off AutoSave, but this is application-wide, which means it
could be very dangerous for the user if he's working on multiple documents.
Therefore, this is not a recommended approach.

Workaround #3. Using SaveAsUI parameter in BeforeSaveHandler:
private void BeforeSaveHandler(Word.Document Doc, ref bool SaveAsUI, ref
bool Cancel)
You may try to encapsulate all of the code in the BeforeSaveHandler with
if(SaveAsUI)
{ ... }
The parameter SaveAsUI is used to indicate whether we need to display the
Save As dialog box. And generally, in AutoSave, the parameter is set to
FALSE. But the problem is that, if the document has already been saved and
it is not 'SaveAs', the flag SaveAsUI will also be false because no 'Save'
dialog box is displayed.

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Karlström

Hi Jialiang Ge
and thanks for your comprehensive reply.

You understood my question perfectly correct.

It seems that Word leaves no way to exactly know when AutoSave
has been executed.
I'm sorry to say that none of your workarounds will work, and we have
considered moving these document controls to the moment the documents
are opened instead. The customer has accepted this as an alternative solution.

Thanks again

--
Regards
Peter Karlström
Midrange AB
Sweden
 
A

Andrei Smolin [Add-in Express]

Hello Peter,

In the add-in, you handle all the command bar buttons and Ribbon controls
that can be used for saving the document: Save, Save As, Save All, etc. In
addition, you intercept the key combinations assigned to these commands.
Then, in the corresponding event handlers, you set a certain flag and check
it in BeforeDocumentSave. If the flag is not set, then the document is
AutoSaved.

On http://blogs.msdn.com/ptorr/archive/2004/01/28/63741.aspx, pay attention
to the post by Trumpet, Inc.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com
 
J

Jialiang Ge [MSFT]

Hello Andrei,

Thank you for the sharing of the blog
(http://blogs.msdn.com/ptorr/archive/2004/01/28/63741.aspx). The comment
from Trumpet Inc. might also be a workaround for the issue.

Hello Peter,

Thanks for your understanding of this limitation of Office product. If you
have any other concern or need anything else, please feel free to let me
know. We will do our best to follow up.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Karlström

Hi Andrei

Your idea is good but my component is a COM-Addin, not a VBA Template Addin.

If I have understood it correctly, I can't override the original FileSave
and FileSaveAs without implementing a template, and this is not an option in
this
project.

Regards
--
Peter Karlström
Midrange AB
Sweden


Andrei Smolin said:
Hello Peter,

In the add-in, you handle all the command bar buttons and Ribbon controls
that can be used for saving the document: Save, Save As, Save All, etc. In
addition, you intercept the key combinations assigned to these commands.
Then, in the corresponding event handlers, you set a certain flag and check
it in BeforeDocumentSave. If the flag is not set, then the document is
AutoSaved.

On http://blogs.msdn.com/ptorr/archive/2004/01/28/63741.aspx, pay attention
to the post by Trumpet, Inc.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com
 
A

Andrei Smolin [Add-in Express]

If I have understood it correctly,

No. You can intercept every command bar button action in a COM add-in. Use
our free Built-in Controls Scanner at
http://www.add-in-express.com/downloads/controls-scanner.php to find the IDs
of built-in controls, find the
controls in the add-in code, and handle their events..

As to Ribbon Controls, you can find their IDs on
http://www.microsoft.com/downloads/...e9-4d11-46a5-898d-23e4f331e9ae&DisplayLang=en.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com


Peter Karlstrom said:
Hi Andrei

Your idea is good but my component is a COM-Addin, not a VBA Template
Addin.

If I have understood it correctly, I can't override the original FileSave
and FileSaveAs without implementing a template, and this is not an option
in
this
project.

Regards
--
Peter Karlstrom
Midrange AB
Sweden
 
P

Peter Karlström

Hi Andrei

I didn't know that.

So if I for instance have ID för the Save button on Words Standard bar,
which is 3,
how do I declare this event?

--
Peter Karlström
Midrange AB
Sweden
 
A

Andrei Smolin [Add-in Express]

Hi Peter,

Loop through the command bars, find the control
(theCurrentCommandBar.FindControl(ID:=3)), assign it to a WithEvents
variable of the Office.CommandbarButton type and process the Click event.

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com
 
P

Peter Karlström

Hi Andrei

Thanks for your tip. I finaly got it!

The complete solution is that I set a Global boolean variabel to True
if the user uses any of Words Save or SaveAs-functions. (There are some...)
In the DocumentBeforeSave I check this variable and then do stuff if its
True. Then I reset it to False.

This works like a charm.

Again, THANKS.

--
Peter Karlström
Midrange AB
Sweden
 
J

Jialiang Ge [MSFT]

Hello Andrei,

Thank you again for your great suggestion. I believe that other community
members will also benefit from your experience sharing. We have also
recorded your workaround into our internal log for future reference. I
appreciate your time and efforts working with us and helping Peter find a
resolution.

Hello Peter,
If I have understood it correctly, I can't override the original
FileSave and FileSaveAs without implementing a template,
and this is not an option in this project.
Peter's suggestion is to set a new event handler for the Save and SaveAs
buttons, therefore, it is different from the direct 'override' of FileSave
and FileSaveAs function in my first reply. (a direct override of FileSave
and FileSaveAs could only be in the macros)

As Peter suggested, please carefully hook the events of all the possible
entries of 'Save', such as Save, Save As, Save All, Ctrl+S, etc, so that
the global flag is set correctly. Sorry for the inconveniences.

I am contacting you to see whether there is anything we can do for you. If
you have any comments on our support service, website design, our product
limitation, our process, and etc., please don't hesitate to let me know. I
will do my best to follow up.

Thank you for using our MSDN Managed Newsgroup Support Service.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Andrei,

Thank you again for your great suggestion. I believe that other community
members will also benefit from your experience sharing. We have also
recorded your workaround into our internal log for future reference. I
appreciate your time and efforts working with us and helping Peter find a
final resolution.

Hello Peter,
If I have understood it correctly, I can't override the original
FileSave and FileSaveAs without implementing a template,
and this is not an option in this project.
Peter's suggestion is to set a new event handler for the Save and SaveAs
buttons, therefore, it is different from the direct 'override' of Save and
SaveAs function in my first reply. (a direct override of FileSave and
FileSaveAs could only be in the macros)

As Andrei suggested, please carefully hook the events of all the possible
entries of 'Save', such as Save, Save As, Save All, Ctrl+S, etc, so that
the global flag is set correctly. Sorry for the inconveniences.

Besides, Peter, I notice that you once posted another issue "Just Save or
Autosave" that is talking about the same question in this queue on
2007-9-27, but you did not get a reply then. My guess is that your managed
newsgroup account did not take effect at the time, and we, the newsgroup
team, failed to capture the question. I am contacting you to see whether
there is anything we can do for you. If you have any comments on our
support service, website design, our product limitation, our process, and
etc., please don't hesitate to let me know. I will do my best to follow up.

Thank you for using our MSDN Managed Newsgroup Support Service.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Andrei Smolin [Add-in Express]

Hello Jialiang,

That's some Trumpet Inc., not me.

I'd like to ask you a simple personal question. May I send you a private
e-mail?

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com
 
J

Jialiang Ge [MSFT]

Hello Andrei,
I'd like to ask you a simple personal question. May I send you a
private e-mail?
Sure, please feel free to send it to my mailbox:
(e-mail address removed) (remove 'online.').

One more point for Peter:

Hello Peter,

You could use the "contact us" channel next time when there is no response
for you in managed newsgroups. Your feedbacks will be directly sent to the
managers of Newsgroups team.
https://support.microsoft.com/contactus2/emailcontact.aspx?scid=sw;en;1296&w
s=msdn&sd=msdn&pa=msdnw

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Charles Waudby

Hi,

Picked up this thread searching for problem with Word 2007 - our solution
uses the CommandBarButton click events in a COM Add-In but the new Ribbon
does not fire (and on advice from MS will never fire) the CommandBarButton
events. DocumentBeforeSave is also fired by AutoSave and so is useless if you
want to trap user saves (as far as I know). The only solution is to repurpose
the FileSave etc which is easy to do in a VS2005 COM Add-In, implement
IRibbonExtensibilty and your XML for the repurpose is injected when
GetCustomUI is called when Word starts. (A trick that seems to work to keep
legacy COM Add-Ins working is to use the repurposed events to fire button
clicks to raise the old events.) As this is global, make sure you only fire
this stuff off for documents belonging to your own app.

I'm still not sure why the repurposing fails in VB6 COM Add-ins -
GetCustomUI is called, so I have to assume there is a problem with the method
signature (even though it appears to be the same as the one I'm using in
..Net) - or have I made a mistake in the code???

Interested if anyone has anuthing better.
 
A

Andrei Smolin [Add-in Express]

Hi Charles,
our solution
uses the CommandBarButton click events in a COM Add-In but the new Ribbon
does not fire (and on advice from MS will never fire) the CommandBarButton
events.

If an add-in adds a custom CommandBarButton, Ribbon will show it on the
Add-ins tab and you'll get the Click event.

Do I miss anything in your reasoning?

Regards from Belarus,

Andrei Smolin
Add-in Express Team Leader
www.add-in-express.com
 
C

Charles Waudby

Our COM Add-In captures existing buttons, eg Save, SaveAs, not buttons we
add, because we want to store the documents in a central repository remotely
via web services (the user does not have direct access to the repository).

FindControls(, 3) finds all the Save buttons, (748 SaveAs etc), but these
buttons do not raise the Clicked event with Word 2007, (unless you get hold
of one of them and use the Execute method).

As DocumentBeforeSave is useless (because it fires on AutoSave with no way
to tell if the event came from the user or AutoSave), we had to find an
alternative.

Part of the reason I posted was to alert people (if they did not already
know) that the CommandBarButtons so not get Ribbon events in Office 2007.

Regards,

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top