Writing to Word with VBA with inline formatting

MikeWT

New Member
Joined
Nov 13, 2012
Messages
44
Here's another challenging problem to solve.

I'm generating a word doc with a macro in excel that takes a list of cells and builds a table on a new word doc. If a cell is numeric, it puts it in column 2, but if it's not numeric it puts it in column 1, like this

Code:
Do This
Do That
123
321
Do this
456
Do that
654

results in:
<table col = 2 row = 3>
<tr><td>1. Do This</td><td></td></tr>
<tr><td>2. Do That</td><td>123<br>321</td></tr>
<tr><td>3. Do This</td><td>456</td></tr>
<tr><td>4. Do This</td><td>654</td></tr>
</table>

What I need to do is format just the "321" in the second row, let's say make it red and bold.

My objects are as follows:

Code:
    Set objWord = CreateObject("Word.Application")    Set objDoc = objWord.Documents.Add
    
    objWord.Visible = True
    
    Set objSelection = objWord.Selection
    Set objRange = objDoc.Range

    objDoc.Tables.Add objRange, 1, 4

    Set objTable = objDoc.Tables(1)

When I'm writing to a particular cell, I can append to the cell contents by using
Code:
objTable.cell(1,2).Range.Text = objTable.cell(1,2).Range.Text + theString

But then the string takes the same formatting as the rest of the cell. Is there a way to append a formatted object to a cell? If so, what kind of object and how do I format it?

I hope that's clear! It's not an easy one to word...

Thanks all!
-Mike
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top