Bold selected text generated in a userform

K

Ken Davis

I have a userform with 6 text boxes. I use a single bookmark to insert the
information into the active document. I'd like to be able to format (bold)
one of the entries as it is inserted in the document.

I've tried tbNAME.font.bold=true and it accepts the statement but it has
no effect on the output. I just don't have the familiarity with OOP and with
Word's internals to know how to proceed. Any light you could shed would be
appreciated.

Thanks in advance! --Ken Davis

VB code is as follows:

Private Sub CommandButton1_Click()

With ActiveDocument

For varCounter1 = 1 To 12 ' varCounter1 increments the inserted text to
cycle through each cell
' Set increment for the number of fields
(bookmarks) in the main document

'VarCellNumber passes a string to append to "bkCELL" which is the base
mnemonic for the cell bookmarks
'this assumes fewer than 20 cells
If varCounter1 < 10 Then varCellNumber = Chr(48 + varCounter1)
If varCounter1 > 9 Then varCellNumber = "1" + Chr(48 + (varCounter1 - 10))

'Actually inserts UserForm2 text into fields. Note that fields are
inserted in reverse order (like pushing onto a stack)
'If statements ignore and/or properly format lines with blank entries
If (tbFACEBOOK) <> "" Then .Bookmarks("bkCELL" +
varCellNumber).Range.InsertBefore Chr(10) + (tbFACEBOOK)

If (tbMYSPACE) <> "" Then .Bookmarks("bkCELL" +
varCellNumber).Range.InsertBefore Chr(10) + (tbMYSPACE)

If (tbIM <> "") Then .Bookmarks("bkCELL" +
varCellNumber).Range.InsertBefore Chr(10) + (tbIM)

If (tbPHONE <> "") And (tbEMAIL <> "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbEMAIL)
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(10) +
(tbPHONE) + Chr(9)
ElseIf (tbPHONE <> "") And (tbEMAIL = "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(10) +
(tbPHONE)
ElseIf (tbEMAIL <> "") And (tbPHONE = "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(10) +
(tbEMAIL)
End If

If (tbNAME <> "") Then
tbNAME.Font.Bold = True
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbNAME)
'Chr(10) + (tbNAME)
End If

Next varCounter1

End With

UserForm2.Hide

End Sub
 
D

Doug Robbins - Word MVP

I would use { DOCVARIABLE } fields to hold the information in the document
and then use the \* charformat switch on the field that you want to appear
as bold, which you achieve by having the D of DOCVARIABLE formatted as bold.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Ken Davis

Doug,

Thanks for the clue. I added the following field: {DOCVARIABLE varName
\* MERGEFORMAT} which I bolded just the D of Docvariable followed by my
bookmark for each cell.

I added the following statements to the top of the code block:

'write tbNAME value to docvariable "varName"
ActiveDocument.Variables("varName").Value = tbNAME
If tbNAME = "" Then ActiveDocument.Variables("varNAME").Value = "dummy"
If tbNAME = "" Then ActiveDocument.Variables("varNAME").Value = ""

If I didn't assign varName.value to some non-blank value 1st it gave me an
error (unassigned value) so if it ultimately was null I had to assign a
non-null value and then the null. Weird but whatever works...

After the code block (just before the end with activedocument) I added

..Fields.Update
..Fields.ToggleShowCodes

to update the values in the active document and to keep from freaking out my
users.

I'm posting the entire code block in hopes that it proves useful to others.
The entire code now is as follows:

Private Sub CommandButton1_Click()

With ActiveDocument

'write tbNAME value to docvariable "varName"
ActiveDocument.Variables("varName").Value = tbNAME
If tbNAME = "" Then ActiveDocument.Variables("varNAME").Value = "dummy"
If tbNAME = "" Then ActiveDocument.Variables("varNAME").Value = ""
'Now write the rest of the variables to bookmarks

For varCounter1 = 1 To 12 ' varCounter1 increments the inserted text to
cycle through each cell
' Set increment for the number of fields
(bookmarks) in the main document

'VarCellNumber passes a string to append to "bkCELL" which is the base
mnemonic for the cell bookmarks
'this assumes fewer than 20 cells
If varCounter1 < 10 Then varCellNumber = Chr(48 + varCounter1)
If varCounter1 > 9 Then varCellNumber = "1" + Chr(48 + (varCounter1 - 10))

'Actually inserts UserForm2 text into fields. Note that fields are
inserted in reverse order (like pushing onto a stack)
'If statements ignore and/or properly format lines with blank entries
If (tbFACEBOOK) <> "" Then .Bookmarks("bkCELL" +
varCellNumber).Range.InsertBefore Chr(10) + (tbFACEBOOK)

If (tbMYSPACE) <> "" Then .Bookmarks("bkCELL" +
varCellNumber).Range.InsertBefore Chr(10) + (tbMYSPACE)

If (tbIM <> "") Then .Bookmarks("bkCELL" +
varCellNumber).Range.InsertBefore Chr(10) + (tbIM)

If (tbNAME <> "") Then
If (tbPHONE <> "") And (tbEMAIL <> "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbEMAIL)
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(10)
+ Chr(9) + (tbPHONE) + Chr(9)
ElseIf (tbPHONE <> "") And (tbEMAIL = "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(10)
+ (tbPHONE)
ElseIf (tbEMAIL <> "") And (tbPHONE = "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(10)
+ (tbEMAIL)
End If
Else
If (tbPHONE <> "") And (tbEMAIL <> "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbEMAIL)
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore Chr(9) +
(tbPHONE) + Chr(9)
ElseIf (tbPHONE <> "") And (tbEMAIL = "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbPHONE)
ElseIf (tbEMAIL <> "") And (tbPHONE = "") Then
.Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbEMAIL)
End If
End If


' If (tbNAME <> "") Then
' tbNAME.Font.Bold = True
' .Bookmarks("bkCELL" + varCellNumber).Range.InsertBefore (tbNAME)
'Chr(10) + (tbNAME)
' End If

Next varCounter1
..Fields.Update
..Fields.ToggleShowCodes

End With

UserForm2.Hide

End Sub
 

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