tags:

views:

362

answers:

2

hi,

I'm creating a word document programatically using VBA.

1) I have a a string with value - "Strategy". I want to make it bold and to be displayed in the word document.

I have tried this below, but the text is never changed:

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")

Dim strategy As String
strategy = "STRATEGY"
Dim objWdRange As Word.Range


wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("C:\Program Files\DailyStrategy.doc")


With wrdDoc
    If wrdDoc.Bookmarks.Exists("MarketCommentry") Then
        wrdDoc.Bookmarks("MarketCommentry").Range.Text =  strategy 

    Set objWdRange = wrdDoc.Content
     With objWdRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "STRATEGY"

        'Make found bold and italic
        With .Replacement.Font
            .Bold = True
            .Italic = True
        End With
        .Execute Replace:=wdReplaceAll
    End With
End With

End If regards

Kojo

+1  A: 

EDIT:: I should have better tried in the VBA debugger first, which I did now. This one should work:

With wrdDoc
   Set objWdRange = wrdDoc.Content
    With objWdRange.Find
       .ClearFormatting
       .Text = "STRATEGY"
       .Execute Replace:=wdReplaceNone
   End With
End With

If objWdRange.Find.Found Then
  'Make found bold and italic
  With objWdRange.Font
       .Bold = True
      .Italic = True
  End With
End If
Doc Brown
Do you mean i should call it just after this line? With objWdRange.Find .Execute Replace:=wdReplaceAll
Kojof
*.Execute* should find and select your text. Then you can apply your changes to the Font. If you don't select anything first, changing the font is useless. I have added the relevant changes as an example to my answer.
Doc Brown
hi, thanks for replying, it makes more sense. However this line does not compile wrdDoc.Selection.Font. I changed it to wrdApp.Selection.Font, but it still doesn't change the text when i run the application.
Kojof
see my changed answer above
Doc Brown
Thanks a lot for that. I've tested it, and it works well if the text is directly on the page, however in the case of text inside textboxes, it doesn't appear to work....Is there a way of getting round this? Also i think you need .Replacement.Text = "TextToChange" in the first part of your answer, otherwise the replacement text is blank.
Kojof
If you do not really want to replace the text, you don't have to set *.Replacement.Text*, just use *.Execute Replace:=wdReplaceNone*. For Textboxes, you can try to loop over all *wordDoc.Shapes*, look if their *.TextFrame.HasText* is true and then use the above "Find/set to bold" technique for the Range object *.TextFrame.ContainingRange*. However, if you have Textboxes inside other shapes, you may have to recurse this (each Shape object can have further Shape objects inside, you reach them via *GroupItems*)
Doc Brown
thanks for that. You're a rock star! lol
Kojof
A: 

If you can select the string try something like

Selection.Font.Bold = True

and see if that works

kevchadders
hi, sorry hmy VBA is quite basic. ow do i select the text , if it is part of a paragraph?Does this line below, not select all the text in the document?Set objWdRange = wrdDoc.Content
Kojof