Consulting

Results 1 to 6 of 6

Thread: Deactivate button after it has been run once

  1. #1
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location

    Deactivate button after it has been run once

    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.

    Regards,
    Dave T
    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    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">
    
    		<tabs>
    
    			<tab
    				id="Test_Menu"
    				label="Test Menu">
    
    				<group
    					id="HLR_Menu-Group-01"
    					label="Setup">
    
    					<button
    						id="HLR_PageSetup"
    						label="Page Setup"
    						showLabel="false"
    						size="large"
    						image="HLR-32x32"
    						onAction="rxbtnHLR_PageSetup_click"
    						description="HLR Page Setup"
    						screentip="HLR Page Setup"
    						supertip="Formats page view, insert signature of user and inserts the current date."
    						getVisible="rxGetVisible"/>
    				</group>
    
    				<group
    					id="Signatures"
    					label="Signatures">
    
    					<button
    						id="SignaturesGrp1Btn1"
                      			label="Dummy"
                      			showLabel="true"
                      			imageMso="CondolatoryEvent"
                      			onAction="rxbtnDummySignature_click"
                      			description="Dummy Signature"
                      			screentip="Dummy Signature"
                      			supertip="Inserts Dummy Signature."/>
    				</group>
    			</tab>
    		</tabs>
    	</ribbon>
    </customUI>
    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
      myRibbon.Invalidate
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    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

        <button
            id="HLR_PageSetup"
            label="Page Setup"
            showLabel="false"
            size="large"
            image="HLR-32x32"
            onAction="rxbtnHLR_PageSetup_click"
            description="HLR Page Setup"
            getEnabled = "AlreadyRun"
            getScreentip="PageSetupTip"
            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)
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    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.

    Regards,
    Dave T

  5. #5
    VBAX Contributor
    Joined
    Mar 2007
    Posts
    140
    Location
    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.

    Regards,
    Dave T
    Attached Files Attached Files

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Works fine for me Dave. The Setup button gets hidden when clicked, and the signature button inserts some text.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •