Solved

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

Posted on 2014-04-22
3
981 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
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…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

770 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