Solved

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

Posted on 2014-04-22
3
1,002 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

763 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