1

I'm trying to save and close all open Word files (to start I only target one of them).

The macro I made works when ran from Word (clicking "Execute"), but it won't run when called from the command line with this:

winword.exe /n /mMacro3
  • Operative system: Windows 11

  • Word version: 2019

  • Word opens and says that "the macros of this project have been disabled".

  • I have tried using a different target file and restarting Word.

  • The target file is "Doc2.docm".

  • I think it is available in "Normal.dotm" because it appears when I select this option in the macro menu.

  • In macro settings, all macros are allowed and also checked the "Trust VBA project DOM" (translated) option just in case.

  • I don't have the option some people in forums have of a "Security" section (not tab) in the first tab of the file's properties in the file explorer with the option to unblock the file.

  • Developer->Protect->Restrict editing's options are completely off.

The macro file:

Public Sub Macro3()
'
' Macro3 Macro
'
'
Dim doc As Document
Dim docName As String

docName = "Doc2.docm"

For Each doc In Documents
    If doc.Name = docName Then
        doc.Save
        doc.Close
    End If
Next doc

MsgBox "The ''" & docName & "'' document was not founr.", vbExclamation
End Sub

What am I doing wrong? Thank you.

9
  • 1
    I'm fairly sure you're running into a security check here and that its going to be hard to do with a word macro. Given that what you want to do is just close word and save, maybe go the powershell route. Powershell can find all word instances and send a soft close, which will trigger word to close all documents. Any document not saved will give a Do you want to save [yes] [no] button. You can then use powershell to detect these popups and send a y to its dialog or enter to save the document. Also, it may be possible to start word from powershell and have more control over it.
    – LPChip
    Commented Feb 27 at 15:38
  • @LPChip Interesting, I'll look into it. Thanks. Commented Feb 27 at 16:26
  • Wow that's just beautiful, thanks again Commented Feb 27 at 17:14
  • Consider simply naming the macro AutoExec in your Normal.dotm. Commented Feb 27 at 18:26
  • @CharlesKenyon Really? So that everytime he starts an instance of word, it closes all his word documents? That would be really annoying.
    – LPChip
    Commented Feb 28 at 15:57

0

You must log in to answer this question.

Browse other questions tagged .