Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-01-2018, 11:25 AM
Fouko Fouko is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Novice
Show chapter by checkbox
 
Join Date: Aug 2018
Posts: 8
Fouko is on a distinguished road
Default Show chapter by checkbox

Hi,

in fact I have 2 requests :
I have a text with 10 chapters
First, I need to have 10 checkbox to choice which chapters I need to show in the final document.
Secondly, those chapters need to have fields that will be completed with informations contained in a table located in the top of the first page.

is it possible to do something like that in Word VBA ? Is someone can share part of code ?



Thanks a lot
Attached Files
File Type: docx CheckboxToShowHideChapters.docx (13.1 KB, 19 views)
Reply With Quote
  #2  
Old 08-01-2018, 08:33 PM
gmayor's Avatar
gmayor gmayor is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

If you bookmark the chapters and title the checkboxes (as in the attached) and run the macro, it will create a document with those checked chapters with the text replaced from the table.

If you add to or change the texts ensure the content control and bookmark naming is maintained.

If you want the new document to have consecutively numbered chapters then you will need to set a counter and replace the number (i.e. remove the apostrophes from the commented out section).
Attached Files
File Type: dotm CheckboxToShowHideChapters.dotm (29.0 KB, 27 views)
__________________
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 08-03-2018, 06:08 AM
Fouko Fouko is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Novice
Show chapter by checkbox
 
Join Date: Aug 2018
Posts: 8
Fouko is on a distinguished road
Default

Thanks a lot for your code.

I'haven't really well understand what you explain here :
Quote:
If you want the new document to have consecutively numbered chapters then you will need to set a counter and replace the number (i.e. remove the apostrophes from the commented out section).
Also, to add more capability, how can I do if i need to keep in place the end of the document without the need of checkbox :
ie
------------------------------------
Chapter 1 - Choiced by checkbox
------------------------------------
Chapter 3 - Choiced by checkbox
------------------------------------
--------- to------------------------
------------------------------------
Chapter 12 - Choiced by checkbox
------------------------------------
End of the doc - on every document, not need to checkbox
------------------------------------

And lastly, how to put a condition that chapter 1 and/or 2 need to be checked to validate the document ?

it's really a great help, thanks a lot, you're writing VBA like you speak. It's not so easy for me.

Martial
Reply With Quote
  #4  
Old 08-03-2018, 07:32 AM
gmayor's Avatar
gmayor gmayor is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

If you look at the main macro, you will see a section that appears green coloured. That code is commented out and does nothing. If you remove the apostrophes from the start of each green coloured line that code will be run along with the rest of the macro to renumber the chapters.


To add your fixed code (which should be bookmarked in the source template also) locate the line

Code:
Next oCC
and immediately after it add
Code:
 'Add this code
        Set oRng = oDoc.Range
        oRng.Collapse 0
        oRng.FormattedText = oSource.Bookmarks("The name of the bookmark with the fixed text").Range.FormattedText
Put the bookmark name of the fixed text where indicated.
__________________
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 08-03-2018, 08:23 AM
Fouko Fouko is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Novice
Show chapter by checkbox
 
Join Date: Aug 2018
Posts: 8
Fouko is on a distinguished road
Default

I let this message, but see the update in the next one.

Great and quick answer, that's work well !

I tried by myself to add introduction text at the top of document by adding your code with some modification, but nothing appears even there is no error. The introduction part have the bookmark name bmStartChapter.

I put this code just after the Dim list section

Code:
'Add introduction text at the beginning
        Set oSource = ThisDocument
        Set oDoc = Documents.Add(oSource.FullName)
        Set oRng = oDoc.Range
        oRng.Collapse 0
        oRng.FormattedText = oSource.Bookmarks("bmStartChapter").Range.FormattedText
Reply With Quote
  #6  
Old 08-03-2018, 09:44 AM
Fouko Fouko is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Novice
Show chapter by checkbox
 
Join Date: Aug 2018
Posts: 8
Fouko is on a distinguished road
Default

I found my error

In fact I need to put the code after the line :
Code:
        oDoc.Range.Text = ""
And that works !!!

Any ideas how to put this condition that chapter 1 and/or 2 need to be checked to validate the document ? Without this condition, the macro send a message.

Thanks, I learn quickly with you.
Reply With Quote
  #7  
Old 08-03-2018, 08:28 PM
gmayor's Avatar
gmayor gmayor is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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 uses the source template to create the new document and the
Code:
oDoc.Range.Text = ""
line ensures that the text in that document is removed first. The remaining bookmarks are added at the end of the document.


If you need Chapter 1 and Chapter 2 to be included always, the simplest approach is to check the two check boxes and change their properties to check the option 'Contents cannot be edited'. Then they cannot be changed from the template.


