PDA

View Full Version : Word AutoSave Bypass?? (Word 2013)



davis1118
01-04-2018, 05:26 PM
I currently have a macro that runs at the BeforeSave event to create a copy of the document and also print the document to PDF. I have it planned to be ran as a global template, and for that reason I have a userform that prompts the user if they want to save with the macro, cancel, or save normal without the macro. This way the user is able to save documents normally if the documents aren't going to be used for work purposes. I thought I had everything figured out until the autosave feature ran after 10 minutes and triggered the macro to run. This would be very annoying to have the userform pop-up every 10 minutes.
Currently, I bypass the macro if the SaveAs is triggered. Is there a way to recognize the autosave event and let it bypass the macro the same way as the SaveAs? I figured the entire code is not necessary, so I only included the beginning to capture what I was referring too above.

I realize I can disable the auto save feature in Word, but I have a feeling that IT and many users would like to keep it turned on for safety reasons. I'm just trying to figure out my other options at this point.


Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
GoTo lbl_End
Else
'All other code
End Sub


As always, thank you for the help!

Dave
01-13-2018, 06:59 AM
Can't you just go File>Options>Save then uncheck the autodocument recovery feature in the Global document? Dave

Dave
01-15-2018, 03:12 PM
I re-read this post and I see that it's not so simple when there's IT folks around. My mistake so I Googled a bit and found some code and some insights to a possible solution. Word VBA's not my thing so I'm no sure this will be helpful. Dave

'use this method to verify is the Save triggered by user or is it AutoSave:
'It works fine with Word 2010 on Windows 7 but on Windows 8.1 it does not work.
void adxWordEvents_DocumentBeforeSave(object sender, ADXHostBeforeSaveEventArgs e)
{
object oBasic = WordApp.WordBasic;
object fIsAutoSave =
oBasic.GetType().InvokeMember(
"IsAutosaveEvent",
BindingFlags.GetProperty,
null, oBasic, null);
if (int.Parse(fIsAutoSave.ToString()) != 1)
{
getActiveAddinInstance().getAnnotator().setActive(false);
}
}
'The other way is to intercept all Ribbon controls causing Word to save the document
'(and produce DocumentBeforeSave). In the event handlers you set a flag and check the
'flag in the DocumentBeforeSave event: if the flag is not set, the event is initiated by AutoSave.
'The idea is to have the flag set whenever a user-initiated action occurs; if the flag is not set
'in DocumentBeforeSave, then the action is intiated by Word itself.
HTH.

alsbc
02-14-2018, 01:43 AM
nvkjvjk

gmayor
02-14-2018, 02:27 AM
You can disable the autosave in Office 16 using a registry hack. That can be called from your process (or separately) and will hold until you change it back with the following function that will toggle the setting. The function will return True if autosave is disabled or false if not.


Function AutoSaveDisabled() As Boolean
Dim wshShell As Object
Dim RegKey As String
Dim rKeyWord As String
Dim wVer As String
Set wshShell = CreateObject("WScript.Shell")
If Val(Application.Version) < 16 Then
'The security issue relates to
'Word versions from 16.0 (Word 365)
MsgBox "This macro is for Word 2016 and later!", vbOKOnly, "Wrong Word Version"
Exit Function
End If
wVer = Application.Version
Start:
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & wVer & "\Word\"
On Error Resume Next
'The registry key does not exist
rKeyWord = wshShell.RegRead(RegKey & "DontAutoSave")
If rKeyWord = "" Then
wshShell.RegWrite RegKey & "DontAutoSave", 0, "REG_DWORD" 'set it at zero
GoTo Start: 'and read it again
End If
If rKeyWord = 1 Then
wshShell.RegWrite RegKey & "DontAutoSave", 0, "REG_DWORD"
AutoSaveDisabled = False
Else
wshShell.RegWrite RegKey & "DontAutoSave", 1, "REG_DWORD"
AutoSaveDisabled = True
End If
lbl_Exit:
Exit Function
End Function