0
votes

I'm trying to keep certain macros running when Word fires up or opens another document, but I want them removed from the list of macros the user can access (alt+f8).

I've tried adding 'private' instead of 'public', but this stops them running altogether for some reason.

Public Sub AutoExec()
    DisplayStylesMenu
End Sub

Public Sub AutoNew() 
    DisplayStylesMenu
End Sub

Public Sub AutoOpen() 
     DisplayStylesMenu
End Sub

Public Sub DisplayStylesMenu()
    ' Opens the Formatting task pane (Style window)
    Application.TaskPanes(wdTaskPaneFormatting).Visible = True

    ' Docks the window on the right
    Application.CommandBars("Styles").Position = msoBarRight
End Sub

Currently, AutoExec, AutoNew and AutoOpen all appear in the publicly accessible macros list (because of the 'public' prefix), but when adding 'private' to any of them, they stop working and there's no error.

How can I keep the macros running on startup, opening a doc etc whilst hiding them from the macro list?

1
Did you try to modify the accessor "Public" to "Private"? It may help.Alexander Bell
I did, and it stops the macros appearsing AND running altogether, which defeats the purpose. I added it to 'AutoExec', 'AutoNew', 'AutoOpen' and 'Sub DisplayStylesMenu', no combination seems to work for me.Timmah
Then you should probably create a template file and put the macros in it, and open new word document based on that template.Alexander Bell
That's actually exactly what I've got set up. The macros live in the Normal.dotm template, on which all new documents are based. Because the macros list shows macros saved in the Normal.dotm file by default as well as the current document, they are present no matter what document the user has open. I tried this too: Private Sub AutoNew() MsgBox "test" Application.TaskPanes(wdTaskPaneFormatting).Visible = True Application.CommandBars("Styles").Position = msoBarRight End Sub This didnt show the msgbox, so how can private subs can run automatically?Timmah
You probably need to create your own template and open new Word docs based on that template.Alexander Bell

1 Answers

4
votes

One way to prevent macros from being shown in the list of macros is to structure them so that they require at least one argument. This can be an essentially a "dummy" variable which will not actually be used.

So, declare a constant in the module, like so:

Const hiddenMacro as Boolean = True

I used a boolean, but you could use String, Integer, Long, etc.

Then, in your AutoNew subroutine, add this variable as a required argument:

Sub AutoNew(hiddenMacro as Boolean)
    MsgBox "Hello, world!", vbInformation
End Sub

Macros which require arguments do not appear in the macro list:

Screenshot of macro list which excludes macros that require arguments

Update Here I highlight what the ThisDocument module is, and show clearly where all of this code belongs in the the ThisDocument module of the Normal.dot file.

Another screenshot with highlighting and junk