I can't figure out how I can change the font style/color of a specific bookmark. Every time I try something, I keep getting the null exception error and and I can't figure out where I'm going wrong in referencing the bookmark itself.

Here's what I've got so far in creating the bookmark:

oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add("path and location of Template.dotx")

        oDoc.Bookmarks.Item("Bookmark1").Range.Text = Textbox1.Text
        'I keep thinking something like this next line will work, but to no avail
        oDoc.Bookmarks.Item("Bookmark1").Range.Font.color = Word.WdColor.wdColorAqua

Dani AI

Generated

A NullReference when you touch a bookmark after writing text usually means the bookmark no longer exists. Setting a bookmark’s Range.Text replaces the bookmark with the new text, so a subsequent call like Document.Bookmarks("Bookmark1") can fail. Check the bookmark exists before you use it and re-create it after replacing the text. (wordmvp.com)

Use this pattern: grab the bookmark Range into a variable, write the text, apply formatting to that Range, then re-add the bookmark around the new text. The code below shows the idea (early-binding). Add a reference to the Microsoft Word Object Library if you want the Word enums and IntelliSense.

' Early-binding (requires reference to Microsoft Word X.X Object Library)
Dim app As New Word.Application()
Dim doc As Word.Document = app.Documents.Add(templatePath)
Dim bmRange As Word.Range = doc.Bookmarks("Bookmark1").Range

bmRange.Text = TextBox1.Text
bmRange.Font.Color = Word.WdColor.wdColorAqua

doc.Bookmarks.Add("Bookmark1", bmRange)
app.Visible = True

If you use late-binding (CreateObject) you won't have Word enums available — either use the numeric/RGB value or convert a .NET color to an OLE color. Example (late-binding):

Dim app As Object = CreateObject("Word.Application")
Dim doc As Object = app.Documents.Add(templatePath)
Dim rng As Object = doc.Bookmarks("Bookmark1").Range

rng.Text = TextBox1.Text
rng.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua)

doc.Bookmarks.Add("Bookmark1", rng)
app.Visible = True

Notes and quick troubleshooting:

  • Verify the bookmark name exactly (use Bookmarks.Exists before referencing). (learn.microsoft.com)
  • Re-creating the bookmark with Bookmarks.Add is the standard fix after replacing Range.Text. (learn.microsoft.com)
  • Editing bookmarks that live in headers/first positions can have odd side effects (watermark/header quirks); test with your template. (stackoverflow.com)

As posted by , using a Bookmark/Range object is the right direction; the extra step most people miss is re-adding the bookmark after you replace its Range.

Here is a good starting point.

This was pulled directly from that page:

Private Sub BookmarkFont()

Me.Paragraphs(1).Range.InsertParagraphBefore()
Dim Bookmark1 As Microsoft.Office.Tools.Word.Bookmark = _
    Me.Controls.AddBookmark(Me.Paragraphs(1).Range, "Bookmark1")
Bookmark1.Text = "This is sample bookmark text."
Bookmark1.Words(3).Font.Color = Word.WdColor.wdColorBlue

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.