Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-22
3
Medium Priority
?
1,117 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

916 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