VBA Word - force save in Docm at shutdown/beforesave

Hi Guys,

I need some help with saving this docm file as always as docm.

i found this code for Excel which works perfectly, but cant really figur out how to make this for word.

kindly help

   
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)

On Error GoTo Errorhandler

If Not ActiveWorkbook.FileFormat = 52 Then
    MsgBox "Please save the file as macroenabled"
    
    
    Cancel = True
    
End If

Exit Sub

Errorhandler:
Exit Sub

End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    Dim fname As Variant
    
    On Error GoTo Errorhandler
    
    If SaveAsUI Then
        Cancel = True   'Cancel the original SaveAs
         'Get filename (with path) for saving
        fname = Application.GetSaveAsFilename(fileFilter:="Excel Marcro-Enabled Workbook (*.xlsm),*.xlsm")
        If fname = False Then Exit Sub  'Exit if user hit Cancel
        Application.EnableEvents = False  'Prevent this event from firing
        ThisWorkbook.SaveAs Filename:=fname, FileFormat:=52
          '52 = xlOpenXMLWorkbookMacroEnabled = xlsm (with macro's in 2007-2010)
        Application.EnableEvents = True  'Re-enable events
    End If
Exit Sub

Errorhandler:
    Application.EnableEvents = True   'So events are never left disabled.
    MsgBox "Error has accured", vbCritical, "Error Message"
    Exit Sub
    
End Sub

Open in new window

LVL 1
HakumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
In a class module renamed EventClassModule
Public WithEvents appWord As Word.Application

Private Sub appWord_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
On Error GoTo Errorhandler

If Not ActiveWorkbook.FileFormat = wdFormatXMLDocumentMacroEnabled Then
    MsgBox "Please save the file as macroenabled"
    
    Cancel = True
    
End If

Exit Sub

Errorhandler:
Exit Sub

End Sub

Private Sub appWord_DocumentBeforeSave _
 (ByVal Doc As Document, _
 SaveAsUI As Boolean, _
 Cancel As Boolean)
    
    Dim fname As Variant
    Dim dlg As FileDialog

    On Error GoTo Errorhandler
    
    If SaveAsUI Then

         'Get filename (with path) for saving
        Set dlg = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs)
        With dlg
            .AllowMultiSelect = False
            .FilterIndex = 2
            intResponse = .Show

            If intResponse = vbCancel Then
                Cancel = True
                Exit Sub
            End If
            fname = .SelectedItems(1)

            ThisDocument.SaveAs FileName:=fname, FileFormat:=wdFormatXMLDocumentMacroEnabled
            Cancel = True   'Cancel the original SaveAs
        End With
    End If
Exit Sub

Errorhandler:
    MsgBox "Error has occured", vbCritical, "Error Message"
    Exit Sub

End Sub

Open in new window

and in the ThisDocument Module
Dim X As New EventClassModule

Private Sub Document_Open()
 Set X.appWord = Word.Application
End Sub

Open in new window

at first save your document and then reopen it

Regards
0
HakumAuthor Commented:
Hi,

I did what you said and i get an error on this:

Error
0
Rgonzo1971Commented:
In a class module?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HakumAuthor Commented:
yes
0
Rgonzo1971Commented:
How does look the icon of your module?
0
HakumAuthor Commented:
like this :)

Module
0
HakumAuthor Commented:
Ahhh!!! sorry got it! i inserted the wrong module type. let me test and get back to you sooorry!!!
0
Rgonzo1971Commented:
it is not a class module
right-click on it / Insert / Class Module
and delete the one you don't need anymore
0
HakumAuthor Commented:
When I Click save this error pops up in the class module

Capture.PNG
0
HakumAuthor Commented:
followed by this after clicking ok:

Capture.PNG
0
Rgonzo1971Commented:
Do you have option Explicit then

Dim intResponse as Integer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HakumAuthor Commented:
worked like a charm! thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.