Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
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 …

837 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