Consulting

Results 1 to 11 of 11

Thread: How to create conditional content control in word using vba?

  1. #1

    How to create conditional content control in word using vba?

    Hello everyone.

    I am currently trying to create a macro template for examination paper that will strictly conditioning some section which needs to be input first before filling the others. Ok first of all. i have 3 common problems here that i got stucked with almost a month. Can't find any suitable tutorial for my needs so i decided to ask here if it's allowed.

    Here is my attacment file Test100_TESTING2.zip

    1) If you downloaded my attachment, as you can see i have 1 part where it writes "Instruction" and below it is a content control which i use to write a questions there. My problem is prioritize the instruction so that it MUST be filled first and CANNOT be null in order to write a questions on the content control below. This is one. Since it has related with VBA and may need high knowledge of VBA, i really need some help because i really weak on it.

    2) Second problem is i want to create a button on every end of the page, in order to let user create new section. This new section will have the same 2 type of content control like the existing one. This button will only show one input text and when the user click ok then button will gone, the text will replace the button instead and the 2 content control will be available below the previous button

    3) This is an extra and might not be related but, still if you guys help me i would appreciate it. if you see this line "THIS QUESTION PAPER CONTAINS 2 PAGES INCLUDING COVER PAGE" and the there is the number there. It's number of total pages. I have try them using numpages and stuff. The only problem is F9 is needed for it to be updated. I want it to be automatic updated but i cant find any suitable tutorial anywhere.


    Now i know im asking a lot and it seems like im asking you guys to do all of this for me. Well to be honest, this is just for experimenting word vba capabilities. Since i don't know much about vba code, i can't test it alone. I mean if it is really possible on doing that, it might be worth to try and ask from the masters. Trust me i try to search the google all the time for this with different keyword, but can't find one related to my questions. This is a forum right, its worth to try and open for discussion. Who knows this might help someone in need.

    God bless you everyone. Thanks for those willing to help. I will be waiting.
    Note: i am using microsoft word 2013

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    You can use something like this for your first issue. The code goes in the this document module. Give the first CC the title Main1. Give the second CC title Conditional1.

    Sorry but I don't have time right now to look at the other issues.

    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
      Select Case ContentControl.Title
        Case "Conditional1"
          If ActiveDocument.SelectContentControlsByTitle("Main1").Item(1).ShowingPlaceholderText Then
            ContentControl.LockContents = True
          Else
            ContentControl.LockContents = False
          End If
      End Select
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    The basic code for 2 and 3 could be as follows. Put it in a new module of your template. It puts a copy of the previous page before the 'button' and updates the fields.
    Name the 'button' content control "NewPage".

    Option Explicit
    
    Sub AddAPage()
    Dim orng As Range
    Dim oStory As Range
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious, Count:=1, Name:=""
        Set orng = ActiveDocument.Bookmarks("\page").Range
        orng.Copy
        orng.Collapse 0
        orng.Paste
        DoEvents
        orng.Select
        orng.Collapse 1
        For Each oStory In ActiveDocument.StoryRanges
            oStory.Fields.Update
            If oStory.StoryType <> wdMainTextStory Then
                While Not (oStory.NextStoryRange Is Nothing)
                    Set oStory = oStory.NextStoryRange
                    oStory.Fields.Update
                Wend
            End If
        Next oStory
        Set oStory = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Then you can add to Greg's macro to process the content control 'Button' when it is clicked.

    Option Explicit
    
    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
        Select Case ContentControl.Title
        Case "NewPage"
            AddAPage
        Case "Conditional1"
            If ActiveDocument.SelectContentControlsByTitle("Main1").Item(1).ShowingPlaceholderText Then
                ContentControl.LockContents = True
            Else
                ContentControl.LockContents = False
            End If
        End Select
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    If you copy and paste page one controls to a new page then you will need to adapt the OnExit event to point to the correct CC:

    Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    Dim lngIndex As Long
        Select Case ContentControl.Title
        Case "NewPage"
            AddAPage
        Case "Conditional1"
          lngIndex = Selection.Range.Information(wdActiveEndPageNumber)
            If ActiveDocument.SelectContentControlsByTitle("Main1").Item(lngIndex).ShowingPlaceholderText Then
                ContentControl.LockContents = True
            Else
                ContentControl.LockContents = False
            End If
        End Select
    lbl_Exit:
        Exit Sub
    End Sub
    Graham, headed off to classes. You code doesn't create a new page, just appends the copied text following the existing text (same page). Was that your intent?
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    First of all thanks gmaxey and gmayor. geez your name both are pretty similar. I thought the same person was posting over and over again. Thanks for the quick tips. I will try to implement them and give me a little time to understand it better. I will tell reply again in couple of hours to tell you guys my status. Thanks .

    God bless you..

  6. #6
    Quote Originally Posted by gmaxey View Post
    Your code doesn't create a new page, just appends the copied text following the existing text (same page). Was that your intent?
    With the OP's sample document, the content control in question is on the last page. The macro I posted copies the previous page and places it before the content control, so effectively it does create a new page.
    Quote Originally Posted by Oneloud91
    I thought the same person was posting over and over again.
    We have been confusing people with the similarity for many years, and make it all the more complicated by frequently collaborating with one another
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Test100_TESTING2.zip

    Yes there would be far less confusion if Mr. Mayor would change his name ;-)

    The code needed some work to ensure that the new controls added would function like the first two fixed controls. See attached.
    Greg

    Visit my website: http://gregmaxey.com

  8. #8
    Guys2. Thanks so much. You did solved it. Thanks. At least word+vba really possible to do this. I need to learn more about vba though. I just want the button to go to the next page with the existing content control because i am assuming most examination paper new section would go to next/fresh page rather than staying on the same page. Also i don't need to create the content control over and over again. Thanks. However, the total page number only updated when i clicked the button right? I did tried it. If i try to create new page just by using enter, it won't updated automatically.

    Is there any code that can make it update automatically whenever the page either increasing or decreasing? This is last question for this thread because i am going to create new thread for new questions anyway.

  9. #9
    oh and one more thing. I noticed something that code for adding new page is not working perfectly as i expected when the previous page is not the same page anymore. For example, the user write down the question and so on until it moves to the next page or more, then when clicking the button to add new page, it will take the previous page which means will definitely return the questions again, rather than the instruction.

  10. #10
    The macro I posted is based on the information and the document you originally supplied. This copies the previous 'page' to immediately before the content control that operates as a button. If you want it to copy a text of variable length that might flow to more than a page, then you need to create a container for that text that can be copied as a whole. The obvious choice is a table. You can then select the last table rather than the previous 'page'.

    Arguably a better option would be to use a building block to holds the repeatable information, but as building blocks must be stored in templates, this is not an ideal solution for forms you may be distributing. Come to that macros in documents create a problem also as there is no way to force users to allow them to run. However your supplied example is a template, so presumably you have considered the implications of using it. If you want to use a building block, save the page as an autotext entry in the template: Test100_TESTING2.zip

    Note there are no 'pages' in a Word document. It is a not a page layout application. If you use fields in the document to display the page count, then you are going to have to force an update to those fields, which, as you have found, do not update automatically. Because there are no pages, when text flows to a further page, there is nothing to trigger the page count.

    The macro I posted contains code to update all the fields in a document.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    Sorry i was back from vacation. Thank you for helping me. I think i got what you mean. and about the page numbering i guess i have to use F9 since i don't have any other choices. Anyway, my answer is solved. Thanks to you both.

Tags for this Thread

Posting Permissions

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