Solved

Microsoft Word 2010 Conditional Printing

Posted on 2016-08-04
11
53 Views
Last Modified: 2016-08-23
Hi All:  In Word I am looking for a way to print or not print certain paragraphs based on the value (true/false) of a checkbox.

The document is a contract that has 10 checkboxes and each box is checked (or not checked) based on the contract requirements.  With each "Checked" checkbox there is an associated "Terms and Conditions" portion that must be included.  What I am looking to do is only print the Terms and Conditions that are associated with the item in the associated checkboxes that are "Checked".

I can put Terms and Conditions for each associated checkbox in individual .docx files if necessary.

Any help appreciated
0
Comment
Question by:cwbarrett
  • 6
  • 4
11 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 41743387
I think you might be approaching this in a roundabout kind of way.  Perhaps a sample document.  

Possible approaches:
Create a mail merge OR  Use quick parts (content controls)  OR  Use "hidden text".

Cheers,
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41743389
As Steve says, we really need to see a sample document.
However, I don't really see any way without using VBA.
0
 

Author Comment

by:cwbarrett
ID: 41744413
Attached is an example of what I am looking to do.  Salespeople will be using this as a Form so it is protected (no password).

Thank you
Experts-Exchange_Sample.docx
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41744776
This should work they way that you ask:
Option Explicit

Sub PrintMe()
    Dim tbl As Table
    Dim ffld As FormField
    Dim rw As Row
    Dim strUnwanted() As String
    Dim u As Integer
    Dim Doc As Document
    Dim rngService As Range
    
    Set Doc = ActiveDocument
    Set tbl = Doc.Tables(1)
    For Each rw In tbl.Rows
        If rw.Cells(2).Range.FormFields.Count > 0 Then
            Set ffld = rw.Cells(2).Range.FormFields(1)
            If ffld.CheckBox.Value = False Then
                ReDim Preserve strUnwanted(u)
                strUnwanted(u) = GetCellText(rw.Cells(1))
                u = u + 1
            End If
        End If
    Next rw
    If u > 0 Then
        If Doc.ProtectionType = wdAllowOnlyFormFields Then
            Doc.Unprotect ', password
        End If
        For u = 0 To UBound(strUnwanted)
            Set rngService = GetServiceTextRange(Doc, strUnwanted(u))
            rngService.Font.Hidden = True
        Next u
        Doc.Protect wdAllowOnlyFormFields, True  ', password
        Doc.PrintOut
        Doc.Unprotect ', password
        Doc.Range.Font.Hidden = False
        Doc.Protect wdAllowOnlyFormFields, True  ', password
    Else
        Doc.PrintOut
    End If
End Sub

Function GetCellText(cl As Cell) As String
    Dim rng As Range
    
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function
Function GetServiceTextRange(Doc As Document, strNotwanted As String) As Range
    Dim rng As Range
    Dim rng1 As Range
    
    Set rng = Doc.Range
    With rng.Find
        .Text = strNotwanted
        .Font.Underline = wdUnderlineSingle
        If Not .Execute() Then
            MsgBox strNotwanted & " text not found"
        Else
            Set GetServiceTextRange = rng.Duplicate
            GetServiceTextRange.Collapse wdCollapseEnd
            GetServiceTextRange.End = Doc.Range.End
            With GetServiceTextRange.Find
                .Text = "^p^p"
                If .Execute() Then
                    GetServiceTextRange.Start = rng.Start
                    GetServiceTextRange.Font.Hidden = True
                End If
            End With
        End If
    End With
End Function

Open in new window

1
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41744787
However, it might be better idea to keep optional text as a Building Blocks and to call it in as necessary. The code would be simpler and hence easier to maintain.
1
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cwbarrett
ID: 41744833
Wow, thank you.  Did not think it would use that much VB.  Going to have to sit down an study this one.  Going to have to get back to this in a couple weeks, starting my vacation today.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41744935
A slightly redesigned document would need simpler code. For instance the options text could be bookmarked. At the moment the code looks for the heading for the start of the block, and then for the next empty paragraph to end the block. A bookmark that covers the whole block would greatly simplify that operation.
0
 

Author Comment

by:cwbarrett
ID: 41745520
I see where a bookmark can cover a string of text.  Do you have an example of how I could make this work?  I don't do VB very well, I do better at VBA.
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 41745719
VB6 and VBA are the same; only the environment is different.  VBScript is similar. .Net can be very different.

In the Word user interface, simply select all the desired text and insert the bookmark.

In VB(A), you can get the text:
strText = doc.Bookmarks("mytext").Range.text

Open in new window


The text can be set like this:
Sub FillBookmark(doc As Document, strBookMark As String, strNewText As String)
    Dim rng As Range
    Set rng = doc.Bookmarks(strBookMark).Range
    rng.Text = strNewText
    doc.Bookmarks.Add strBookMark, rng 're-add the bookmark in case it was overwritten
End Sub

Open in new window

0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 41745722
To hide all the text contained in a bookmark:

    doc.Bookmarks.Range.Font.Hidden = True

Open in new window

0
 

Author Closing Comment

by:cwbarrett
ID: 41767082
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now