• Hiding Macros from users (English Word 2003)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Hiding Macros from users (English Word 2003)

    Author
    Topic
    #430841

    I have a template in the Word startup folder that contains lots of macros assigned to a number of keys. I’d like to hide all these macros from the user as they can be seen from the Tools:Macro:Macros. I can hide them by making a macro “Private” but if I do that Word then can’t find that macro in the routine I use to assign them to the keys.

    I have used a number of modules to group similar macros together.

    Any ideas please….

    Dennis

    Viewing 3 reply threads
    Author
    Replies
    • #1007075

      I don’t believe you can do this, though I am by no means a Word expert, so hopefully someone will prove me wrong. You can also hide the macros from the Macros dialog by adding an optional typed parameter but this hides them from the menu Customize dialog too and seems to prevent Application.KeyBindings.Add from working. (is that the code you are using?)
      As a matter of interest, why do you want to hide them, given that you are allowing acces to run them anyway?

      • #1007079

        I have come to the same conclusion – methods that remove a macro from the Macros dialog also disable the possibility to assign a shortcut key.

      • #1007157

        Thanks for all the replies, it’s nice to know that it’s not me!

        Since my template has a lot of macros in it that get bound to various key combinations I just wanted to remove them from the Macro list as there is no need to run them manually. It would also then stop the “clutter” that they make in the list so that any personal macros the staff member has created can easily be seen. I just like things tidy!

        Thanks
        Dennis.

    • #1007104

      You want to prevent users from running them through other means? That seems a bit difficult.

      In theory, you should be able to ascertain the control used to invoke a macro, but when I’ve tried this in the past, I never quite worked out how to do it.

    • #1007115

      I found it was possible to create a macro that is hidden but could be run from a toolbar button by…
      1. Create the Sub
      2. Create the toolbar button customisation to run the Sub
      3. Change the Sub to a Function

      Whilst this worked, the same process did not allow me to customise a keyboard command to run the macro.

      Perhaps a workaround could be arrived at whereby all of your subs are changed to functions and you show just one sub which can call any of the functions but only if a keyboard modifier is held down. This gives the users the impression that there are no macros (apart from one sub which does nothing) but you could use it as your anchor for all the keyboard commands.

    • #1007165

      “user” and “manually” in this thread ought to be synonymous.
      If you have code that is to be accessed directly by the user, Word’s interface includes:
      (a) Menu (Tools, Macro, Macros) and
      ( Keyboard assignments (shortcut keys) and
      © Mouse assignments (toolbar buttons).
      This is how end-users talk to programs. Your program included.

      Macros are procedures that can be assigned/accessed in this way; macros are your user interface. Anything else ought not be a macro.

      Try this:
      Make a list of those procedures which are to be invoked directly by your user. These are your macros. Make them “Public Sub”s with no parameters.
      Make every other procedure a public function with a parameter, returning a result, or both. (I find it hard toi think of a reason for a function NOT to be returning a value).

      You will find that your users have access to what you want them to have, and do not have access to what you don’t want them to have.
      Your users will be provided with the choice to invoke your code via menu, keyboard shortcut or mouse-click, as is their right.
      Your public functions will be available to you, and any other developers on your system, as a library of procedures to sped up your application dveelopment.

      I know that in VBA users can invoke public functions that have no parameters. I’ve found it much easier to make every procedure a Function except for those procedures that are to be invoked by the end-user.

      More complex user interfaces are prepared through the user of GUI forms with event processing.

      • #1007180

        I did a quick test but it didn’t work, I’m afraid. The Keybinding routine fell over at the line where I had changes the Sub to a Function.

        I like the idea though. I never thought of using a Function instead of a Sub for things that don’t need a return. Just thinking about it though, I think most of Windows kernel calls are Functions that are often used as simple Subroutines.

        I’ll do some more tests as soon as I can. It may also have something to do with the way I’ve separated the macros into different Modules that may be causing a problem.

        TTFN
        Dennis

        • #1007196

          > fell over at the line where I had changes the Sub to a Function.

          I can be a bit harsh at times. [gentler] Carry on with your vest efforts in your current project, but in your next project try the suggested method.

          I generally have a very good idea of the user interface; the user says “I want to do (a), ( and ©”. That tells me I need three macros, “MacroA”, “MacroB”, and “MacroC”.
          That’s what the user has asked for, it’s what I’ve proposed, and it’s what I’m being paid for.
          With those three macros in place as parameter-less “Public Sub”s, every procedure I write from then on will be a Public Function, with parameters and a result, but never with no parameters and no result.
          It makes my life very easy, and cannot confusauser.[/gentler]

        • #1007505

          Hi Dennis,

          Put an “Option Private Module” declaration at the top of any code Module housing subs you don’t want the user to see in the Macros list. These subs will be callable by any other sub in the same project, but not by any sub in any other project.

          Another solution is to add an optional argument to the parameter list for each sub you want to hide. This works because macros that have arguments (even if they’re public macros) don’t show in the ‘run macro’ list. The macro is still available to those who know it’s there (or just called from a button). If the code has to remain Public (because it’s used in multiple modules) but you don’t want the users to be able to see it in the list of macros (from the macro recorder) or to run it, use something like:

          Sub HiddenMacro(optional NoVal as Boolean = True)
          ‘ your code here
          End Sub

          Cheers

          Cheers,
          Paul Edstein
          [Fmr MS MVP - Word]

          • #1007526

            Sorry, still doesn’t work. It always falls over in the module that has all the KeyBinding commands. I’m going to try re-writing the code to put the some of the routines I want hidden in the same module as the KeyBinding routines to see if that works.

            Thanks to everyone who has offered help so far. As soon as my latest project is out of the way I’m going make to this problem. There MUST be a solution somewhere. If I every find it I’ll post the result.

            TTFN
            Dennis

            • #1007528

              Good luck, but I’m not optimistic – the conditions needed for a piece of code to be available for keybindings are exactly the same as those for making it visible in the macros list.

      • #1007224

        [indent]


        I find it hard toi think of a reason for a function NOT to be returning a value


        [/indent]
        there are few reasons (which is not to say none) to write a function that returns nothing. In fact, there are those who would argue that everything should be a function unless it needs to be called from a menu button or a key combination. That way you can return as many parameters as necessary (just pass them ByRef) and always return at least a True/False value to indicate whether the routine failed or not. However, as with all things, the realities of life may interfere with the theories of best practice! grin

        • #1007230

          > those who would argue that everything should be a function unless it needs to be called from a menu
          Oh, you’ve met me then? (grin!)
          Regardless of the rights-and-wrongs, I find this approach serves me well in clarity of design.
          Of course, it has also turned every one of my applications into a cornucopia of useful utility procedures! In some cases, into powerful engines.

          • #1007233

            I confess I oscillate between the thinking that everything needs to return some sort of success value, and that some things should just work; if they don’t something major has gone wrong and should be dealt with accordingly – i.e. either everything returns False if it doesn’t work or something blows up. Sometimes I like the latter as it lets you know that something really weird has happened!

            • #1007248

              [lunch break] I did an analysis on my main library (UW.DOT) which holds 706 procedures.
              36% are Functions with No results!
              04% are Subs (with no results, natch)
              60% are Functions with results.[/lunch break]

    Viewing 3 reply threads
    Reply To: Hiding Macros from users (English Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: