Solved

Microsoft Word 2010 Conditional Printing

Posted on 2016-08-04
11
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 15

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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