Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Microsoft Word 2010 Conditional Printing

Posted on 2016-08-04
11
Medium Priority
?
112 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

927 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