View Full Version : Deactivate button after it has been run once

Dave T
06-04-2014, 05:49 PM
Hello All,

I have created my first ribbon which for me works well and is a considerable improvement over adding my old Word 2003 macro menus to the Quick Access Toolbar.

I have attached a very cut down version of the ribbon and associated macros that I use to create a report.

If a user clicks on the ‘Page Setup’ button it runs a macro that changes the page margins and also calls other macros.
The problem is that if a user clicks on the button again it runs the other macros again which then causes problems.
What I would like to do is add some extra code that ensures the ‘Page Setup’ button and its associated macros can only be run once.

I do not want the picture to be changed and all I would like is a message box or similar pop up that tells the users that the ‘Page Setup’ button has already been clicked and the associated macros have run; i.e. the button is deactivated after being run once.

Any help to achieve this would be appreciated.

Dave T

Bob Phillips
06-05-2014, 02:12 AM
The best way would be to add a callback for the visible property, which entails adding amending the XML, and adding OnLoad code and visibility callback code.

The XML would now look like this

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="rxOnload">

<ribbon startFromScratch="false">


label="Test Menu">


label="Page Setup"
description="HLR Page Setup"
screentip="HLR Page Setup"
supertip="Formats page view, insert signature of user and inserts the current date."


description="Dummy Signature"
screentip="Dummy Signature"
supertip="Inserts Dummy Signature."/>

The ribbon code, probably best in its own module, would be

Option Explicit

Public myRibbon As IRibbonUI

Sub rxOnload(ribbon As IRibbonUI)
Set myRibbon = ribbon
End Sub

'Callback for HLR_PageSetup getVisible
Sub rxGetVisible(control As IRibbonControl, ByRef returnedVal)

Select Case control.ID

Case "HLR_PageSetup": returnedVal = Not visPageSetup
End Select
End Sub

and finally, amend your procedure to force the ribbon to be rebuilt (with the button hidden)

Public visPageSetup As Boolean

Sub HLR_PageSetup()
' Resets page margins and zoom
Dim Win As Window
With Selection.PageSetup
.Orientation = wdOrientPortrait
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.TopMargin = CentimetersToPoints(1.4)
.BottomMargin = CentimetersToPoints(1.4)
.LeftMargin = CentimetersToPoints(2.4)
.RightMargin = CentimetersToPoints(2.4)
.HeaderDistance = CentimetersToPoints(1)
.FooterDistance = CentimetersToPoints(1)
End With

' Go to end of document and change font size to 2 points
Selection.EndKey Unit:=wdStory
With Selection.Font
.Size = 2
End With

'Move up 3 lines and insert signature
Selection.MoveUp Unit:=wdLine, count:=3
Call DummySignature 'Insert Signature
Call InsertDateToday 'Inerts today's date
'Call IfBoldKeepWithNext ''Keep with Next' is applied to any bold text
'Call RemoveTrailingWhitespace 'Removes extra whitespace after 'REPORT' in heading

' Go back to start of document
Selection.HomeKey Unit:=wdStory

visPageSetup = True
End Sub

06-05-2014, 07:24 PM
Another way --

in Signatures ...

Option Explicit
Public oRibbon As IRibbonUI
Public bBeenRun As Boolean

'Callback for customUI.onLoad
Public Sub Ribbon_OnLoad(ribbon As IRibbonUI)
Set oRibbon = ribbon
bBeenRun = False
End Sub

'Callback for HLR_PageSetup getEnabled
Sub AlreadyRun(control As IRibbonControl, ByRef returnedVal)
returnedVal = Not bBeenRun
End Sub

'Callback for HLR_PageSetup getScreentip
Sub PageSetupTip(control As IRibbonControl, ByRef returnedVal)
Dim b As Boolean
Call AlreadyRun(control, b)
returnedVal = IIf(b, "Click to run PageSetup", "PageSetup can only be run once")
End Sub

In PageSetup

'Callback for 'HLR Page Setup' onAction
Sub rxbtnHLR_PageSetup_click(control As IRibbonControl)

bBeenRun = True
oRibbon.InvalidateControl ("HLR_PageSetup")

Call HLR_PageSetup
MsgBox "pagesetup run"
End Sub

Tweak to the CustomUI XML to use getEnabled and getScreentip callbacks

label="Page Setup"
description="HLR Page Setup"
getEnabled = "AlreadyRun"
supertip="Formats page view, insert signature of user and inserts the current date."/>

I attached my effort, but rename off the .zip part if you want to see it

You can 'program around' the Boolean flag, I use things like that to keep things obvious (I get myself into trouble when I try to be TOO clever)

Dave T
06-05-2014, 07:54 PM
Hello xld,

Hello xld,

Thank you, I appreciate your reply.

To test your suggestions I created a new document and added the code in the appropriate places, but for some reason whenever I click on either of the two buttons on the tab I get the following message:

The macro cannot be found or has been disabled because of your Macro security settings.

I have even saved the new document to the templates folder on my C drive and the error still occurs, do you know why this is happening ???


Hello Paul,

Thank you, thank you,

Your suggestions run perfectly.
I will have to look closely at both suggestions to learn from what each of you has done.

I really appreciate both of you offering me help.

Dave T

Dave T
06-17-2014, 08:51 PM
Hello xld,

Can either you or someone else please explain to me why I get a macro warning when I created a new Word template and copied your suggestions into it.

I have attached a copy of my new template and just added .zip to the end; so to open my attachment just delete the .zip part.

Dave T

Bob Phillips
06-18-2014, 08:22 AM
Works fine for me Dave. The Setup button gets hidden when clicked, and the signature button inserts some text.