Search code examples
vbauserform

VBA: Load only a UserForm from a different Document


Working in VBA here (complete beginner). I'd like a UserForm (e.g. "Master") in Doc1 to load a UserForm (e.g. "Apprentice) from Doc2 on a button click. I don't want Doc2 to open - only its UserForm.

After loading the "Apprentice" UserForm from Doc1, I want its buttons' actions to apply to Doc1. So for example, if the Apprentice UserForm has a button that adds a string of "Hello" to the Document, it should add "Hello" to Doc1 when clicked.

Hope this makes sense and apologies in advance if not


Solution

  • What you ask is not possible. In order to access and run code that's in a document, that document must be running in Word (open). VBA code can't run from a closed document.

    Perhaps what you need is a dotm (template) that you load as an add-in - the code is loaded but the file is not opened in the usual sense of the term. Normal.dotm acts in this sense as an add-in, for example, but is also accessible to the user. Any other template loaded as an add-in is not accessible to the user the same way Normal.dotm is, but the code it contains is available in the same way.

    The code in Doc1 can run code in an add-in, and the code in the add-in can work with any document currently open in the Word application.