none
Run time error 5892 when running a macro from protected view.

    Question

  • Hi all

    I've created an MS Word document that contains various form fields and buttons to streamline and digitize quite a laborious process. I've set all of my breaks in the relevant areas of the document in order to allow the user to complete form fields only using Restricted Editing. I've added a new button with the following functionality:

    • Run a macro to copy the document and "Save As..." a .docx document (IT security prevent distribution of any macro-enabled documents both internally and externally).
    • Open a new Outlook mail item.
    • Populate the subject, to, CC, BCC and body using data from some of the form fields.
    • Display the email.

    All of the above works perfectly when not running in Restricted Editing mode, as soon as I turn on the Restricted Editing I receive "Run-time error '5892': That method is not available on that object.". The debugger suggests the issue is with running the macro. I've disabled the line containing the command to run macro and that does indeed seem to be the case as I can run the remaining code without a hitch.

    So I'm wondering if this is a problem with running macros in Restricted Editing view? Edit: Have tried to call the sub using a couple of methods ([Call SaveCopyAs()] & [Application.Run MacroName:="SaveCopyAs"]) and receive the same issue.

    Code for the sub is as follows:

    Option Explicit
    Sub SaveCopyAs()
        Const lCancelled_c As Long = 0
        Dim sSaveAsPath As String
        sSaveAsPath = GetSaveAsPath
        If VBA.LenB(sSaveAsPath) = lCancelled_c Then Exit Sub
         'Save changes to original document
        ActiveDocument.Save
         'the next line copies the active document
        Application.Documents.Add ActiveDocument.FullName
         'the next line saves the copy to your location and name
        ActiveDocument.SaveAs sSaveAsPath
         'next line closes the copy leaving you with the original document
        ActiveDocument.Close
    End Sub
    
    
    Public Function GetSaveAsPath() As String
        Dim fd As Office.FileDialog
        Set fd = Word.Application.FileDialog(msoFileDialogSaveAs)
        fd.InitialFileName = ActiveDocument.Name
        If fd.Show Then GetSaveAsPath = fd.SelectedItems(1)
    End Function

    Many thanks in advance.

    J


    • Edited by JMcG123 Monday, April 15, 2019 3:22 PM more detail.
    Monday, April 15, 2019 3:07 PM

Answers

  • Just in case anyone encounters the same issue in the future I've found a solution.

    Implementing the code directly into the document rather than an accompanying module works perfectly. I'm not certain but I believe the Restricted Editing mode disables macros from being called from within the document VBA for some reason - it works if you use "Developer">"Code">"Macros" but not when trying to call within a sub in the main document from a module.

    • Marked as answer by JMcG123 Tuesday, April 16, 2019 11:21 AM
    Tuesday, April 16, 2019 11:20 AM