Formatting words within a string inserted in Word using VBA (1 Viewer)

Zedster

Registered User.
Local time
Today, 12:36
Joined
Jul 2, 2019
Messages
169
I am using Access to insert a string into a table cell in a Word using VBA.

The string is of the format: "Subject 1: text goes here & vbCrLf & Subject 2: More text goes here & vbCrLf & Subject 3: Yet more text goes here"

In the word document I would like it to appear like this:

Subject 1: text goes here
Subject 2: More text goes here
Subject 3: Yet more text goes here

I confess to not even knowing how to approach it because I need to reference specific points in the string to apply formatting rather than specific points in the word document.

Would anyone have any ideas?
 

Minty

AWF VIP
Local time
Today, 12:36
Joined
Jul 26, 2013
Messages
10,368
You would need to use rich text formatting.
So your example in a rich text box would be stored as

Code:
<div><font face="Droid Sans" size=4 color="#DEDEDE"><strong>Subject 1:</strong>&nbsp;text goes here<br>
<strong>Subject 2:</strong>&nbsp;More text goes here<br>
<strong>Subject 3:</strong>&nbsp;Yet more text goes here</font> </div>

The colour code is for a light grey, but you get the idea.
 

Zedster

Registered User.
Local time
Today, 12:36
Joined
Jul 2, 2019
Messages
169
You would need to use rich text formatting.
So your example in a rich text box would be stored as

Code:
<div><font face="Droid Sans" size=4 color="#DEDEDE"><strong>Subject 1:</strong>&nbsp;text goes here<br>
<strong>Subject 2:</strong>&nbsp;More text goes here<br>
<strong>Subject 3:</strong>&nbsp;Yet more text goes here</font> </div>

The colour code is for a light grey, but you get the idea.

How do I use rich text formatting? I tried inserting the HTML code you suggest in the text string I am inserting but all that happens is the HTML tags appear in the word document.
 

sxschech

Registered User.
Local time
Today, 04:36
Joined
Mar 2, 2010
Messages
792
Here is some code to unbold text in a cell of a word table. The Sub has the optional paramater BoldCell which will change it to bold rather than unbold by adding TRUE at the end of the parameter list. Since I use it mainly to unbold, you may need to further modify it to do the opposite.

I copied and pasted snippets of the code I use. Hope it is enough to go on. This is how it is called from the code that is looking at the word table cells. Also included links from where I found the code.

Code:
With worddoc.Tables(1)
       ...
       UnboldInCell worddoc, RemoveAndClean(.cell(2, 1).Range.Text, "Subject:"), 2, 1
...
...
End With

Code:
Sub UnboldInCell(worddoc As Object, boldtext As String, r As Integer, c As Integer, Optional BoldCell As Boolean)
'Based solution on how to unbold text in a cell rather than
'document from here:
'https://stackoverflow.com/questions/36729431/using-word-vba-to-format-parts-of-table-cell-text
'and combined with code style used in msReplaceCellData and UnboldText in this
'code module
'20190531
'Further refined by paragraph: Finding text without changing the selection
'because the previous code was changing text throughout the document rather
'than the cell.  So far, this seems to change only the text in the portion
'of the document that we want to unbold.
'https://docs.microsoft.com/en-us/office/vba/word/concepts/customizing-word/finding-and-replacing-text-or-formatting
'20190604
'Added optional BoldCell so that can use same function to bold text
'20200205
    Dim WordRangeCell As Object
    Set WordRangeCell = worddoc.Tables(1).cell(r, c).Range
    
    With WordRangeCell.Find
        .Text = boldtext '"Updated Tunnel"
        .Forward = True
        .Execute
        If BoldCell = False Then
            If .Found = True Then .Parent.Bold = False
        Else
            If .Found = True Then .Parent.Bold = True
        End If
    End With
End Sub

Other supporting function

Code:
Function RemoveAndClean(WordToClean As String, WordToRemove As String)
'Try to get rid of extra spaces, tabs, crlf
'20190122
    Dim stWord As String
    Dim intWordPosition As Integer
    
    stWord = TrimAll(Mid(WordToClean, Len(WordToRemove)))
    stWord = Replace(stWord, Chr(7), "")
    stWord = Replace(stWord, Chr(13), "")
    If Left(stWord, 1) = ":" Then
        stWord = Trim(Mid(stWord, 2))
    End If
    RemoveAndClean = CleanString(stWord)
End Function
 

bastanu

AWF VIP
Local time
Today, 04:36
Joined
Apr 13, 2010
Messages
1,402
I think you need to show us how you do the inserting of the string into the Word table cell.

Cheers,
 

conception_native_0123

Well-known member
Local time
Today, 06:36
Joined
Mar 13, 2021
Messages
1,833
I think you need to show us how you do the inserting of the string into the Word table cell.

Cheers,
Not only that, but word itself has an object model that you can use to insert bold text and plain text anywhere you want in a word document. It is my guess that you are doing just that at the moment. And if you are, inserting bold text should be no problem using the word object model. Another thing you can do, which is pretty simple, is record a macro in Microsoft word doing exactly what you're doing now from access, and simply copying the code that word produces. I do that all the time because I don't like using Microsoft word. I make the computer do the work so I don't have to. Usually works out perfectly. Lol.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:36
Joined
May 7, 2009
Messages
19,230
Code:
'https://stackoverflow.com/questions/4457050/using-vba-for-word-to-select-text-and-make-it-bold
Sub MakeTextBold(ParamArray text() As Variant)
'
' a Macro
'
'
'Dim A(3) As String
Dim i As Integer
Dim rng As Range
Set rng = ActiveDocument.Content
For i = 0 To UBound(text)
'    Selection.Find.ClearFormatting
'    Selection.Find.Replacement.ClearFormatting
'    With Selection.Find
    rng.Find.ClearFormatting
    rng.Find.Replacement.ClearFormatting
    With rng.Find
       .Forward = True
       .Wrap = wdFindStop
       .Format = False
       .MatchCase = False
       .MatchWholeWord = False
       .MatchWildcards = False
       .MatchSoundsLike = False
       .MatchAllWordForms = False
       .Replacement.Font.Bold = True

       .Execute FindText:=text(i), ReplaceWith:=text(i), Format:=True, _
         Replace:=wdReplaceAll

    End With
Next i
End Sub

to call:

Call MakeTextBold("Subject 1:", "Subject 2:", "Subject 3:")
 

conception_native_0123

Well-known member
Local time
Today, 06:36
Joined
Mar 13, 2021
Messages
1,833
LOL! way to research arne! good one. ha ha. coincidentally, the 2nd answer on SO is exactly the answer I gave here!
 

Users who are viewing this thread

Top Bottom