Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-19-2015, 09:04 AM
Unk Unk is offline VBA and Word basics help Windows 7 64bit VBA and Word basics help Office 2010 64bit
Novice
VBA and Word basics help
 
Join Date: Apr 2015
Posts: 3
Unk is on a distinguished road
Default VBA and Word basics help

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
This is in

Code:
Project (NameOfMyDocument)
  MicrosoftWordObjects
     ThisDocument
On the page I have two controls, and the first one controls what the other one displays-- until I close the whole thing and open it again, at which point the above code is gone. The above code changes one control as you exit another, whereas what I really need is a control that changes a text field and its formatting.

Thanks in advance
Reply With Quote
  #2  
Old 04-19-2015, 09:47 PM
gmayor's Avatar
gmayor gmayor is offline VBA and Word basics help Windows 7 64bit VBA and Word basics help Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 04-19-2015, 10:07 PM
Unk Unk is offline VBA and Word basics help Windows 7 64bit VBA and Word basics help Office 2010 64bit
Novice
VBA and Word basics help
 
Join Date: Apr 2015
Posts: 3
Unk is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 04-19-2015, 11:20 PM
gmayor's Avatar
gmayor gmayor is offline VBA and Word basics help Windows 7 64bit VBA and Word basics help Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 04-19-2015, 11:35 PM
Unk Unk is offline VBA and Word basics help Windows 7 64bit VBA and Word basics help Office 2010 64bit
Novice
VBA and Word basics help
 
Join Date: Apr 2015
Posts: 3
Unk is on a distinguished road
Default

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
When it was only in the second one, that's when it was vanishing. In both cases it was a .docm, I haven't changed anything about the way I was saving it.

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.
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 08:28 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft