Solved

Microsoft Word 2010 Conditional Printing

Posted on 2016-08-04
11
94 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

632 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