Link to home
Start Free TrialLog in
Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on

Word ActiveX button becomes disconnected from template

Building a template with numerous VBA automation features in Word 2016.  I typically use Ribbon buttons to launch macros, but in this case I am also using some ActiveX buttons to launch context-sensitive macros from within certain sections of the document.  For example, a button to display instructional text that relates to this particular section.

The code for this button is contained in the ThisDocument module of the template VBA project.  Button 1, for example, displays a userform with some instructional text that relates to the section where the button is located.  The user has two options when closing the userform:  1) close the userform and leave the calling button intact (in case they want to use it again), or 2) close  the userform and delete the ActiveX button that called the macro, because they won't need it again.   #2 works fine, closes the userform, finds the OLEControlObject button by name and deletes it.   But after doing that once, all of the other buttons in the document, each of which has
VBA onClick code in the template, now do nothing. Seems as if the buttons no longer know to find their code in the attached template.  When I go into design mode to look at the code behind any button, I get a brand new empty macro.  The template is still attached to the new document.

What am I doing wrong?

While I have you, any thoughts on a more elegant way to accomplish what I describe above (context-sensitive help or customization)?  I started by having a single ribbon button that discovers (via bookmarks) where the insertion point is, then displays the appropriate help text or customization options to match that section.  But I worry the user won't stop and hit all the necessary customization points or instructions, so I went this direction.

Thanks!
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

I think we need to see the code that does the delete, I presume it does it for more than the button itself...
Can you post the code snippet that deletes the button?
Avatar of Bryce Bassett

ASKER

Each of my ActiveX buttons has an action macro, then at the end they call this sub and pass the buttonname to it, so it can remove the calling button:
Sub killbuttonmacro(ByVal buttonname As String)

Selection.MoveStart

Dim objShape As InlineShape

For Each objShape In ActiveDocument.InlineShapes
    If objShape.Type = wdInlineShapeOLEControlObject Then
        If objShape.OLEFormat.Object.Name = buttonname Then
            objShape.Delete
            Exit For
        End If
    End If
Next objShape

killbutton = False

End Sub

Open in new window


I know it's only deleting the one button, because all the rest remain.

The crux of the problem is this:  The macros that control the actions of the buttons are in the (1) ThisDocument module of the template, but in the new Document spawned from the template, the buttons are looking for their code (and not finding it) in the ThisDocument module of the new document, not the template.  At least I assume that because when I go back into Design Mode and right click a button to view code, it takes me to an empty macro in (2) the ThisDocument module of the new doc.
User generated image.

In my experience, you can always call VBA macros that are contained within the template that spawned the activedocument, but that rule doesn't seem to apply here.   I seem to be missing something basic.
ASKER CERTIFIED SOLUTION
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Guy - the issue is that the macro code is not being carried over from the .dotm file to a newly created .docx file based on that template so there is no code to run when clicking on the ActiveX controls in the .docx. See the attached test file I create based on the originators question/code. As EE is famed for not allowing macro-enabled files to be uploaded (go figure on a software site!) just remove the .zip exentsion to reveal the digitally signed .dotm file.
EE-versatilebb.dotm.zip
Duh!  Of course.  I knew I was overlooking something basic.  I certainly could make the macros accessible from the ribbon, but the whole reason for the inline buttons is to call out places in the document where section instructions or customization macros are available to the user.  Unless you can suggest a better approach, I think I will have to (at least temporarily) create the new document as a .docm as suggested.    

Thanks for helping me figure this out.
To Jamie's point, I figured the save as wouldn't help, but that I can create a .docm directly from the template.  True?  I'll have a look at your zip file later.  Gotta run.  Thanks again.
Thanks.  I will look into the OrganizerCopy method.  For now, rather than add a new .docx based on the template, I'm having my code directly open the template then save as a .docm with a new name, so the buttons work.  Then I'll provide a button to save as a .docx when done.  It's a bit clumsy, but it works for now.