PDA

View Full Version : [SOLVED:] Make Font Bold On Document If Option "YES" Selected



HTSCF Fareha
08-17-2022, 07:40 AM
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

HTSCF Fareha
08-17-2022, 10:22 PM
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

Paul_Hossler
08-18-2022, 06:52 PM
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

HTSCF Fareha
08-19-2022, 11:29 AM
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?

Chas Kenyon
08-22-2022, 08:30 AM
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.