View Single Post
 
Old 08-18-2019, 08:10 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Clearly you are not adding text to the bookmark, but next to it.

There is no need to open the header view to write to it if you use ranges, nor is there any need to delete the bookmark.

The following will create the bookmark if not present already and then add the text to it and reset the bookmark to the added text. See also the FillBM function on my web site for filling existing bookmarks.
Code:
Sub addBM_Header()
Dim oRng As Range
Dim oBM As Bookmark
    'Check if the bookmark exists
    For Each oBM In ActiveDocument.Bookmarks
        If oBM.Name = "NombreEncab" Then
            GoTo FillBM
            Exit For
        End If
    Next oBM
    'Bookmark not present so add-it
    Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    If Len(oRng) > 3 Then
        oRng.Start = oRng.Start + 3
        oRng.Collapse 1
    End If
    ActiveDocument.Bookmarks.Add Range:=oRng, Name:="NombreEncab"

FillBM:
    'Write to the bookmark
    Set oRng = ActiveDocument.Bookmarks("NombreEncab").Range
    oRng.Text = "bookmark text"
    'Reset the bookmark
    ActiveDocument.Bookmarks.Add Range:=oRng, Name:="NombreEncab"
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote