![]() |
|
#1
|
|||
|
|||
![]()
I've created a document with a control (a drop-down menu) that changes the content of a second control depending on what the user selects in the first one. I created the code for this in VBA, but as it stands now it all vanishes when I close the document, and I have to enter the code again.
What I need in the end is to create a control that allows readers of the document to select one of three types, and have text input fields on the page below change according to the type selected. One of these input fields should be formatted as an ol/ ordered list/ numbered list, so when the user types and hits return, a new numbered line appears. The other two types can just be free text entry fields. I've read all of this page, as well as all of the pages it links to: https://msdn.microsoft.com/en-us/library/3hekt07s.aspx but I'm still pretty much lost about how to even just save the document with the functionality that I've created so far. If someone could just walk me through some of the basics that would be a great help. Here's the VBA code: Code:
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) If ContentControl.Tag = "TypeSelect" Then Debug.Print ContentControl.Range Select Case ContentControl.Range Case "Task" ActiveDocument.ContentControls(2).Range = "Enter Steps" Case "Concept" ActiveDocument.ContentControls(2).Range = "Enter Descriptive Content" Case Else ActiveDocument.ContentControls(2).Range = "Enter Reference Data" End Select End If End Sub Code:
Project (NameOfMyDocument) MicrosoftWordObjects ThisDocument Thanks in advance |
#2
|
||||
|
||||
![]()
The code disappears almost certainly because you are saving the document as DOCX format which doesn't support macros, rather than DOCM format, which does.
Probably better still, save it as a macro enabled template (DOTM format) and create new documents from the template (File > New). If you want the macro to ignore the default text in the list box, then leave in the two optional lines. The Debug.Print ContentControl.Range line is not required other than for testing. Code:
Option Explicit Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) If ContentControl.Tag = "TypeSelect" Then If Not ContentControl.Range = ContentControl.PlaceholderText Then 'Optional Select Case ContentControl.Range Case "Task" ActiveDocument.ContentControls(2).Range = "Enter Steps" Case "Concept" ActiveDocument.ContentControls(2).Range = "Enter Descriptive Content" Case Else ActiveDocument.ContentControls(2).Range = "Enter Reference Data" End Select End If End If 'Optional with previous optional line End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
gmayor:
Thanks, I was saving it as a macro-enabled doc, the problem seems to have been that I was putting the code in the project (in the document, basically) rather than in a template. I moved it into the normal template and the code now stays when I close and reopen. I think creating a template for this document and then creating new ones from that will be the way to go, just learning about all that, Thanks very much for the advice about the placeholder text, that works. |
#4
|
||||
|
||||
![]()
This type of code should go into the document project and not the normal template, and that document must be saved as DOCM or DOTM.
![]()
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
![]()
Well that's interesting. That's how I had it originally. I saved it as a .docm, and when I opened it again, the code was gone. Which was what I was describing in the original post here. I would open the DOCM, the functionality was no longer working, and when I opened the Visual Basic Editor and looked at ThisDocument (under MyDocumentName), the code I had entered was no longer there.
Now I have it both there and in Normal, and it's still there when I open the docm again. The code is just as you show it, except now in both of these ThisDocument locations: Code:
Normal Microsoft Word Objects ThisDocument Project(MyDocumentName) Microsoft Word Objects ThisDocument I'll have more time to learn about this during the coming week, but it's good to have it working anyway, and I can go from there. Thanks. |
![]() |
|