How do i make a string bold
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.Fo开发者_StackOverflow中文版nt
.Bold = True
.Italic = True
End With
.Execute Replace:=wdReplaceAll
End With
End With
End If regards
Kojo
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
精彩评论