How to make bold font or italic in MS word using VBA

Hi all
I am using office 2003 and have designed the following macro which runs fine.  The code simply takes contents of each cell in column 3 and appends it to corresponding row in column 4.  At the end of the day column 3 is deleted.  I have added some titles for what was col 3 and what is col 4.  I need to bold these titles namely, Name and Description.  How can I make the font bold for just the itles or Italic them without affecting the rest of the text in the cell ?
Sub ModifyTheTableStructure()
  
Dim wd As Object
Set wd = CreateObject("Word.Application")
wd.Documents.Open FileName:="C:\test2.doc"
  
'if there is less or more than one table code will not run. need to format table
If wd.ActiveDocument.Tables.Count <> 1 Then
    wd.ActiveDocument.Close 0
    GoTo Done
End If
  
Dim strTableText As String
strTableText = ""
  
Dim r As Long
  
With wd.ActiveDocument.Tables(1)
       .Rows(1).Delete
       If .Rows.Count > 1 Then
       'go through remaining rows
        For r = 2 To .Rows.Count
            'store the cell in the third column to put in 4th
            .Cell(r, 4).Range.Text = "Name" & vbCrLf & .Cell(r, 4).Range.Text & "Description" & vbCrLf & .Cell(r, 3).Range.Text
        Next r
    Else
        'do nothing. there are no rows in the table
    End If
   'delete the third column
  .Columns(3).Delete
End With
  
'clean up & put carriag returns
  
Done:
Set wd = Nothing
End Sub

Open in new window

LVL 1
sherydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mcsilverCommented:
It sounds like you are putting the title text in the same cell as "data" text.  Is that true?
0
sherydAuthor Commented:
Yes, the title text is in the same cell as the data text.  I have tried this -
 .Cell(r, 4).Range.Text = Format("Name", bold) & vbCrLf & .Cell(r, 4).Range.Text.... but its not working.  I have also tried the following code attached. It bolds the first title fine but it does not always bold the Description title because sometimes I may have several names in several lines in under the Name title.  Any help will be well appreciated


Cell(r, 4).Range.Text = "Name" & vbCrLf & .Cell(r, 4).Range.Text & "Description" & vbCrLf & .Cell(r, 3).Range.Text
            .Cell(r, 4).Range.bold = False
            .Cell(r, 4).Range.Paragraphs(1).Range.bold = True
            .Cell(r, 4).Range.Paragraphs(4).Range.bold = True

Open in new window

0
mcsilverCommented:
Am I correct that the cell's text looks something like this just before the "Name" and "Description" labels must be bolded?

Name
John
Martha
[possibly more names...]
Description
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Proin sed mauris at dui ullamcorper commodo a eu erat.
[possibly more text...]

If so then try this:
Dim r As Integer
    r = 1
    
    ' Roundabout way of placing the cursor at the top
    ' of the desired cell.
    With ActiveDocument.Tables(1)
        .Cell(r, 4).Select
        Selection.MoveLeft Unit:=wdCharacter, Count:=1
    End With
    
    ' Bold "Name".
    ' Assumption: There is only one occurrence of "Name"
    ' in the cell.
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Name"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    If Selection.Find.Execute Then
        Selection.Font.Bold = True
    End If
    
    Selection.Find.ClearFormatting
    
    ' Bold "Description".
    ' Assumption: There is only one occurrence of "Description"
    ' in the cell.
    With Selection.Find
        .Text = "Description"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    If Selection.Find.Execute Then
        Selection.Font.Bold = True
    End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

sherydAuthor Commented:
Correct to say this is the format I have:
Name
John
Martha
[possibly more names...]
Description
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Proin sed mauris at dui ullamcorper commodo a eu erat.
[possibly more text...]

However, the assumption that there is only one Description is not always true.  In some but rare instances the word ' description' appears in text under the Description title.  Maybe I could do a search for a description that appears in its own line and bold this one and ignore the rest.  Is this possible , - unless there is a better way to do all this.
0
mcsilverCommented:
Word has a construct called a Bookmark, which you could possibly insert just before each title and then search for when bolding.  Each bookmark requires a name that is unique in the document, I believe.  However, maybe all you need to do is temporarily insert one bookmark per title text and then after you've used them to find and bold the title text for a single cell, you delete the bookmarks.

Here's some code that creates a bookmark at the current cursor location, goes to it and finally, deletes it:

I don't know how foolproof it is though, or whether it's worth going to that trouble.

Perhaps I'm missing something but if it's a macro that generates the cell's contents (title, description etc.) then wouldn't that macro be able to correctly bold the title and description labels?  (Maybe the answer to that is obvious to someone who's not in need of sleep.)


' Create a bookmark.
ActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="TitleBookmark"

' Go to the bookmark.
    Selection.GoTo What:=wdGoToBookmark, Name:="TitleBookmark"
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

' Delete the bookmark.
ActiveDocument.Bookmarks("TitleBookmark").Delete

Open in new window

0
sherydAuthor Commented:
Solution works but only partially and doesn't address the problem in totality
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.