Consulting

Results 1 to 5 of 5

Thread: Make Font Bold On Document If Option "YES" Selected

  1. #1
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    367
    Location

    Make Font Bold On Document If Option "YES" Selected

    I have a number of option buttons which show on the document as either "NO" or "YES". What I am trying to achieve is if the option is answered "YES", then that word is made bold on the document, leaving the option "NO" in normal weight.

    This is a section of Options that I have which work okay in the form, but minus the Bold text if the answer is "YES".

    If .optDeliveredNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range.Text = "NO"
            Else
                ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range.Text = "YES"
            End If
    If .optEvidenceNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Evidence").Item(1).Range.Text = "NO"
            Else
                ActiveDocument.SelectContentControlsByTag("Evidence").Item(1).Range.Text = "YES"
            End If
    If .optEmployeeNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Employee").Item(1).Range.Text = "NO"
            Else
                ActiveDocument.SelectContentControlsByTag("Employee").Item(1).Range.Text = "YES"
            End If
    If .optFundsNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Funds").Item(1).Range.Text = "NO"
            Else
                ActiveDocument.SelectContentControlsByTag("Funds").Item(1).Range.Text = "YES"
            End If

  2. #2
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    367
    Location
    I think that I have found the answer, but am not sure if it is the most efficient.

    If .optDeliveredNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range.Text = "NO"
                ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range.Bold = False
            Else
                ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range.Text = "YES"
                ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range.Bold = True
            End If
            If .optEvidenceNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Evidence").Item(1).Range.Text = "NO"
                ActiveDocument.SelectContentControlsByTag("Evidence").Item(1).Range.Bold = False
            Else
                ActiveDocument.SelectContentControlsByTag("Evidence").Item(1).Range.Text = "YES"
                ActiveDocument.SelectContentControlsByTag("Evidence").Item(1).Range.Bold = True
            End If
            If .optEmployeeNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Employee").Item(1).Range.Text = "NO"
                ActiveDocument.SelectContentControlsByTag("Employee").Item(1).Range.Bold = False
            Else
                ActiveDocument.SelectContentControlsByTag("Employee").Item(1).Range.Text = "YES"
                ActiveDocument.SelectContentControlsByTag("Employee").Item(1).Range.Bold = True
            End If
            If .optFundsNo.Value = True Then
                ActiveDocument.SelectContentControlsByTag("Funds").Item(1).Range.Text = "NO"
                ActiveDocument.SelectContentControlsByTag("Funds").Item(1).Range.Bold = False
            Else
                ActiveDocument.SelectContentControlsByTag("Funds").Item(1).Range.Text = "YES"
                ActiveDocument.SelectContentControlsByTag("Funds").Item(1).Range.Bold = True
            End If

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,393
    Location
    You could probably save some typing by using something like this. Not tested at all

    Performance-wise probably not much difference with either, but maintenance-wise I think the second option is easier to debug and modify

        With ActiveDocument.SelectContentControlsByTag("Delivered").Item(1).Range
            .Text = IIf(.optDeliveredNo.Value, "NO", "YES")
            .Bold = IIf(.optDeliveredNo.Value, False, True)
        End With
    or maybe

    Sub DoAll()
        Call SetOptions("Delivered")
        Call SetOptions("Evidence")
        Call SetOptions("Employee")
        Call SetOptions("Funds")
    End Sub
    
    
    
    Sub SetOptions(TheTag As String)
        With ActiveDocument.SelectContentControlsByTag(TheTag).Item(1).Range
            .Text = IIf(.optDeliveredNo.Value, "NO", "YES")
            .Bold = IIf(.optDeliveredNo.Value, False, True)
        End With
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    367
    Location
    Thanks Paul for having a look at this. My solution is working fine, so will probably stick with it. I did give your option one a go, but it threw up a compile error : method or data member not found on .optDeliveredNo

    Speaking about the speed issue, how does one test the performance of code within a dotm?

  5. #5
    VBAX Regular
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    78
    Location
    Quote Originally Posted by HTSCF Fareha View Post
    Thanks Paul for having a look at this. My solution is working fine, so will probably stick with it. I did give your option one a go, but it threw up a compile error : method or data member not found on .optDeliveredNo

    Speaking about the speed issue, how does one test the performance of code within a dotm?
    With a stopwatch and some [large] sample data. Or, you can incorporate a timer in your code, which adds additional overhead, if not much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •