Formatted text to Word

alpair Posted messages 1 Status Member -  
 Dartagnan123 -
Hello everyone,

I am managing a Word document from Excel using VBA. I want to write text with formatting (bold + italic) at the location of a Word bookmark, but I can only write plain text.
I tried using the clipboard, but it crashes.
Here’s my small piece of code:

Dim MyText As String
MyText = "Top the CCM forum"
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Set wordApp = CreateObject("word.application")
Set wordDoc = wordApp.Documents.Open("C:\Test.doc")
'----------- What works ----- :-) ---------

wordDoc.Bookmarks("HERE").Range.Text = MyText

'----------- What doesn't work :-( ------

With wordDoc.Bookmarks("HERE")
.Range.Font.Size = 20
.Range.Bold = wdToggle
.Range.Italic = wdToggle
.Range.Text = MyText
End With

I have been struggling for 2 days surfing the forums which is informative but unfortunately without results.
A little help?
Thank you
Configuration: Windows XP Internet Explorer 6.0 Office 2003

1 answer

Yipee
 
Hello,

The code is good, you just need to select the text before applying the changes
(similar to the manual action in Word: if you change the font definition, it does not affect the text located just after the cursor.).

See you!
2
Dartagnan123
 
Hello,

I am working with VBA Excel 2003 on a Word document.
Do you know how to select text from VBA Excel in a Word document in order to change the Bold format for just a part of a sentence (for example, a word)?

Thank you
0