Link to home
Start Free TrialLog in
Avatar of cwbarrett
cwbarrett

asked on

Microsoft Word 2010 Conditional Printing

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
Avatar of DrTribos
DrTribos
Flag of Australia image

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,
Avatar of GrahamSkan
As Steve says, we really need to see a sample document.
However, I don't really see any way without using VBA.
Avatar of cwbarrett
cwbarrett

ASKER

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
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

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.
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.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To hide all the text contained in a bookmark:

    doc.Bookmarks.Range.Font.Hidden = True

Open in new window