Solved

How Do I Make a Word Bold in a Phrase When Automating Word in VB.Net?

Posted on 2014-04-22
3
951 Views
Last Modified: 2014-04-22
I am building a Table using Word Automation in VB.Net and am currently filling the cells with strings. In this particular case the cell contains name and address information. I would like to bold the name and leave the remainder alone. I would like to do this on the fly and not have to search later. I think it would be possible to use a bookmark at the beginning of the cell and then again at the end of the cell and then search between bookmarks for something like square brackets enclosing the name. When found, the brackets would be removed and the text changed to bold. This could be done when the cell is filled and then repeated as other cells are filled. My problem here is I can't figure out how to set the bookmarks. Also, This might be a really dumb way to do it since I'm really good at doing dumb things.
So, what is the best way to do this? Thanks for your help.
0
Comment
Question by:rkulp
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40015326
This code assumes and you are filling the cell with text where the name is separate from the rest of the text. It is a VBA macro, so has the word 'Set' on line 5. This is unwanted in .Net.
Sub FillCell(strLeadingText As String, strName As String, strTrailingText As String, cl As Word.Cell)
    Dim rng As Word.Range
    
    cl.Range.Text = strLeadingText & strName & strTrailingText
    Set rng = cl.Range
    rng.Font.Bold = False
    rng.Start = cl.Range.Start + Len(strLeadingText)
    rng.End = rng.Start + Len(strName)
    rng.Font.Bold = True
End Sub 

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 40015335
There will be an equivalent syntax for .net but I don't know it but one prospect is for example two commands:

activedocument.Tables(1).Cell(2,2).Range.Text = "fred and Doris"
activedocument.Tables(1).Cell(2,2).Range.Words(1).Font.Bold = true

Chris
0
 
LVL 1

Author Closing Comment

by:rkulp
ID: 40015455
Thank you very much. It worked perfectly.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now