Formatting Word bookmark text

mike phillips

Member
Joined
Aug 21, 2015
Messages
7
Programming Experience
5-10
Fresh from the help on the IDE forum, and having extensively Googled, I am now stuck on trying to format text written to a Word bookmark. I have successfully done this in Excel and tried replicating the code with no success. The code runs ok with no errors but does not produce the required text format.

Grateful for a nudge.

VB.NET:
Dim oWord As Word.Application
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open("C:\WelfareForm.doc")  
Dim oDoc As Word.Document
Dim wdrng As Word.Range
wdrng = oDoc.Bookmarks.Item('BOOKMARK').Range.Style
With wdrng
            .Font.Color = 255
            .Font.Bold = True
            .Font.Size = 18
            .Text = "foo"
 End With
 
Dim wdrng As Word.Range
wdrng = oDoc.Bookmarks.Item('BOOKMARK').Range.Style
The Style property represents a Style type object, but you are assigning it to a Range type variable.
 
The Style property represents a Style type object, but you are assigning it to a Range type variable.

Yes, my typo (that was one of my thrashng about attempts....:apologetic:)


VB.NET:
Dim oWord As Word.Application
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open("C:\WelfareForm.doc")  
Dim oDoc As Word.Document
Dim wdrng As Word.Range
wdrng = oDoc.Bookmarks.Item('BOOKMARK').Range
With wdrng
            .Font.Color = 255
            .Font.Bold = True
            .Font.Size = 18
            .Text = "foo"
 End With

will print 'foo' in the bookmark but not do anything else. How do I get the fomatting?
 
Maybe Range.Style.Font? I don't have Office here so I can't say.
Range Members (Word)
Style Members (Word)

Range is, unforunately, 'not a menber of Range' :apologetic: BUT, kicking your VBtin can down the road I came upon a working solution, so many thanks for the responses.

VB.NET:
Dim oWord As Word.Application
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Open("C:\WelfareForm.doc")  
Dim oDoc As Word.Document
Dim wdrng As Word.Range
wdrng = oDoc.Bookmarks.Item('BOOKMARK').Range
wdrng.Text = "Foo"
With wdrng.Font
    .Color = 255
    .Bold = True
    .Size = 8
End With
on to the next hurdle!!
 
Back
Top