![]() |
#1
|
|||
|
|||
![]()
Hello everyone,
I am currently working on a contract template. By clicking on a check box for additional services, certain paragraphs shall appear/disappear in the contract. e.g. by clicking on the checkbox "Hosting" in a software contract, additional terms for hosting services shall appear in the end of the contract. I have and ActiveX-Checkbox. But how can I link it in the VBA to a certain text paragraph? Shall this paragraph be a bookmarked Text oder a RichText-ContentControl? let's start with this: Private Sub CheckBox1_Click() I am no advanced Microsoft User, however I'd like to learn more on VBA etc. Any help appreciated. Thank you in advance Sebastian |
#2
|
|||
|
|||
![]()
Hi
You can use either a bookmark or a content control, that's entirely up to you and how you structure the document. To hide the text in a content control: ActiveDocument.ContentControls.Item(1).Range.Font. Hidden=1 To show the text in a content control: ActiveDocument.ContentControls.Item(1).Range.Font. Hidden=0 To hide text in a bookmark range: ActiveDocument.Bookmarks("BookmarkName").Range.fon t.hidden=1 To show text in a bookmark range: ActiveDocument.Bookmarks("BookmarkName").Range.fon t.hidden=0 To hide the hidden fonts from view: Activewindow.View.ShowHiddenText = False ActiveWindow.View.ShowAll=False If you toggle show/hide button you will still be able to see the hidden text, but it won't print. If you want the text completely removed, use bookmark ranges and input contents by automating autotext entries. Let me know if you need help with the later. |
#3
|
||||
|
||||
![]()
Simply toggling the font's hidden property is unreliable at best. Depending on how the user has Word configured, the text may remain visible and, even if it doesn't, might print anyway.
To do this properly, you need to conditionally insert/delete the text - including any formatting. One way is to use field coding (but even that is susceptible to unexpected behaviour if the user has word configured to display the field codes and/or print them). The safest way is to either hard-code the conditional content into a macro or a separate reference document, but that is also the least convenient approach.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
Great thanks Paul. I agree.
Sebastian, what I use to do was either bring in the content text from a document resource file to a bookmark location. Or Autotexts are great for content you don't change that you bring in conditionally. So the resource files are for text that may be edited from time to time, autotexts or quickparts are for building blocks. This stub gives you an idea what you can do. Capture your checkbox events then based on your logic bring in the elements or remove them. (I've just shown bringing them in.) Unfortunately, Bookmarks can easily be deleted or typed in accidentally in paragraphs. If you can use a table cell and put the bookmark around the table cell, that is better, i.e. not so easy to accidentally delete. Sub ImportDocElementsStub() Dim bCheckBoxA As Boolean, bCheckBoxB As Boolean, bCheckBoxC As Boolean Dim lRangeStart As Long, lRangeEnd As Long Dim sResourceFile As String Dim oDoc As Word.Document Dim oTemplate As Word.Template Dim oRange As Word.Range Const cBookmarkForAutoText As String = "AutotextElement" Const cBookmarkForResource As String = "ResourceElement" ' Reference if you need it may be to specific document Set oDoc = ActiveDocument ' You will need to reference the template with the autotexts ' whether that be the template attached to the document ' or a global template Set oTemplate = oDoc.AttachedTemplate ' The path to your resource file with the text formatted and ready sResourceFile = "C:\ResourceFile.docx" ' Determine if your checkboxes have been checked or not bCheckBoxA = True bCheckBoxB = True bCheckBoxC = True If bCheckBoxA Then Set oRange = oDoc.Bookmarks(cBookmarkForAutoText).Range ' Sample of how to insert a building block Set oRange = oTemplate.BuildingBlockEntries(cBookmarkForAutoTex t).Insert _ (oRange, RichText:=True) ' Add the bookmark back to the range oRange.Bookmarks.Add "AutoTextElement" End If ' Bookmark around a paragraph - NOT IDEAL If bCheckBoxB Then lRangeStart = oDoc.Bookmarks(cBookmarkForResource).Start ' So you don't loose the bookmark if around a paragraph lRangeEnd = oDoc.Bookmarks(cBookmarkForResource).End - 1 ' reference to range for text Set oRange = oDoc.Range(lRangeStart, lRangeEnd) ' Either you can open and copy the data ' or insertfile to a range oRange.InsertFile FileName:=sResourceFile End If 'if you can use a table cell, do so! If bCheckBoxC Then ' Bookmark around a single cell table Set oRange = oDoc.Bookmarks("TableCellBookmark").Range oRange.InsertFile sResourceFile End If Set oRange = Nothing Set oTemplate = Nothing Set oDoc = Nothing End Sub |
#5
|
|||
|
|||
![]()
Sebastian.. the structure of your document is so important to get this working.
Can you attach a document with dummy text? It's hard to work out a full solution without analysing the template. Thanks. (The stub may help with what you can do, it's the how that's important and that really depends on the design.) |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
subspace3 | Excel Programming | 5 | 07-09-2015 04:45 PM |
Multiple Check In/check Out Times | big0 | Excel | 4 | 09-19-2013 05:02 AM |
![]() |
learn2office | Word | 1 | 11-27-2012 02:02 AM |
Link word check box to access check box | Mrkieth | Word | 4 | 01-30-2012 06:43 AM |