The lines
Code:
    Else
        MsgBox "No checkboxes were checked"
are then superfluous.
__________________
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
  #8  
Old 08-05-2018, 03:16 PM
Fouko Fouko is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Novice
Show chapter by checkbox
 
Join Date: Aug 2018
Posts: 8
Fouko is on a distinguished road
Default

Thanks, everything works well

Quote:
the simplest approach is to check the two check boxes and change their properties to check the option 'Contents cannot be edited'. Then they cannot be changed from the template
To be clear, I need to test if checkbox 1 or/and Checkbox 2 à true
To validate the document, One of them or both must be checked.

Thanks
Reply With Quote
  #9  
Old 08-05-2018, 08:00 PM
gmayor's Avatar
gmayor gmayor is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

In that case add the following immediately after the Set oSource = ThisDocument line
Code:
If oSource.SelectContentControlsByTitle("Chapter1").Item(1).Checked = False And _
       oSource.SelectContentControlsByTitle("Chapter2").Item(1).Checked = False Then
        MsgBox "Chapter 1 and/or Chapter 2 must be selected"
        GoTo lbl_Exit
    End If
__________________
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
  #10  
Old 08-10-2018, 04:00 AM
Fouko Fouko is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Novice
Show chapter by checkbox
 
Join Date: Aug 2018
Posts: 8
Fouko is on a distinguished road
Default

You're the one, thanks a lot
I have separated in 2 check,
  1. Chapter 1 OR Chapter 2 need to be checked
  2. Only Chapter 1 or Chapter 2, not together have to be checked

After
Code:
Set oSource = ThisDocument
I add :
Code:
'test si les chapter 1 ou 2 sont cochés
    If oSource.SelectContentControlsByTitle("Chapter1").Item(1).Checked = False And _
       oSource.SelectContentControlsByTitle("Chapter2").Item(1).Checked = False Then
        MsgBox "Les cases 1 ou 2 doivent être cochées"
        GoTo lbl_Exit
    End If
    
     'test si les chapter 1 et 2 sont cochés
    If oSource.SelectContentControlsByTitle("Chapter1").Item(1).Checked = True And _
       oSource.SelectContentControlsByTitle("Chapter2").Item(1).Checked = True Then
        MsgBox "Les cases 1 et 2 ne doivent pas être cochées ensemble"
        GoTo lbl_Exit
    End If
I'm really happy to learn that
Reply With Quote
  #11  
Old 08-15-2018, 12:31 AM
Zhibek Zhibek is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2013
Advanced Beginner
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Hello, excuse me, but how did you create the arrow which shows and close the chapter? I sign it in green round.
Thank you in advance
Attached Images
File Type: png screen2.png (46.6 KB, 22 views)
Reply With Quote
  #12  
Old 08-15-2018, 02:24 AM
gmayor's Avatar
gmayor gmayor is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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 'arrow' appears when you hover your cursor over a paragraph to which a heading style has been applied. It is a built-in Word indicator.
__________________
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
  #13  
Old 08-15-2018, 03:39 AM
Zhibek Zhibek is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2013
Advanced Beginner
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
The 'arrow' appears when you hover your cursor over a paragraph to which a heading style has been applied. It is a built-in Word indicator.
Graham,
I can't find it.
Where is that "arrow"?
Where is word indicator located?
Reply With Quote
  #14  
Old 08-15-2018, 04:28 AM
gmayor's Avatar
gmayor gmayor is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
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

It is built into the heading styles. Chapter 1 has a heading style applied to it. If you hover the cursor over the paragraph with that style it will appear for the duration that the cursor is present.
__________________
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
  #15  
Old 08-15-2018, 07:45 PM
Zhibek Zhibek is offline Show chapter by checkbox Windows 10 Show chapter by checkbox Office 2013
Advanced Beginner
 
Join Date: Aug 2018
Location: Almaty, Kazakhstan
Posts: 30
Zhibek is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
It is built into the heading styles. Chapter 1 has a heading style applied to it. If you hover the cursor over the paragraph with that style it will appear for the duration that the cursor is present.
aaa I understood
Thank you so much!)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bookmark will not show/hide based on CC Checkbox lord_kaiser Word VBA 1 04-17-2018 01:19 AM
Show chapter by checkbox How to use checkbox to show/hide bookmarked text? namrehx Word VBA 16 12-14-2017 01:45 PM
How to use checkbox to show and hide bookmarked text? namrehx Word VBA 1 12-12-2017 02:17 PM
Show chapter by checkbox Show Userform when Content Control CheckBox ticked derajlance Word VBA 1 05-13-2016 01:55 PM
Show chapter by checkbox Show/Hide Text based on Checkbox Selection tammytran105 Word VBA 7 10-02-2014 04:30 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:58 PM.


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