Passing font property using a variable

E

Elessvie

Hello, folks --

Is it possible to pass a font property to a procedure using a variable? In
the procedure snippet below, I use (or try to use) “fntAttrib†to hold a
value like “bold†or “italicâ€. I assume the data type must be String. Is
this wrong?

Here is relevant part of the procedure:

Sub SearchForTerm(ByVal fntAttrib As String)
<snip>
If .Found Then
With oRng
.Font.fntAttrib = True
<snip>

To call it, I am using a button in a UserForm with the code below, passing
"Underline" to replace "fnt.Attribute" above:

Private Sub cmdTestingOnly_Click()
SearchForTerm ("Underline")
End Sub

When trying to run it, the error message "Method or data member not found"
appears on this and .fntAttribute is highlighted.

If someone could set me straight on this, I’d be really grateful. If I
haven't given enough information, please let me know.

Thank you very much,

-Lynne
 
J

Jay Freedman

Hi Lynne,

You can pass a string, but you can't use it directly as a member of a Font
object (or any object). VBA just doesn't know how to interpret that.

What you can do is set up a Select Case that uses the passed string to
determine which case to execute, something like this:

If .Found Then
With oRng
Select Case LCase(fntAttrib)
Case "bold"
.Font.Bold = True

Case "italic"
.Font.Italic = True

Case "underline"
.Font.Underline = wdUnderlineSingle
' ...
Case Else
' do nothing
End Select
End With
End If

Note the use of the LCase function to get the all-lower-case equivalent of
whatever was passed as fntAttrib, because the Select Case is case-sensitive
("Underline" won't match "underline"). And I wish there were not two
completely different meanings of the term "case" in the same sentence!

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top